From 7b1b47ea5668308a746437d4ae7263de6c71c586 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 3 Jan 2023 18:11:24 +0100
Subject: [PATCH] creation wip

---
 app/models/communication/website/page.rb      |  2 +-
 .../website/page/administrator.rb             |  2 +-
 .../communication/website/page/author.rb      |  2 +-
 .../website/page/education_diploma.rb         |  4 +
 .../website/page/education_program.rb         |  4 +
 .../website/page/research_paper.rb            |  4 +
 .../website/page/research_volume.rb           |  4 +
 .../communication/website/page/researcher.rb  |  2 +-
 .../communication/website/page/teacher.rb     |  2 +-
 .../communication/website/page/with_type.rb   | 21 ++++-
 .../website/with_dependencies.rb              |  6 +-
 .../website/with_special_pages.rb             | 82 ++++---------------
 lib/tasks/app.rake                            |  2 +-
 13 files changed, 62 insertions(+), 75 deletions(-)

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 1e0cc646e..fd2e655c6 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -92,7 +92,7 @@ class Communication::Website::Page < ApplicationRecord
                     git_block_dependencies +
                     type_git_dependencies
     dependencies += [parent] if has_parent?
-    dependencies.flatten
+    dependencies.flatten.compact
   end
 
   def git_destroy_dependencies(website)
diff --git a/app/models/communication/website/page/administrator.rb b/app/models/communication/website/page/administrator.rb
index 90e37781e..8a63c70da 100644
--- a/app/models/communication/website/page/administrator.rb
+++ b/app/models/communication/website/page/administrator.rb
@@ -48,7 +48,7 @@ class Communication::Website::Page::Administrator < Communication::Website::Page
   def type_git_dependencies
     [
       website.config_default_permalinks,
-      website.website.administrators.map(&:administrator)
+      website&.administrators&.map(&:administrator)
     ]
   end
 end
diff --git a/app/models/communication/website/page/author.rb b/app/models/communication/website/page/author.rb
index 10e663553..5e7cda9b7 100644
--- a/app/models/communication/website/page/author.rb
+++ b/app/models/communication/website/page/author.rb
@@ -48,7 +48,7 @@ class Communication::Website::Page::Author < Communication::Website::Page
   def type_git_dependencies
     [
       website.config_default_permalinks,
-      website.website.authors.map(&:author)
+      website&.authors&.map(&:author)
     ]
   end
 end
diff --git a/app/models/communication/website/page/education_diploma.rb b/app/models/communication/website/page/education_diploma.rb
index cec9c8b2a..b864ec7c5 100644
--- a/app/models/communication/website/page/education_diploma.rb
+++ b/app/models/communication/website/page/education_diploma.rb
@@ -41,6 +41,10 @@
 #
 class Communication::Website::Page::EducationDiploma < Communication::Website::Page
 
+  def is_necessary_for_website?
+    website.about && website.about&.respond_to(:education_diplomas)
+  end
+
   def full_width
     true
   end
diff --git a/app/models/communication/website/page/education_program.rb b/app/models/communication/website/page/education_program.rb
index 87e0bb52c..1f9334d37 100644
--- a/app/models/communication/website/page/education_program.rb
+++ b/app/models/communication/website/page/education_program.rb
@@ -41,6 +41,10 @@
 #
 class Communication::Website::Page::EducationProgram < Communication::Website::Page
 
+  def is_necessary_for_website?
+    website.about && website.about&.respond_to(:education_programs)
+  end
+
   def full_width
     true
   end
diff --git a/app/models/communication/website/page/research_paper.rb b/app/models/communication/website/page/research_paper.rb
index 5e1669fdb..1497d4cfa 100644
--- a/app/models/communication/website/page/research_paper.rb
+++ b/app/models/communication/website/page/research_paper.rb
@@ -41,6 +41,10 @@
 #
 class Communication::Website::Page::ResearchPaper < Communication::Website::Page
 
