tags: update category cache whenever category changes.
Do `update_category_cache_for_all` in a callback instead of calling it manually everywhere.
This commit is contained in:
@@ -36,7 +36,6 @@ class TagsController < ApplicationController
|
||||
@tag = Tag.find(params[:id])
|
||||
check_privilege(@tag)
|
||||
@tag.update_attributes(params[:tag], :as => CurrentUser.role)
|
||||
@tag.update_category_cache_for_all
|
||||
respond_with(@tag)
|
||||
end
|
||||
|
||||
|
||||
@@ -117,7 +117,6 @@ private
|
||||
tag = Tag.find_by_name(token[1])
|
||||
tag.category = Tag.categories.value_for(token[2])
|
||||
tag.save
|
||||
tag.update_category_cache_for_all
|
||||
|
||||
else
|
||||
raise "Unknown token: #{token[0]}"
|
||||
|
||||
@@ -15,6 +15,8 @@ class Tag < ApplicationRecord
|
||||
validates :name, uniqueness: true, tag_name: true, on: :create
|
||||
validates_inclusion_of :category, in: TagCategory.category_ids
|
||||
|
||||
after_save :update_category_cache_for_all, if: :category_changed?
|
||||
|
||||
module ApiMethods
|
||||
def to_legacy_json
|
||||
return {
|
||||
@@ -221,8 +223,7 @@ class Tag < ApplicationRecord
|
||||
tag.update_category_cache
|
||||
|
||||
if category_id != tag.category && !tag.is_locked? && ((CurrentUser.is_builder? && tag.post_count < 10_000) || tag.post_count <= 50)
|
||||
tag.update_column(:category, category_id)
|
||||
tag.update_category_cache_for_all
|
||||
tag.update_attribute(:category, category_id)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -173,7 +173,6 @@ class TagAlias < TagRelationship
|
||||
def ensure_category_consistency
|
||||
if antecedent_tag.category != consequent_tag.category && antecedent_tag.category != Tag.categories.general
|
||||
consequent_tag.update_attribute(:category, antecedent_tag.category)
|
||||
consequent_tag.update_category_cache_for_all
|
||||
end
|
||||
|
||||
true
|
||||
|
||||
@@ -112,11 +112,9 @@ class TagTest < ActiveSupport::TestCase
|
||||
|
||||
should "reset its category after updating" do
|
||||
tag = FactoryGirl.create(:artist_tag)
|
||||
tag.update_category_cache_for_all
|
||||
assert_equal(Tag.categories.artist, Cache.get("tc:#{Cache.hash(tag.name)}"))
|
||||
|
||||
tag.update_attribute(:category, Tag.categories.copyright)
|
||||
tag.update_category_cache_for_all
|
||||
assert_equal(Tag.categories.copyright, Cache.get("tc:#{Cache.hash(tag.name)}"))
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user