diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb index edd70c33d37a68b6508229bd4fa28157ec3b81f6..ea37392060c98559fed7a497ddf8efb24486a5f6 100644 --- a/app/models/communication/website/github_file.rb +++ b/app/models/communication/website/github_file.rb @@ -2,13 +2,14 @@ # # Table name: communication_website_github_files # -# id :uuid not null, primary key -# about_type :string not null -# github_path :string -# created_at :datetime not null -# updated_at :datetime not null -# about_id :uuid not null -# website_id :uuid not null +# id :uuid not null, primary key +# about_type :string not null +# github_path :string +# manifest_identifier :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null +# website_id :uuid not null # # Indexes # @@ -80,9 +81,9 @@ class Communication::Website::GithubFile < ApplicationRecord def github_params { - path: about.github_path_generated, + path: manifest_data[:generated_path], previous_path: github_path, - data: about.to_jekyll(self) + data: manifest_data[:data].call(self) } end @@ -106,10 +107,6 @@ class Communication::Website::GithubFile < ApplicationRecord "[#{about.class.name.demodulize}] Save #{about.to_s}" end - def github_blob_commit_message(blob) - "[Medium] Save ##{blob.id}" - end - def github_remove_commit_message "[#{about.class.name.demodulize}] Remove #{about.to_s}" end @@ -117,4 +114,10 @@ class Communication::Website::GithubFile < ApplicationRecord def github_blob_remove_commit_message(blob) "[Medium] Remove ##{blob.id}" end + + def manifest_data + @manifest_data ||= about.github_manifest.detect { |item| + item[:identifier] == manifest_identifier + } + end end diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index 9dabc4851e20c763a92732d8d385bb5fce8a225f..8965a6d01531f44e007c7380265b7c160ebd1be2 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -60,7 +60,7 @@ class Communication::Website::Imported::Website < ApplicationRecord def sync_authors begin - Communication::Website::Author.skip_callback(:save, :after, :publish_github_files) + skip_publish_callback(Communication::Website::Author) wordpress.authors.each do |data| author = authors.where(university: university, identifier: data['id']).first_or_initialize author.data = data @@ -69,13 +69,13 @@ class Communication::Website::Imported::Website < ApplicationRecord # Batch update all changes (1 query only, good for github API limits) website.publish_authors! ensure - Communication::Website::Author.set_callback(:save, :after, :publish_github_files) + set_publish_callback(Communication::Website::Author) end end def sync_categories begin - Communication::Website::Category.skip_callback(:save, :after, :publish_github_files) + skip_publish_callback(Communication::Website::Category) wordpress.categories.each do |data| category = categories.where(university: university, identifier: data['id']).first_or_initialize category.data = data @@ -85,7 +85,7 @@ class Communication::Website::Imported::Website < ApplicationRecord # Batch update all changes (1 query only, good for github API limits) website.publish_categories! ensure - Communication::Website::Category.set_callback(:save, :after, :publish_github_files) + set_publish_callback(Communication::Website::Category) end end @@ -99,7 +99,7 @@ class Communication::Website::Imported::Website < ApplicationRecord def sync_pages begin - Communication::Website::Page.skip_callback(:save, :after, :publish_github_files) + skip_publish_callback(Communication::Website::Page) wordpress.pages.each do |data| page = pages.where(university: university, identifier: data['id']).first_or_initialize page.data = data @@ -109,13 +109,13 @@ class Communication::Website::Imported::Website < ApplicationRecord # Batch update all changes (1 query only, good for github API limits) website.publish_pages! ensure - Communication::Website::Page.set_callback(:save, :after, :publish_github_files) + set_publish_callback(Communication::Website::Page) end end def sync_posts begin - Communication::Website::Post.skip_callback(:save, :after, :publish_github_files) + skip_publish_callback(Communication::Website::Post) wordpress.posts.each do |data| post = posts.where(university: university, identifier: data['id']).first_or_initialize post.data = data @@ -124,7 +124,7 @@ class Communication::Website::Imported::Website < ApplicationRecord # Batch update all changes (1 query only, good for github API limits) website.publish_posts! ensure - Communication::Website::Post.set_callback(:save, :after, :publish_github_files) + set_publish_callback(Communication::Website::Post) end end @@ -139,4 +139,13 @@ class Communication::Website::Imported::Website < ApplicationRecord generated_element.save end end + + def skip_publish_callback(model) + model.skip_callback(:commit, :after, :publish_github_files, on: [:create, :update]) + end + + + def set_publish_callback(model) + model.set_callback(:commit, :after, :publish_github_files, on: [:create, :update]) + end end diff --git a/app/models/concerns/with_github_files.rb b/app/models/concerns/with_github_files.rb index e5cdda27442324aea770cec730119c87559c6521..d2d4988ae25325c0b5dff6af1621f7ca85592720 100644 --- a/app/models/concerns/with_github_files.rb +++ b/app/models/concerns/with_github_files.rb @@ -25,18 +25,32 @@ module WithGithubFiles ) end + def github_manifest + [ + { + identifier: "primary", + generated_path: github_path_generated, + data: -> (github_file) { to_jekyll(github_file) } + } + ] + end + protected def create_github_files list_of_websites.each do |website| - github_files.find_or_create_by(website: website) + github_manifest.each do |manifest_item| + github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier]) + end end end def publish_github_files list_of_websites.each do |website| - github_file = github_files.find_or_create_by(website: website) - github_file.publish + github_manifest.each do |manifest_item| + github_file = github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier]) + github_file.publish + end end end diff --git a/db/migrate/20211209110929_add_manifest_identifier_to_communication_website_github_files.rb b/db/migrate/20211209110929_add_manifest_identifier_to_communication_website_github_files.rb new file mode 100644 index 0000000000000000000000000000000000000000..9734e721dd690c3bc618522dba72545586065e47 --- /dev/null +++ b/db/migrate/20211209110929_add_manifest_identifier_to_communication_website_github_files.rb @@ -0,0 +1,5 @@ +class AddManifestIdentifierToCommunicationWebsiteGithubFiles < ActiveRecord::Migration[6.1] + def change + add_column :communication_website_github_files, :manifest_identifier, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 0b5bed61e68f479324d773a9a40d0ed87b33ad86..3d52f5d0b550994e8ce1eed622ef92c71f0f0149 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_12_06_102444) do +ActiveRecord::Schema.define(version: 2021_12_09_110929) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -126,6 +126,7 @@ ActiveRecord::Schema.define(version: 2021_12_06_102444) do t.uuid "website_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "manifest_identifier" t.index ["about_type", "about_id"], name: "index_communication_website_github_files_on_about" t.index ["website_id"], name: "index_communication_website_github_files_on_website_id" end diff --git a/test/fixtures/communication/website/github_files.yml b/test/fixtures/communication/website/github_files.yml index 1ee87a4e4b5e7b1a0b7aa77a5ef2072a724383a3..f62b00bd44515d0bd25111210b6ec85cdedca846 100644 --- a/test/fixtures/communication/website/github_files.yml +++ b/test/fixtures/communication/website/github_files.yml @@ -2,13 +2,14 @@ # # Table name: communication_website_github_files # -# id :uuid not null, primary key -# about_type :string not null -# github_path :string -# created_at :datetime not null -# updated_at :datetime not null -# about_id :uuid not null -# website_id :uuid not null +# id :uuid not null, primary key +# about_type :string not null +# github_path :string +# manifest_identifier :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null +# website_id :uuid not null # # Indexes # @@ -19,7 +20,6 @@ # # fk_rails_... (website_id => communication_websites.id) # -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: github_path: MyString diff --git a/test/models/communication/website/github_file_test.rb b/test/models/communication/website/github_file_test.rb index 7e80aa4a4f43adcc1c10d4b6289481b352a00f0f..72374f59d14999ee55f3516ed13be3eefa558f26 100644 --- a/test/models/communication/website/github_file_test.rb +++ b/test/models/communication/website/github_file_test.rb @@ -2,13 +2,14 @@ # # Table name: communication_website_github_files # -# id :uuid not null, primary key -# about_type :string not null -# github_path :string -# created_at :datetime not null -# updated_at :datetime not null -# about_id :uuid not null -# website_id :uuid not null +# id :uuid not null, primary key +# about_type :string not null +# github_path :string +# manifest_identifier :string +# created_at :datetime not null +# updated_at :datetime not null +# about_id :uuid not null +# website_id :uuid not null # # Indexes #