+  def is_necessary_for_website?
+    website.about && website.about&.respond_to(:research_papers)
+  end
+
   def current_git_path
     "#{git_path_prefix}papers/_index.html"
   end
diff --git a/app/models/communication/website/page/research_volume.rb b/app/models/communication/website/page/research_volume.rb
index 11d4d39a5..3aca8617b 100644
--- a/app/models/communication/website/page/research_volume.rb
+++ b/app/models/communication/website/page/research_volume.rb
@@ -41,6 +41,10 @@
 #
 class Communication::Website::Page::ResearchVolume < Communication::Website::Page
 
+  def is_necessary_for_website?
+    website.about && website.about&.respond_to(:research_volumes)
+  end
+
   def current_git_path
     "#{git_path_prefix}volumes/_index.html"
   end
diff --git a/app/models/communication/website/page/researcher.rb b/app/models/communication/website/page/researcher.rb
index b3e339784..b098fb254 100644
--- a/app/models/communication/website/page/researcher.rb
+++ b/app/models/communication/website/page/researcher.rb
@@ -48,7 +48,7 @@ class Communication::Website::Page::Researcher < Communication::Website::Page
   def type_git_dependencies
     [
       website.config_default_permalinks,
-      website.website.researchers.map(&:researcher)
+      website&.researchers&.map(&:researcher)
     ]
   end
 end
diff --git a/app/models/communication/website/page/teacher.rb b/app/models/communication/website/page/teacher.rb
index 205415104..f3ca0a2f4 100644
--- a/app/models/communication/website/page/teacher.rb
+++ b/app/models/communication/website/page/teacher.rb
@@ -48,7 +48,7 @@ class Communication::Website::Page::Teacher < Communication::Website::Page
   def type_git_dependencies
     [
       website.config_default_permalinks,
-      website.website.teachers.map(&:teacher)
+      website&.teachers&.map(&:teacher)
     ].flatten
   end
 end
diff --git a/app/models/communication/website/page/with_type.rb b/app/models/communication/website/page/with_type.rb
index 27f32f512..a2ef3eaee 100644
--- a/app/models/communication/website/page/with_type.rb
+++ b/app/models/communication/website/page/with_type.rb
@@ -3,7 +3,26 @@ module Communication::Website::Page::WithType
 
   
   included do
-    HOME_TYPE = '::Communication::Website::Page::Home'
+    HOME_TYPE = 'Communication::Website::Page::Home'
+
+    TYPES = [
+      Communication::Website::Page::Home, # Always start with home
+      Communication::Website::Page::Accessibility,
+      Communication::Website::Page::Administrator,
+      Communication::Website::Page::Author,
+      Communication::Website::Page::CommunicationPost,
+      Communication::Website::Page::EducationDiploma,
+      Communication::Website::Page::EducationProgram,
+      Communication::Website::Page::LegalTerm,
+      Communication::Website::Page::Organization,
+      Communication::Website::Page::Person,
+      Communication::Website::Page::PrivacyPolicy,
+      Communication::Website::Page::ResearchPaper,
+      Communication::Website::Page::ResearchVolume,
+      Communication::Website::Page::Researcher,
+      Communication::Website::Page::Sitemap,
+      Communication::Website::Page::Teacher
+    ]
 
     scope :home, -> { where(type: HOME_TYPE) }
   end
diff --git a/app/models/communication/website/with_dependencies.rb b/app/models/communication/website/with_dependencies.rb
index e303cb563..e1fb3f03d 100644
--- a/app/models/communication/website/with_dependencies.rb
+++ b/app/models/communication/website/with_dependencies.rb
@@ -61,15 +61,15 @@ module Communication::Website::WithDependencies
   end
 
   def administrators
-    about.administrators
+    about&.administrators
   end
 
   def researchers
-    about.researchers
+    about&.researchers
   end
 
   def teachers
