From ed81096d2a5bc3a6265072ba69bf60e623eaee01 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 3 Jan 2023 16:06:59 +0100
Subject: [PATCH] special git paths

---
 .../website/page/administrator.rb             |  4 ++
 .../communication/website/page/author.rb      |  4 ++
 .../website/page/communication_post.rb        |  6 +--
 .../website/page/education_diploma.rb         |  4 ++
 .../website/page/education_program.rb         |  4 ++
 app/models/communication/website/page/home.rb |  4 ++
 .../website/page/organization.rb              |  4 ++
 .../communication/website/page/person.rb      |  4 ++
 .../website/page/research_paper.rb            |  4 ++
 .../website/page/research_volume.rb           |  4 ++
 .../communication/website/page/researcher.rb  |  4 ++
 .../communication/website/page/teacher.rb     |  4 ++
 .../communication/website/page/with_path.rb   | 54 ++++++++++---------
 13 files changed, 77 insertions(+), 27 deletions(-)

diff --git a/app/models/communication/website/page/administrator.rb b/app/models/communication/website/page/administrator.rb
index 5ea50a18f..93fed5bbc 100644
--- a/app/models/communication/website/page/administrator.rb
+++ b/app/models/communication/website/page/administrator.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Administrator < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}administrators/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/author.rb b/app/models/communication/website/page/author.rb
index b6871bad6..e04954cf0 100644
--- a/app/models/communication/website/page/author.rb
+++ b/app/models/communication/website/page/author.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Author < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}authors/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/communication_post.rb b/app/models/communication/website/page/communication_post.rb
index a6cc99a42..10a7bba96 100644
--- a/app/models/communication/website/page/communication_post.rb
+++ b/app/models/communication/website/page/communication_post.rb
@@ -41,11 +41,11 @@
 #
 class Communication::Website::Page::CommunicationPost < Communication::Website::Page
 
-  def git_path(website)
-    "#{git_path_content_prefix(website)}posts/_index.html"
+  def current_git_path
+    "#{git_path_prefix}posts/_index.html"
   end
 
-  def git_dependencies
+  def git_dependencies(website)
     [
       website.config_default_permalinks,
       website.categories,
diff --git a/app/models/communication/website/page/education_diploma.rb b/app/models/communication/website/page/education_diploma.rb
index d63a207ae..5c05e798f 100644
--- a/app/models/communication/website/page/education_diploma.rb
+++ b/app/models/communication/website/page/education_diploma.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::EducationDiploma < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}diplomas/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/education_program.rb b/app/models/communication/website/page/education_program.rb
index b84bda7a4..298a0a325 100644
--- a/app/models/communication/website/page/education_program.rb
+++ b/app/models/communication/website/page/education_program.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::EducationProgram < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}programs/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/home.rb b/app/models/communication/website/page/home.rb
index 4a850e991..a5b816619 100644
--- a/app/models/communication/website/page/home.rb
+++ b/app/models/communication/website/page/home.rb
@@ -50,4 +50,8 @@ class Communication::Website::Page::Home < Communication::Website::Page
   def set_slug
     self.slug = ''
   end
+
+  def validate_slug
+    true
+  end
 end
diff --git a/app/models/communication/website/page/organization.rb b/app/models/communication/website/page/organization.rb
index ff0017ce9..b67936691 100644
--- a/app/models/communication/website/page/organization.rb
+++ b/app/models/communication/website/page/organization.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Organization < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}organizations/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/person.rb b/app/models/communication/website/page/person.rb
index 1a084c359..2f3baf146 100644
--- a/app/models/communication/website/page/person.rb
+++ b/app/models/communication/website/page/person.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Person < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}persons/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/research_paper.rb b/app/models/communication/website/page/research_paper.rb
index e03da13f5..8692c5e79 100644
--- a/app/models/communication/website/page/research_paper.rb
+++ b/app/models/communication/website/page/research_paper.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::ResearchPaper < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}papers/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/research_volume.rb b/app/models/communication/website/page/research_volume.rb
index d758ea6ee..06113453e 100644
--- a/app/models/communication/website/page/research_volume.rb
+++ b/app/models/communication/website/page/research_volume.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::ResearchVolume < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}volumes/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/researcher.rb b/app/models/communication/website/page/researcher.rb
index 2133aaa4d..184ffc29f 100644
--- a/app/models/communication/website/page/researcher.rb
+++ b/app/models/communication/website/page/researcher.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Researcher < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}researchers/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/teacher.rb b/app/models/communication/website/page/teacher.rb
index 148eaa1b0..5461b31bc 100644
--- a/app/models/communication/website/page/teacher.rb
+++ b/app/models/communication/website/page/teacher.rb
@@ -41,4 +41,8 @@
 #
 class Communication::Website::Page::Teacher < Communication::Website::Page
 
+  def current_git_path
+    "#{git_path_prefix}teachers/_index.html"
+  end
+
 end
diff --git a/app/models/communication/website/page/with_path.rb b/app/models/communication/website/page/with_path.rb
index 15692369e..7c4bab76c 100644
--- a/app/models/communication/website/page/with_path.rb
+++ b/app/models/communication/website/page/with_path.rb
@@ -2,18 +2,8 @@ module Communication::Website::Page::WithPath
   extend ActiveSupport::Concern
 
   included do
-    validates :slug,
-              presence: true,
-              unless: :kind_home?
-    validate  :slug_must_be_unique
-    validates :slug,
-              format: {
-                with: /\A[a-z0-9\-]+\z/,
-                message: I18n.t('slug_error')
-              },
-              unless: :kind_home?
-
     before_validation :set_slug
+    validate :validate_slug
   end
 
   def path
@@ -31,30 +21,26 @@ module Communication::Website::Page::WithPath
   end
 
   def git_path(website)
-    # Same website and page published
+    # Same website only, page published only
+    # FIXME is it ever called for other websites?
     return unless website.id == communication_website_id && published
-
-    path = git_path_content_prefix(website)
-    if kind_home?
-      path += "_index.html"
-    elsif has_special_git_path?
-      path += "#{kind.split('_').last}/_index.html"
-    else
-    end
-    
-    "#{git_path_prefix}pages/#{slug_with_ancestors}/_index.html"
+    current_git_path
   end
 
   def url
     return unless published
     return if website.url.blank?
-    "#{website.url}#{path}"
+    "#{website.url}#{path}".gsub('//', '/')
   end
 
   protected
 
+  def current_git_path
+    @current_git_path ||= "#{git_path_prefix}pages/#{slug_with_ancestors}/_index.html"
+  end
+
   def git_path_prefix
-    git_path_content_prefix(website)
+    @git_path_prefix ||= git_path_content_prefix(website)
   end
 
   def set_slug
@@ -74,8 +60,28 @@ module Communication::Website::Page::WithPath
               .exists?
   end
 
+  def validate_slug
+    slug_must_be_present
+    slug_must_be_unique
+    slug_must_have_proper_format
+  end
+
+  def slug_must_be_present
+    errors.add(:slug, ActiveRecord::Errors.default_error_messages[:absent]) if slug.blank?
+  end
+
   def slug_must_be_unique
     errors.add(:slug, ActiveRecord::Errors.default_error_messages[:taken]) if slug_unavailable?(slug)
   end
 
+  def slug_must_have_proper_format
+    # TODO method equivalent of:
+    # validates :slug,
+    #           format: {
+    #             with: /\A[a-z0-9\-]+\z/,
+    #             message: I18n.t('slug_error')
+    #           },
+    #           unless: :kind_home?
+  end
+
 end
-- 
GitLab