Skip to content
Snippets Groups Projects
Unverified Commit 22a0956e authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

regenerate slug if needed

parent d909c795
No related branches found
No related tags found
No related merge requests found
......@@ -59,4 +59,9 @@ class Communication::Website::Author < ApplicationRecord
)
end
protected
def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists?
end
end
......@@ -101,4 +101,7 @@ class Communication::Website::Category < ApplicationRecord
end
end
def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists?
end
end
......@@ -65,7 +65,7 @@ class Communication::Website::Page < ApplicationRecord
validates :title, presence: true
validates :slug, uniqueness: { scope: :communication_website_id }
before_validation :regenerate_slug, :make_path
before_validation :make_path
after_save :update_children_paths if :saved_change_to_path?
scope :ordered, -> { order(:position) }
......@@ -93,13 +93,8 @@ class Communication::Website::Page < ApplicationRecord
protected
def regenerate_slug
current_slug = self.slug
n = 0
while self.class.unscoped.where(communication_website_id: communication_website_id, slug: self.slug).where.not(id: id).exists?
n += 1
self.slug = [current_slug, n].join('-')
end
def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists?
end
def make_path
......
......@@ -70,4 +70,10 @@ class Communication::Website::Post < ApplicationRecord
def to_s
"#{title}"
end
protected
def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists?
end
end
......@@ -6,5 +6,22 @@ module WithSlug
uniqueness: { scope: :university_id }
validates :slug,
format: { with: /\A[a-z0-9\-]+\z/, message: I18n.t('slug_error') }
before_validation :regenerate_slug
def regenerate_slug
current_slug = self.slug
n = 0
while slug_unavailable?(self.slug)
n += 1
self.slug = [current_slug, n].join('-')
end
end
protected
def slug_unavailable?(slug)
self.class.unscoped.where(university_id: self.university_id, slug: slug).where.not(id: self.id).exists?
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment