From 4e66057bca7184e3180fa022a4277dca7435adbb Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Fri, 12 Nov 2021 15:37:12 +0100
Subject: [PATCH] homepage slug

---
 app/models/communication/website/page.rb | 9 ++++++++-
 app/models/concerns/with_slug.rb         | 2 +-
 config/locales/communication/en.yml      | 6 ++++++
 config/locales/communication/fr.yml      | 6 ++++++
 4 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 215e1ea26..1afcade4a 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -57,8 +57,9 @@ class Communication::Website::Page < ApplicationRecord
              dependent: :nullify
 
   validates :title, presence: true
+  validate :homepage_is_published?
 
-  before_save :make_path
+  before_validation :make_path
   after_save :update_children_paths if :saved_change_to_path?
 
   scope :ordered, -> { order(:position) }
@@ -93,4 +94,10 @@ class Communication::Website::Page < ApplicationRecord
   def update_children_paths
     children.each(&:save)
   end
+
+  def homepage_is_published?
+     if path == '/' and !published
+        errors.add(:published, :home_not_published)
+     end
+  end
 end
diff --git a/app/models/concerns/with_slug.rb b/app/models/concerns/with_slug.rb
index cf2785e4c..96fe49fdb 100644
--- a/app/models/concerns/with_slug.rb
+++ b/app/models/concerns/with_slug.rb
@@ -2,7 +2,7 @@ module WithSlug
   extend ActiveSupport::Concern
 
   included do
-    before_validation :generate_slug, if: Proc.new { |o| o.slug.blank? }
+    before_validation :generate_slug, if: Proc.new { |o| o.slug.nil? }
   end
 
   protected
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 1b28d20a7..84201c897 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -106,6 +106,12 @@ en:
         text: Text
         title: Title
         website: Website
+    errors:
+      models:
+        communication/website/page:
+          attributes:
+            published:
+              home_not_published: must be checked. Homepage has to be published.
   enums:
     communication/website/menu/item:
       kind:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 8a117d0ff..36736beac 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -106,6 +106,12 @@ fr:
         text: Texte
         title: Titre
         website: Site Web
+    errors:
+      models:
+        communication/website/page:
+          attributes:
+            published:
+              home_not_published: doit être coché. La page d'accueil doit être publiée.
   enums:
     communication/website/menu/item:
       kind:
-- 
GitLab