diff --git a/app/models/page_data/page_tag.rb b/app/models/page_data/page_tag.rb index 79c2d9ff..604a362c 100644 --- a/app/models/page_data/page_tag.rb +++ b/app/models/page_data/page_tag.rb @@ -2,6 +2,8 @@ class PageTag < ApplicationRecord belongs_to :page, polymorphic: true belongs_to :user + after_update :update_slug_to_reflect_label_changes + # Delimiter to be used wherever we want to allow submitting multiple tags in a single string SUBMISSION_DELIMITER = ',,,|||,,,' @@ -12,4 +14,13 @@ class PageTag < ApplicationRecord def self.icon 'label' end + + def update_slug_to_reflect_label_changes + self.update_slug! if (saved_change_to_tag? && self.tag.present?) + end + + def update_slug! + new_slug = PageTagService.slug_for(self.tag) + self.update(slug: new_slug) unless new_slug == self.slug + end end