change how tag counts are recalculated after a tag category changes
This commit is contained in:
@@ -23,6 +23,7 @@ class TagsController < ApplicationController
|
|||||||
def update
|
def update
|
||||||
@tag = Tag.find(params[:id])
|
@tag = Tag.find(params[:id])
|
||||||
@tag.update_attributes(params[:tag])
|
@tag.update_attributes(params[:tag])
|
||||||
|
@tag.update_category_cache_for_all
|
||||||
respond_with(@tag)
|
respond_with(@tag)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
class Tag < ActiveRecord::Base
|
class Tag < ActiveRecord::Base
|
||||||
METATAGS = "-user|user|-approver|approver|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv"
|
METATAGS = "-user|user|-approver|approver|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv"
|
||||||
attr_accessible :category
|
attr_accessible :category
|
||||||
after_save :update_category_cache_for_all
|
|
||||||
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
|
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
|
||||||
|
|
||||||
module ApiMethods
|
module ApiMethods
|
||||||
@@ -84,24 +83,23 @@ class Tag < ActiveRecord::Base
|
|||||||
Danbooru.config.reverse_tag_category_mapping[category]
|
Danbooru.config.reverse_tag_category_mapping[category]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_category_cache_for_all(force = false)
|
def update_category_cache_for_all
|
||||||
if category_changed? || force
|
Danbooru.config.all_server_hosts.each do |host|
|
||||||
update_category_cache
|
delay(:queue => host).update_category_cache
|
||||||
delay(:queue => "default").update_category_post_counts if category_changed?
|
|
||||||
|
|
||||||
Danbooru.config.other_server_hosts.each do |host|
|
|
||||||
delay(:queue => host).update_category_cache
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
delay(:queue => "default").update_category_post_counts
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_category_post_counts
|
def update_category_post_counts
|
||||||
old_field = "tag_count_#{Danbooru.config.reverse_tag_category_mapping[category_was]}".downcase
|
Post.raw_tag_match(name).find_each do |post|
|
||||||
new_field = "tag_count_#{category_name}".downcase
|
post.reload
|
||||||
if old_field != new_field
|
post.set_tag_counts
|
||||||
Post.without_timeout do
|
post.update_column(:tag_count, post.tag_count)
|
||||||
Post.raw_tag_match(name).update_all("#{old_field} = #{old_field} - 1, #{new_field} = #{new_field} + 1")
|
post.update_column(:tag_count_general, post.tag_count_general)
|
||||||
end
|
post.update_column(:tag_count_artist, post.tag_count_artist)
|
||||||
|
post.update_column(:tag_count_copyright, post.tag_count_copyright)
|
||||||
|
post.update_column(:tag_count_character, post.tag_count_character)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -138,7 +136,7 @@ class Tag < ActiveRecord::Base
|
|||||||
|
|
||||||
if category_id != tag.category
|
if category_id != tag.category
|
||||||
tag.update_column(:category, category_id)
|
tag.update_column(:category, category_id)
|
||||||
tag.update_category_cache_for_all(true)
|
tag.update_category_cache_for_all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
14
script/fixes/006.rb
Normal file
14
script/fixes/006.rb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
|
||||||
|
|
||||||
|
Post.where("created_at > '2013-02-01'").find_each do |post|
|
||||||
|
puts "Fixing #{post.id}"
|
||||||
|
post.reload
|
||||||
|
post.set_tag_counts
|
||||||
|
post.update_column(:tag_count, post.tag_count)
|
||||||
|
post.update_column(:tag_count_general, post.tag_count_general)
|
||||||
|
post.update_column(:tag_count_artist, post.tag_count_artist)
|
||||||
|
post.update_column(:tag_count_copyright, post.tag_count_copyright)
|
||||||
|
post.update_column(:tag_count_character, post.tag_count_character)
|
||||||
|
end
|
||||||
@@ -91,9 +91,11 @@ class TagTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "reset its category after updating" do
|
should "reset its category after updating" do
|
||||||
tag = FactoryGirl.create(:artist_tag)
|
tag = FactoryGirl.create(:artist_tag)
|
||||||
|
tag.update_category_cache_for_all
|
||||||
assert_equal(Tag.categories.artist, MEMCACHE.get("tc:#{tag.name}"))
|
assert_equal(Tag.categories.artist, MEMCACHE.get("tc:#{tag.name}"))
|
||||||
|
|
||||||
tag.update_attribute(:category, Tag.categories.copyright)
|
tag.update_attribute(:category, Tag.categories.copyright)
|
||||||
|
tag.update_category_cache_for_all
|
||||||
assert_equal(Tag.categories.copyright, MEMCACHE.get("tc:#{tag.name}"))
|
assert_equal(Tag.categories.copyright, MEMCACHE.get("tc:#{tag.name}"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user