From b27f8927553bcba06e9f761c41d979ce65713ad3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 9 Dec 2021 16:55:44 +0100
Subject: [PATCH] multiple github files for authors + fix after_touch

---
 app/models/communication/website/author.rb        | 15 ++++++++++++++-
 app/models/communication/website/category.rb      | 15 +++------------
 app/models/communication/website/menu.rb          |  2 ++
 app/models/concerns/with_github_files.rb          |  5 ++---
 .../communication/website/authors/jekyll.html.erb |  7 ++++++-
 .../website/authors/jekyll_collection.yml.erb     |  3 +++
 6 files changed, 30 insertions(+), 17 deletions(-)
 create mode 100644 app/views/admin/communication/website/authors/jekyll_collection.yml.erb

diff --git a/app/models/communication/website/author.rb b/app/models/communication/website/author.rb
index 40ae84026..25480be7f 100644
--- a/app/models/communication/website/author.rb
+++ b/app/models/communication/website/author.rb
@@ -52,7 +52,20 @@ class Communication::Website::Author < ApplicationRecord
 
   # Override from WithGithubFiles
   def github_path_generated
-    "_authors/#{slug}.html"
+    "auteurs/#{slug}.html"
+  end
+
+  def github_manifest
+    super << {
+      identifier: "collection_item",
+      generated_path: "_data/authors/#{slug}.yml",
+      data: -> (github_file) { ApplicationController.render(
+        template: "admin/communication/website/authors/jekyll_collection",
+        formats: [:yml],
+        layout: false,
+        assigns: { author: self, github_file: github_file }
+      ) }
+    }
   end
 
   protected
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 01063ea02..e029f89a5 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -81,24 +81,15 @@ class Communication::Website::Category < ApplicationRecord
     "actualites/#{slug}.html"
   end
 
-  # Override from WithGithubFiles
-  def to_jekyll(github_file)
-    ApplicationController.render(
-      template: "admin/communication/website/categories/jekyll",
-      layout: false,
-      assigns: { category: self, github_file: github_file }
-    )
-  end
-
   def github_manifest
     super << {
       identifier: "collection_item",
-      generated_path: "_data/categories/#{slug}.yml",
+      generated_path: "_data/authors/#{slug}.yml",
       data: -> (github_file) { ApplicationController.render(
-        template: "admin/communication/website/categories/jekyll_collection",
+        template: "admin/communication/website/authors/jekyll_collection",
         formats: [:yml],
         layout: false,
-        assigns: { category: self, github_file: github_file }
+        assigns: { author: self, github_file: github_file }
       ) }
     }
   end
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index 5b39c46ca..3214611a3 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -31,6 +31,8 @@ class Communication::Website::Menu < ApplicationRecord
   validates :title, :identifier, presence: true
   validates :identifier, uniqueness: { scope: :communication_website_id }
 
+  after_touch :publish_github_files
+
   scope :ordered, -> { order(created_at: :asc) }
 
   def to_s
diff --git a/app/models/concerns/with_github_files.rb b/app/models/concerns/with_github_files.rb
index fb0ad047c..5a6aea780 100644
--- a/app/models/concerns/with_github_files.rb
+++ b/app/models/concerns/with_github_files.rb
@@ -6,7 +6,6 @@ module WithGithubFiles
 
     after_save :create_github_files
     after_save_commit :publish_github_files
-    after_touch :publish_github_files
   end
 
   def force_publish!
@@ -41,7 +40,7 @@ module WithGithubFiles
   def create_github_files
     list_of_websites.each do |website|
       github_manifest.each do |manifest_item|
-        github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier])
+        github_files.where(website: website, manifest_identifier: manifest_item[:identifier]).first_or_create
       end
     end
   end
@@ -49,7 +48,7 @@ module WithGithubFiles
   def publish_github_files
     list_of_websites.each do |website|
       github_manifest.each do |manifest_item|
-        github_file = github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier])
+        github_file = github_files.where(website: website, manifest_identifier: manifest_item[:identifier]).first_or_create
         github_file.publish
       end
     end
diff --git a/app/views/admin/communication/website/authors/jekyll.html.erb b/app/views/admin/communication/website/authors/jekyll.html.erb
index 7c6661057..2f36fcfbd 100644
--- a/app/views/admin/communication/website/authors/jekyll.html.erb
+++ b/app/views/admin/communication/website/authors/jekyll.html.erb
@@ -1,10 +1,15 @@
 ---
 title: "<%= @author.to_s %>"
+slug: "<%= @author.slug %>"
 identifier: "<%= @author.id %>"
+permalink: "/auteurs/<%= @author.slug %>"
 first_name: "<%= @author.first_name %>"
 last_name: "<%= @author.last_name %>"
-slug: "<%= @author.slug %>"
 biography: >
   <%= prepare_for_github @author.biography, @author.university %>
+pagination:
+  enabled: true
+  tag: <%= @author.id %>
+  permalink: /:num/
 ---
 <%= @github_file.github_frontmatter.content.html_safe %>
diff --git a/app/views/admin/communication/website/authors/jekyll_collection.yml.erb b/app/views/admin/communication/website/authors/jekyll_collection.yml.erb
new file mode 100644
index 000000000..1f1259c00
--- /dev/null
+++ b/app/views/admin/communication/website/authors/jekyll_collection.yml.erb
@@ -0,0 +1,3 @@
+name: <%= @author.to_s %>
+identifier: <%= @author.id %>
+link: /auteurs/<%= @author.slug %>
-- 
GitLab