From 825359c7eea057f89cf86ef0292c238a6f1e5013 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 27 Mar 2023 07:40:40 +0200
Subject: [PATCH] permalink clean

---
 app/models/communication/website/permalink.rb          |  1 +
 app/models/concerns/with_permalink.rb                  |  4 +++-
 app/views/admin/application/static/_permalink.html.erb |  2 +-
 .../admin/application/static/_permalink.json.jbuilder  | 10 ++++++++++
 .../communication/websites/pages/static.json.jbuilder  |  5 +++++
 5 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 app/views/admin/application/static/_permalink.json.jbuilder
 create mode 100644 app/views/admin/communication/websites/pages/static.json.jbuilder

diff --git a/app/models/communication/website/permalink.rb b/app/models/communication/website/permalink.rb
index 5329fcc0c..1225fdf11 100644
--- a/app/models/communication/website/permalink.rb
+++ b/app/models/communication/website/permalink.rb
@@ -54,6 +54,7 @@ class Communication::Website::Permalink < ApplicationRecord
   scope :for_website, -> (website) { where(website_id: website.id) }
   scope :current, -> { where(is_current: true) }
   scope :not_current, -> { where(is_current: false) }
+  scope :not_root, -> { where.not(path: '/') }
 
   def self.config_in_website(website, language)
     required_kinds_in_website(website).map { |permalink_class|
diff --git a/app/models/concerns/with_permalink.rb b/app/models/concerns/with_permalink.rb
index 8b26356ff..1246b2464 100644
--- a/app/models/concerns/with_permalink.rb
+++ b/app/models/concerns/with_permalink.rb
@@ -9,7 +9,9 @@ module WithPermalink
   end
 
   def previous_permalinks_in_website(website)
-    permalinks.for_website(website).not_current
+    permalinks.for_website(website)
+              .not_current
+              .not_root
   end
 
   # Persisted in db or nil
diff --git a/app/views/admin/application/static/_permalink.html.erb b/app/views/admin/application/static/_permalink.html.erb
index 99409ca32..6e8fc9525 100644
--- a/app/views/admin/application/static/_permalink.html.erb
+++ b/app/views/admin/application/static/_permalink.html.erb
@@ -4,7 +4,7 @@ url: "<%= @about.current_permalink_in_website(@website)&.path %>"
 slug: "<%= forced_slug || @about.slug %>"
 <% end %>
 <%
-previous_permalinks = @about.previous_permalinks_in_website(@website).where.not(path: "/")
+previous_permalinks = @about.previous_permalinks_in_website(@website)
 if previous_permalinks.any?
 %>
 aliases:
diff --git a/app/views/admin/application/static/_permalink.json.jbuilder b/app/views/admin/application/static/_permalink.json.jbuilder
new file mode 100644
index 000000000..bae4fabb2
--- /dev/null
+++ b/app/views/admin/application/static/_permalink.json.jbuilder
@@ -0,0 +1,10 @@
+json.url @about.current_permalink_in_website(@website)&.path
+
+if @about.respond_to?(:slug) && @about.slug.present?
+  forced_slug ||= nil
+  json.slug (forced_slug || @about.slug)
+end
+
+previous_permalinks = @about.previous_permalinks_in_website(@website)
+json.aliases previous_permalinks.collect(&:path) if previous_permalinks.any?
+  
\ No newline at end of file
diff --git a/app/views/admin/communication/websites/pages/static.json.jbuilder b/app/views/admin/communication/websites/pages/static.json.jbuilder
new file mode 100644
index 000000000..65a5477b8
--- /dev/null
+++ b/app/views/admin/communication/websites/pages/static.json.jbuilder
@@ -0,0 +1,5 @@
+json.extract! @page,
+              :title,
+              :breadcrumb_title
+
+json.partial! 'admin/application/static/permalink'
\ No newline at end of file
-- 
GitLab