tags: ensure aliased tag categories stay in sync.

* When a tag's category is changed, also change the category of any aliases pointing to it. For
  example, if "ff7" is aliased to "final_fantasy_vii", and "final_fantasy_vii" is changed to a
  copyright tag, then change the empty "ff7" tag to be a copyright tag too.

* Don't allow changing the category of an aliased tag. For example, if "ff7" is aliased to
  "final_fantasy_vii", then don't allow changing the "ff7" tag to be a non-copyright tag.

This ensures that the categories of aliased tags stay in sync with that of their parent tags. This
way aliased tags are colored correctly in wikis and other places.
This commit is contained in:
evazion
2022-11-22 21:54:22 -06:00
parent 1a9718250f
commit b234727832
4 changed files with 54 additions and 2 deletions

View File

@@ -23,8 +23,10 @@ class Tag < ApplicationRecord
validates :name, tag_name: true, uniqueness: true, on: :create
validates :name, tag_name: true, on: :name
validates :category, inclusion: { in: TagCategory.category_ids }
validate :validate_category, if: :category_changed?
before_create :create_character_tag_for_cosplay_tag, if: :is_cosplay_tag?
after_save :update_tag_alias_categories, if: :saved_change_to_category?
after_save :update_category_cache, if: :saved_change_to_category?
after_save :update_category_post_counts, if: :saved_change_to_category?
@@ -173,6 +175,21 @@ class Tag < ApplicationRecord
def update_category_cache
Cache.put("tc:#{Cache.hash(name)}", category, 3.hours)
end
# When a tag's category is changed, also change the categories of any aliases pointing to it.
def update_tag_alias_categories
consequent_aliases.each do |tag_alias|
if tag_alias.antecedent_tag.category != category
tag_alias.antecedent_tag.update!(category: category, updater: updater)
end
end
end
def validate_category
if is_aliased? && category != aliased_tag.category
errors.add(:base, "Can't change the category of an aliased tag")
end
end
end
concerning :NameMethods do