From c65cabd3a59300ee157a5630b4b99667fbb8b0f4 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 2 Jan 2023 17:27:30 +0100
Subject: [PATCH] wip

---
 .../website/page/communication_posts.rb       | 15 +++++++++
 app/models/communication/website/page/home.rb | 11 +++++++
 .../communication/website/page/special.rb     | 17 ++++++++++
 .../communication/website/page/with_kind.rb   | 31 ++++++++++++++-----
 .../communication/website/page/with_path.rb   |  9 ++----
 5 files changed, 68 insertions(+), 15 deletions(-)
 create mode 100644 app/models/communication/website/page/communication_posts.rb
 create mode 100644 app/models/communication/website/page/home.rb
 create mode 100644 app/models/communication/website/page/special.rb

diff --git a/app/models/communication/website/page/communication_posts.rb b/app/models/communication/website/page/communication_posts.rb
new file mode 100644
index 000000000..6b81a470a
--- /dev/null
+++ b/app/models/communication/website/page/communication_posts.rb
@@ -0,0 +1,15 @@
+class Communication::Website::Page::CommunicationPosts < Communication::Website::Page::Special
+
+  def git_path(website)
+    "#{git_path_content_prefix(website)}posts/_index.html"
+  end
+
+  def git_dependencies
+    [
+      website.config_default_permalinks,
+      website.categories,
+      website.authors.map(&:author),
+      website.posts
+    ].flatten
+  end
+end
\ No newline at end of file
diff --git a/app/models/communication/website/page/home.rb b/app/models/communication/website/page/home.rb
new file mode 100644
index 000000000..14ebb4cc7
--- /dev/null
+++ b/app/models/communication/website/page/home.rb
@@ -0,0 +1,11 @@
+class Communication::Website::Page::Home < Communication::Website::Page::Special  
+  def git_path(website)
+    "#{git_path_content_prefix(website)}_index.html"
+  end
+
+  protected 
+
+  def set_slug
+    self.slug = ''
+  end
+end
\ No newline at end of file
diff --git a/app/models/communication/website/page/special.rb b/app/models/communication/website/page/special.rb
new file mode 100644
index 000000000..d5770ee21
--- /dev/null
+++ b/app/models/communication/website/page/special.rb
@@ -0,0 +1,17 @@
+class Communication::Website::Page::Special < Communication::Website::Page
+  def is_necessary?
+    true
+  end
+
+  def full_width_by_default?
+    true
+  end
+
+  def published_by_default?
+    true
+  end
+
+  def unpublishable?
+    true
+  end
+end
diff --git a/app/models/communication/website/page/with_kind.rb b/app/models/communication/website/page/with_kind.rb
index b3724e964..bfb4c49e0 100644
--- a/app/models/communication/website/page/with_kind.rb
+++ b/app/models/communication/website/page/with_kind.rb
@@ -11,17 +11,18 @@ module Communication::Website::Page::WithKind
       research_papers: 30,
       research_volumes: 32,
       legal_terms: 80,
-        sitemap: 81,
-        privacy_policy: 82,
-        accessibility: 83,
+      sitemap: 81,
+      privacy_policy: 82,
+      accessibility: 83,
       organizations: 90,
       persons: 100,
-        administrators: 110,
-        authors: 120,
-        researchers: 130,
-        teachers: 140
+      administrators: 110,
+      authors: 120,
+      researchers: 130,
+      teachers: 140
     }, _prefix: 'kind'
 
+    # -> dans les nouvelles classes
     SPECIAL_PAGES_WITH_GIT_SPECIAL_PATH = [
       'communication_posts',
       'education_programs',
@@ -36,20 +37,33 @@ module Communication::Website::Page::WithKind
       'teachers'
     ].freeze
 
+    # deprecated, remove
     after_create :move_legacy_root_pages, if: :kind_home?
 
+    def as_special_page
+      @as_special_page ||= begin
+        # A normal page is always a normal page
+        return self if is_regular_page?
+        # A special page is the same in the database, but has some additional and specific methods
+        special_page_class = "Communication::Website::Page::#{kind.to_s.classify}".constantize
+        special_page_class.find id
+      end
+    end
+
     def is_special_page?
       kind != nil
     end
 
     def is_regular_page?
-      !is_special_page?
+      kind == nil
     end
 
+    # -> dans les nouvelles classes
     def has_special_git_path?
       is_special_page? && SPECIAL_PAGES_WITH_GIT_SPECIAL_PATH.include?(kind)
     end
 
+    # -> dans les nouvelles classes
     def special_page_git_dependencies(website)
       dependencies = [website.config_default_permalinks]
       case kind
@@ -76,6 +90,7 @@ module Communication::Website::Page::WithKind
 
   private
 
+  # deprecated, remove
   def move_legacy_root_pages
     root_pages = website.pages.where.not(id: id).root
     root_pages.update_all(parent_id: id)
diff --git a/app/models/communication/website/page/with_path.rb b/app/models/communication/website/page/with_path.rb
index 984d4b904..ae2f1356c 100644
--- a/app/models/communication/website/page/with_path.rb
+++ b/app/models/communication/website/page/with_path.rb
@@ -13,7 +13,7 @@ module Communication::Website::Page::WithPath
               },
               unless: :kind_home?
 
-    before_validation :check_slug
+    before_validation :set_slug
   end
 
   def path
@@ -53,12 +53,7 @@ module Communication::Website::Page::WithPath
 
   protected
 
-  def check_slug
-    if kind_home?
-      self.slug = ""
-      return
-    end
-
+  def set_slug
     self.slug = to_s.parameterize if self.slug.blank?
     current_slug = self.slug
     n = 0
-- 
GitLab