diff --git a/app/models/communication/website/imported/author.rb b/app/models/communication/website/imported/author.rb new file mode 100644 index 0000000000000000000000000000000000000000..8040783bd0d7b700300e7be17cbf5e47ee3b8c0e --- /dev/null +++ b/app/models/communication/website/imported/author.rb @@ -0,0 +1,74 @@ +# == Schema Information +# +# Table name: communication_website_imported_authors +# +# id :uuid not null, primary key +# data :jsonb +# description :text +# identifier :string +# name :string +# slug :string +# created_at :datetime not null +# updated_at :datetime not null +# author_id :uuid not null +# university_id :uuid not null +# website_id :uuid not null +# +# Indexes +# +# idx_communication_website_imported_auth_on_author (author_id) +# idx_communication_website_imported_auth_on_university (university_id) +# idx_communication_website_imported_auth_on_website (website_id) +# +# Foreign Keys +# +# fk_rails_... (author_id => communication_website_authors.id) +# fk_rails_... (university_id => universities.id) +# fk_rails_... (website_id => communication_website_imported_websites.id) +# +class Communication::Website::Imported::Author < ApplicationRecord + + belongs_to :university + belongs_to :website, + class_name: 'Communication::Website::Imported::Website' + belongs_to :author, + class_name: 'Communication::Website::Author', + optional: true + + before_validation :sync + + default_scope { order(:name) } + + def data=(value) + super value + self.slug = value['slug'] + self.name = value['name'] + self.description = value['description'] + end + + def to_s + "#{name}" + end + + protected + + def sync + if author.nil? + self.author = Communication::Website::Author.new university: university, + website: website.website # Real website, not imported website + self.author.last_name = "Doe" # No title yet + self.author.first_name = "John" # No title yet + self.author.save + end + puts "Update author #{author.id}" + sanitized_name = Wordpress.clean_string(self.name.to_s).split(' ') + sanitized_biography = Wordpress.clean_string description.to_s + unless sanitized_name.blank? + author.first_name = sanitized_name.first + author.last_name = sanitized_name[1..10].join(' ') + end + author.slug = slug + author.biography = sanitized_biography unless sanitized_biography.blank? + author.save + end +end diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index 2a168e2369076d02dfdc963ca9ad6dad25a2f0c0..a740032f9a5984828d796bab63467c911ca33265 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -23,6 +23,8 @@ class Communication::Website::Imported::Website < ApplicationRecord belongs_to :university belongs_to :website, class_name: 'Communication::Website' + has_many :authors, + class_name: 'Communication::Website::Imported::Author' has_many :categories, class_name: 'Communication::Website::Imported::Category' has_many :media, @@ -33,6 +35,7 @@ class Communication::Website::Imported::Website < ApplicationRecord class_name: 'Communication::Website::Imported::Post' def run! + sync_authors sync_categories sync_media sync_pages @@ -46,6 +49,14 @@ class Communication::Website::Imported::Website < ApplicationRecord @wordpress ||= Wordpress.new website.domain_url end + def sync_authors + wordpress.authors.each do |data| + author = authors.where(university: university, identifier: data['id']).first_or_initialize + author.data = data + author.save + end + end + def sync_categories wordpress.categories.each do |data| category = categories.where(university: university, identifier: data['id']).first_or_initialize diff --git a/app/services/wordpress.rb b/app/services/wordpress.rb index ad6add34dff068d9233ca83fb2f31a610fa00d6c..627871fb9d23b3a00d8e3f2ec60394ba2c2f08ac 100644 --- a/app/services/wordpress.rb +++ b/app/services/wordpress.rb @@ -51,6 +51,10 @@ class Wordpress @domain = domain end + def authors + load "#{domain}/wp-json/wp/v2/users" + end + def categories load "#{domain}/wp-json/wp/v2/categories" end diff --git a/db/migrate/20211028120556_create_communication_website_imported_author.rb b/db/migrate/20211028120556_create_communication_website_imported_author.rb new file mode 100644 index 0000000000000000000000000000000000000000..12195c70b5caf0e496dfbc0d6fd27c08f144a869 --- /dev/null +++ b/db/migrate/20211028120556_create_communication_website_imported_author.rb @@ -0,0 +1,28 @@ +class CreateCommunicationWebsiteImportedAuthor < ActiveRecord::Migration[6.1] + def change + create_table :communication_website_imported_authors, id: :uuid do |t| + t.references :university, + null: false, + foreign_key: true, + type: :uuid, + index: { name: 'idx_communication_website_imported_auth_on_university' } + t.references :website, + null: false, + foreign_key: { to_table: :communication_website_imported_websites }, + type: :uuid, + index: { name: 'idx_communication_website_imported_auth_on_website' } + t.references :author, + null: false, + foreign_key: { to_table: :communication_website_authors }, + type: :uuid, + index: { name: 'idx_communication_website_imported_auth_on_author' } + + t.string :name + t.text :description + t.string :slug + t.string :identifier + t.jsonb :data + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index cce82cf826694bdebd305f8f5de3e2200925c781..3a95f13d680d92b545bec755b55ed6f5bea0f8d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_28_100510) do +ActiveRecord::Schema.define(version: 2021_10_28_120556) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -114,6 +114,22 @@ ActiveRecord::Schema.define(version: 2021_10_28_100510) do t.index ["communication_website_post_id", "communication_website_category_id"], name: "post_category" end + create_table "communication_website_imported_authors", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "university_id", null: false + t.uuid "website_id", null: false + t.uuid "author_id", null: false + t.string "name" + t.text "description" + t.string "slug" + t.string "identifier" + t.jsonb "data" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["author_id"], name: "idx_communication_website_imported_auth_on_author" + t.index ["university_id"], name: "idx_communication_website_imported_auth_on_university" + t.index ["website_id"], name: "idx_communication_website_imported_auth_on_website" + end + create_table "communication_website_imported_categories", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.uuid "website_id", null: false @@ -448,6 +464,9 @@ ActiveRecord::Schema.define(version: 2021_10_28_100510) do add_foreign_key "communication_website_categories", "communication_website_categories", column: "parent_id" add_foreign_key "communication_website_categories", "communication_websites" add_foreign_key "communication_website_categories", "universities" + add_foreign_key "communication_website_imported_authors", "communication_website_authors", column: "author_id" + add_foreign_key "communication_website_imported_authors", "communication_website_imported_websites", column: "website_id" + add_foreign_key "communication_website_imported_authors", "universities" add_foreign_key "communication_website_imported_categories", "communication_website_categories", column: "category_id" add_foreign_key "communication_website_imported_categories", "communication_website_imported_websites", column: "website_id" add_foreign_key "communication_website_imported_categories", "universities"