-    about.teachers
+    about&.teachers
   end
 
   def people_in_blocks
diff --git a/app/models/communication/website/with_special_pages.rb b/app/models/communication/website/with_special_pages.rb
index e9014e600..b64c85051 100644
--- a/app/models/communication/website/with_special_pages.rb
+++ b/app/models/communication/website/with_special_pages.rb
@@ -2,79 +2,31 @@ module Communication::Website::WithSpecialPages
   extend ActiveSupport::Concern
 
   included do
-
-    # first level pages with test
-    SPECIAL_PAGES_WITH_TEST = [
-      'legal_terms',
-      'sitemap',
-      'privacy_policy',
-      'accessibility',
-      'communication_posts',
-      'education_programs',
-      'education_diplomas',
-      'research_papers',
-      'research_volumes',
-      'organizations'
-    ]
-
-    # team pages
-    SPECIAL_PAGES_FOR_PERSONS = [
-      'administrators',
-      'authors',
-      'researchers',
-      'teachers'
-    ]
-
     after_create :create_missing_special_pages
-    after_touch :create_missing_special_pages, :manage_special_pages_publication
-
-    def special_page(kind)
-      pages.where(kind: kind).first
-    end
-
-    def create_missing_special_pages
-      homepage = create_special_page('home')
-      SPECIAL_PAGES_WITH_TEST.each do |kind|
-        create_special_page(kind, homepage.id) if public_send("has_#{kind}?")
-      end
-      if has_persons?
-        persons = create_special_page('persons', homepage.id)
-        SPECIAL_PAGES_FOR_PERSONS.each do |kind|
-          create_special_page(kind, persons.id) if public_send("has_#{kind}?")
-        end
-      end
-    end
+    after_touch :create_missing_special_pages
+  end
 
-    def manage_special_pages_publication
-      special_pages_keys.each do |kind|
-        published = public_send("has_#{kind}?")
-        special_page(kind).update(published: published)
-      end
+  def create_missing_special_pages
+    home = nil
+    special_pages = Communication::Website::Page::TYPES.each do |page_class|
+      page = create_special_page page_class, home
+      home = page if home.nil?
     end
-
   end
 
-  private
+  protected
 
-  def create_special_page(kind, parent_id = nil)
-    i18n_key = "communication.website.pages.defaults.#{kind}"
-    page = pages.where(kind: kind).first_or_initialize(
-      title: I18n.t("#{i18n_key}.title"),
-      slug: I18n.t("#{i18n_key}.slug"),
-      description_short: I18n.t("#{i18n_key}.description_short"),
-      parent_id: parent_id,
-      published: true,
-      university_id: university_id
-    )
+  def create_special_page(page_class, parent)
+    page = page_class.where(website: self, university: university).first_or_initialize
     if page.new_record?
-      # This is a bit brutal, as it might generate several syncs at the same time
-      page.save_and_sync
+      i18n_key = "communication.website.pages.defaults.#{page.type_key}"
+      page.title = I18n.t("#{i18n_key}.title")
+      page.slug = I18n.t("#{i18n_key}.slug")
+      page.parent = parent
+      page.full_width = page.full_width_by_default?
+      page.published = page.published_by_default?
+      page.save_and_sync if page.is_necessary_for_website?
     end
     page
   end
-
-  def special_pages_keys
-    @special_pages_keys ||= pages.where.not(kind: nil).pluck(:kind).uniq
-  end
-
 end
diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake
index 6324ffc3b..3d2e0425f 100644
--- a/lib/tasks/app.rake
+++ b/lib/tasks/app.rake
@@ -10,7 +10,7 @@ namespace :app do
   task fix: :environment do
     Communication::Website::Page.find_each do |page|
       if page.kind
-        type = "::Communication::Website::Page::#{page.kind.to_s.classify}"
+        type = "Communication::Website::Page::#{page.kind.to_s.classify}"
         page.update_column :type, type
       end
     end
-- 
GitLab