From 7a15450dbe8d3673045df2582aca7c25573a6b97 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 9 Dec 2021 14:19:59 +0100
Subject: [PATCH] github_manifest

---
 .../communication/website/github_file.rb      | 29 ++++++++++---------
 .../communication/website/imported/website.rb | 25 +++++++++++-----
 app/models/concerns/with_github_files.rb      | 20 +++++++++++--
 ...r_to_communication_website_github_files.rb |  5 ++++
 db/schema.rb                                  |  3 +-
 .../communication/website/github_files.yml    | 16 +++++-----
 .../communication/website/github_file_test.rb | 15 +++++-----
 7 files changed, 73 insertions(+), 40 deletions(-)
 create mode 100644 db/migrate/20211209110929_add_manifest_identifier_to_communication_website_github_files.rb

diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb
index edd70c33d..ea3739206 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 9dabc4851..8965a6d01 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 e5cdda274..d2d4988ae 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 000000000..9734e721d
--- /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 0b5bed61e..3d52f5d0b 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 1ee87a4e4..f62b00bd4 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 7e80aa4a4..72374f59d 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
 #
-- 
GitLab