From c1672fb38779e8a02e3805b6443c64d403522d39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 22 Nov 2021 14:18:26 +0100
Subject: [PATCH] slug validator

---
 app/models/communication/website/author.rb   | 2 ++
 app/models/communication/website/category.rb | 1 +
 app/models/communication/website/page.rb     | 1 +
 app/models/communication/website/post.rb     | 5 +++--
 app/models/concerns/with_slug.rb             | 2 --
 app/models/education/teacher.rb              | 2 ++
 6 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/app/models/communication/website/author.rb b/app/models/communication/website/author.rb
index a81414d3d..e316d74bf 100644
--- a/app/models/communication/website/author.rb
+++ b/app/models/communication/website/author.rb
@@ -39,6 +39,8 @@ class Communication::Website::Author < ApplicationRecord
            class_name: 'Communication::Website::Post',
            dependent: :nullify
 
+  validates :slug, uniqueness: { scope: :website_id }
+
   scope :ordered, -> { order(:last_name, :first_name) }
 
   def to_s
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 5d662a5c3..811c1d7e6 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -59,6 +59,7 @@ class Communication::Website::Category < ApplicationRecord
 
 
   validates :name, presence: true
+  validates :slug, uniqueness: { scope: :website_id }
 
   scope :ordered, -> { order(:position) }
 
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 5e7b1f45b..042b5bcf0 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -63,6 +63,7 @@ class Communication::Website::Page < ApplicationRecord
              dependent: :nullify
 
   validates :title, presence: true
+  validates :path, uniqueness: { scope: :website_id }
 
   before_validation :make_path
   after_save :update_children_paths if :saved_change_to_path?
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 51bbcec9b..5896b97cd 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -49,11 +49,12 @@ class Communication::Website::Post < ApplicationRecord
                           foreign_key: 'communication_website_post_id',
                           association_foreign_key: 'communication_website_category_id'
 
+  validates :title, presence: true
+  validates :slug, uniqueness: { scope: :website_id }
+
   scope :ordered, -> { order(published_at: :desc, created_at: :desc) }
   scope :recent, -> { order(published_at: :desc).limit(5) }
 
-  validates :title, presence: true
-
   def github_path_generated
     "_posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
   end
diff --git a/app/models/concerns/with_slug.rb b/app/models/concerns/with_slug.rb
index c13d07f01..793e8f090 100644
--- a/app/models/concerns/with_slug.rb
+++ b/app/models/concerns/with_slug.rb
@@ -2,8 +2,6 @@ module WithSlug
   extend ActiveSupport::Concern
 
   included do
-    validates :slug,
-              uniqueness: { scope: :university_id }
     validates :slug,
               format: { with: /\A[a-z0-9\-]+\z/, message: I18n.t('slug_error') }
   end
diff --git a/app/models/education/teacher.rb b/app/models/education/teacher.rb
index 970d4a478..33e0abb22 100644
--- a/app/models/education/teacher.rb
+++ b/app/models/education/teacher.rb
@@ -36,6 +36,8 @@ class Education::Teacher < ApplicationRecord
                           association_foreign_key: 'education_program_id'
   has_many :websites, -> { distinct }, through: :programs
 
+  validates :slug, uniqueness: { scope: :university_id }
+
   scope :ordered, -> { order(:last_name, :first_name) }
 
   def to_s
-- 
GitLab