Skip to content
Snippets Groups Projects
Commit f5292daf authored by pabois's avatar pabois
Browse files

imported authors

parent 42c18d54
No related branches found
No related tags found
No related merge requests found
# == 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
......@@ -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
......
......@@ -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
......
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
......@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment