From d54c9aa1e2c2099880123391825bd08bc929f7f1 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 7 Apr 2022 17:30:53 +0200
Subject: [PATCH] regeneration pages

---
 app/models/communication/website/page.rb      | 14 +++++-----
 .../communication/website/page/with_kind.rb   |  2 +-
 app/models/communication/website/post.rb      |  2 +-
 .../website/with_dependencies.rb              | 16 ++++++++++++
 .../website/with_special_pages.rb             | 26 ++++++++++++-------
 app/models/concerns/with_git.rb               |  1 +
 .../configs/permalinks/static.html.erb        | 14 +++++-----
 .../website/pages/_form.html.erb              |  4 +--
 .../website/pages/_treebranch.html.erb        |  2 +-
 .../communication/website/pages/show.html.erb |  2 +-
 .../admin/education/programs/static.html.erb  |  2 +-
 11 files changed, 55 insertions(+), 30 deletions(-)

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 2d3d66e79..8cdeac7a8 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -89,12 +89,14 @@ class Communication::Website::Page < ApplicationRecord
   end
 
   def git_dependencies(website)
-    [self] +
-    website.menus +
-    descendents +
-    active_storage_blobs +
-    siblings +
-    git_block_dependencies
+    dependencies = [self] +
+                    website.menus +
+                    descendents +
+                    active_storage_blobs +
+                    siblings +
+                    git_block_dependencies
+    dependencies += website.education_programs if kind_education_programs?
+    dependencies
   end
 
   def git_destroy_dependencies(website)
diff --git a/app/models/communication/website/page/with_kind.rb b/app/models/communication/website/page/with_kind.rb
index f464cf6a4..022acc8ba 100644
--- a/app/models/communication/website/page/with_kind.rb
+++ b/app/models/communication/website/page/with_kind.rb
@@ -25,7 +25,7 @@ module Communication::Website::Page::WithKind
       kind != nil
     end
 
-    def is_not_special_page?
+    def is_regular_page?
       !is_special_page?
     end
 
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 72910c15f..ec9e66e7b 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -94,7 +94,7 @@ class Communication::Website::Post < ApplicationRecord
   def url
     return unless published
     return if website.url.blank?
-    "#{website.url}#{website.index_for(:communication_posts).path}#{path}"
+    "#{website.url}#{website.special_page(:communication_posts).path}#{path}"
   end
 
   def to_s
diff --git a/app/models/communication/website/with_dependencies.rb b/app/models/communication/website/with_dependencies.rb
index 2d7f31e6b..66953a033 100644
--- a/app/models/communication/website/with_dependencies.rb
+++ b/app/models/communication/website/with_dependencies.rb
@@ -76,6 +76,22 @@ module Communication::Website::WithDependencies
   end
 
   # those tests has_xxx? should match the special page kind
+  def has_home?
+    true
+  end
+
+  def has_legal_terms?
+    true
+  end
+
+  def has_sitemap?
+    true
+  end
+
+  def has_privacy_policy?
+    true
+  end
+
   def has_communication_posts?
     posts.published.any?
   end
diff --git a/app/models/communication/website/with_special_pages.rb b/app/models/communication/website/with_special_pages.rb
index e0dbb6021..3204537e5 100644
--- a/app/models/communication/website/with_special_pages.rb
+++ b/app/models/communication/website/with_special_pages.rb
@@ -3,14 +3,17 @@ module Communication::Website::WithSpecialPages
 
   included do
 
+    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')
-      # first level generic pages
-      ['legal_terms', 'sitemap', 'privacy_policy'].each do |kind|
-        create_special_page(kind, homepage.id)
-      end
       # first level pages with test
-      ['communication_posts', 'education_programs', 'research_articles', 'research_volumes'].each do |kind|
+      ['legal_terms', 'sitemap', 'privacy_policy', 'communication_posts', 'education_programs', 'research_articles', 'research_volumes'].each do |kind|
         create_special_page(kind, homepage.id) if public_send("has_#{kind}?")
       end
       # team pages
@@ -21,13 +24,11 @@ module Communication::Website::WithSpecialPages
         end
       end
     end
-    # handle_async
 
     def manage_special_pages_publication
-      current_special_pages_keys = pages.where.not(kind: nil).pluck(:kind).uniq
-      current_special_pages_keys.each do |kind|
-        state = !(respond_to?("has_#{kind}?") && !public_send("has_#{kind}?"))
-        pages.where(kind: kind).update_all(published: state)
+      special_pages_keys.each do |kind|
+        published = public_send("has_#{kind}?")
+        special_page(kind).update(published: published)
       end
     end
 
@@ -37,6 +38,7 @@ module Communication::Website::WithSpecialPages
 
   def create_special_page(kind, parent_id = nil)
     i18n_key = "communication.website.pages.defaults.#{kind}"
+    # TODO: tmp should retrieve index_page (warning persons -> people) ,+ images/text
     pages.where(kind: kind).first_or_create(
       title: I18n.t("#{i18n_key}.title"),
       slug: I18n.t("#{i18n_key}.slug"),
@@ -47,5 +49,9 @@ module Communication::Website::WithSpecialPages
     )
   end
 
+  def special_pages_keys
+    @special_pages_keys ||= pages.where.not(kind: nil).pluck(:kind).uniq
+  end
+
 
 end
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index 4d8fbe8f3..99d3b266b 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -37,6 +37,7 @@ module WithGit
 
   def sync_with_git
     websites_for_self.each do |website|
+      website.touch
       next unless website.git_repository.valid?
       dependencies = git_dependencies(website).to_a.flatten.uniq.compact
       dependencies.each do |object|
diff --git a/app/views/admin/communication/website/configs/permalinks/static.html.erb b/app/views/admin/communication/website/configs/permalinks/static.html.erb
index bd2143afd..693685c4f 100644
--- a/app/views/admin/communication/website/configs/permalinks/static.html.erb
+++ b/app/views/admin/communication/website/configs/permalinks/static.html.erb
@@ -1,22 +1,22 @@
 <% if @website.has_communication_posts? %>
-posts:          /<%= @website.index_for(:communication_posts).path %>/:year/:month/:day/:slug/
+posts:          <%= @website.special_page(:communication_posts).path %>/:year/:month/:day/:slug/
 <% end %>
 <% if @website.has_communication_categories? %>
-categories:     /<%= @website.index_for(:communication_posts).path %>/:slug/
+categories:     <%= @website.special_page(:communication_posts).path %>/:slug/
 <% end %>
 <% if @website.has_people? %>
-persons:        /<%= @website.index_for(:persons).path %>/:slug/
+persons:        <%= @website.special_page(:people).path %>/:slug/
 <% end %>
 <% if @website.has_authors? %>
-authors:        /<%= @website.index_for(:persons).path %>/:slug/<%= @website.index_for(:communication_posts).path %>/
+authors:        /<%= @website.special_page(:people).path %>/:slug/<%= @website.special_page(:communication_posts).slug %>/
 <% end %>
 <%# ces paths complémentaires sont nécessairesà Hugo mais on ne les utilise pas %>
 <% if @website.has_administrators? %>
-administrators: /<%= @website.index_for(:persons).path %>/:slug/roles/
+administrators: /<%= @website.special_page(:people).path %>/:slug/roles/
 <% end %>
 <% if @website.has_teachers? %>
-teachers:       /<%= @website.index_for(:persons).path %>/:slug/programs/
+teachers:       /<%= @website.special_page(:people).path %>/:slug/programs/
 <% end %>
 <% if @website.has_researchers? %>
-researchers:    /<%= @website.index_for(:persons).path %>/:slug/articles/
+researchers:    /<%= @website.special_page(:people).path %>/:slug/articles/
 <% end %>
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index dd6c9e88e..7846ec1ef 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -18,7 +18,7 @@
           <%= f.association :related_category,
                             collection: collection_tree(@website.categories),
                             label_method: ->(p) { sanitize p[:label] },
-                            value_method: ->(p) { p[:id] } if @website.categories.any? && page.is_not_special_page? %>
+                            value_method: ->(p) { p[:id] } if @website.categories.any? && page.is_regular_page? %>
         </div>
       </div>
     </div>
@@ -36,7 +36,7 @@
                           class: 'js-slug-input',
                           data: { source: '#communication_website_page_title' }
                         } unless page.kind_home? %>
-            <%= f.input :published if page.is_not_special_page? %>
+            <%= f.input :published if page.is_regular_page? %>
             <%= f.association :parent,
                               collection: collection_tree(@website.pages, page),
                               include_blank: false,
diff --git a/app/views/admin/communication/website/pages/_treebranch.html.erb b/app/views/admin/communication/website/pages/_treebranch.html.erb
index 73e7ea759..2958456fd 100644
--- a/app/views/admin/communication/website/pages/_treebranch.html.erb
+++ b/app/views/admin/communication/website/pages/_treebranch.html.erb
@@ -25,7 +25,7 @@
       <% end %>
       <div class="btn-group ms-auto" role="group">
         <%= edit_link page %>
-        <%= destroy_link page, confirm_message: page.children.any? ? t('please_confirm_with_children') : t('please_confirm') if page.is_not_special_page? %>
+        <%= destroy_link page, confirm_message: page.children.any? ? t('please_confirm_with_children') : t('please_confirm') if page.is_regular_page? %>
       </div>
     </div>
     <ul class="list-unstyled treeview__children js-treeview-children <%= 'js-treeview-sortable-container' if can?(:reorder, page) %> ms-4" data-id="<%= page.id %>">
diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb
index 27fe594df..ad538af64 100644
--- a/app/views/admin/communication/website/pages/show.html.erb
+++ b/app/views/admin/communication/website/pages/show.html.erb
@@ -90,7 +90,7 @@
 <% end %>
 
 <% content_for :action_bar_left do %>
-  <%= destroy_link @page if @page.is_not_special_page? %>
+  <%= destroy_link @page if @page.is_regular_page? %>
   <%= link_to t('static'),
               static_admin_communication_website_page_path(@page),
               class: button_classes('btn-light') if current_user.server_admin? %>
diff --git a/app/views/admin/education/programs/static.html.erb b/app/views/admin/education/programs/static.html.erb
index b07fa6c36..f3375da17 100644
--- a/app/views/admin/education/programs/static.html.erb
+++ b/app/views/admin/education/programs/static.html.erb
@@ -3,7 +3,7 @@
 <% administrator_involvements = @about.involvements_through_roles.includes(:person).ordered_by_name %>
 title: >
   <%= @about.name %>
-url: /<%= @website.index_for(:education_programs).path %><%= @about.path %>/
+url: <%= @website.special_page(:education_programs).path %><%= @about.path %>/
 description: >
   <%= prepare_text_for_static @about.description %>
 position: <%= @about.position %>
-- 
GitLab