fixes #749, fixes tag alias related regressions

This commit is contained in:
albert
2013-03-05 22:51:21 -05:00
parent 80f63cc587
commit e53d71b31b
5 changed files with 64 additions and 45 deletions

View File

@@ -51,9 +51,8 @@ class TagAliasCorrection
def fix! def fix!
clear_cache clear_cache
Post.raw_tag_match(tag_alias.antecedent_name).each do |post| tag_alias.update_cache
post.save tag_alias.update_posts
end
tag_alias.antecedent_tag.fix_post_count if tag_alias.antecedent_tag tag_alias.antecedent_tag.fix_post_count if tag_alias.antecedent_tag
tag_alias.consequent_tag.fix_post_count if tag_alias.consequent_tag tag_alias.consequent_tag.fix_post_count if tag_alias.consequent_tag
end end

View File

@@ -34,6 +34,8 @@ class Tag < ActiveRecord::Base
end end
module CountMethods module CountMethods
extend ActiveSupport::Concern
module ClassMethods module ClassMethods
def counts_for(tag_names) def counts_for(tag_names)
select_all_sql("SELECT name, post_count FROM tags WHERE name IN (?)", tag_names) select_all_sql("SELECT name, post_count FROM tags WHERE name IN (?)", tag_names)
@@ -102,7 +104,6 @@ class Tag < ActiveRecord::Base
end end
module NameMethods module NameMethods
module ClassMethods
def normalize_name(name) def normalize_name(name)
name.downcase.tr(" ", "_").gsub(/\A[-~]+/, "").gsub(/\*/, "") name.downcase.tr(" ", "_").gsub(/\A[-~]+/, "").gsub(/\*/, "")
end end
@@ -139,11 +140,6 @@ class Tag < ActiveRecord::Base
end end
end end
def self.included(m)
m.extend(ClassMethods)
end
end
module ParseMethods module ParseMethods
def normalize(query) def normalize(query)
query.to_s.downcase.strip query.to_s.downcase.strip
@@ -468,7 +464,7 @@ class Tag < ActiveRecord::Base
extend ViewCountMethods extend ViewCountMethods
include CategoryMethods include CategoryMethods
extend StatisticsMethods extend StatisticsMethods
include NameMethods extend NameMethods
extend ParseMethods extend ParseMethods
include RelationMethods include RelationMethods
extend SuggestionMethods extend SuggestionMethods

View File

@@ -1,6 +1,7 @@
class TagAlias < ActiveRecord::Base class TagAlias < ActiveRecord::Base
after_save :clear_all_cache after_save :clear_all_cache
after_save :update_cache after_save :update_cache
after_save :ensure_category_consistency
after_destroy :clear_all_cache after_destroy :clear_all_cache
before_validation :initialize_creator, :on => :create before_validation :initialize_creator, :on => :create
validates_presence_of :creator_id validates_presence_of :creator_id
@@ -86,6 +87,14 @@ class TagAlias < ActiveRecord::Base
end end
end end
def ensure_category_consistency
if antecedent_tag && consequent_tag && antecedent_tag.category != consequent_tag.category
consequent_tag.update_attribute(:category, antecedent_tag.category)
end
true
end
def clear_all_cache def clear_all_cache
Danbooru.config.all_server_hosts.each do |host| Danbooru.config.all_server_hosts.each do |host|
delay(:queue => host).clear_cache(host) delay(:queue => host).clear_cache(host)
@@ -103,8 +112,14 @@ class TagAlias < ActiveRecord::Base
end end
def update_posts def update_posts
Post.raw_tag_match(antecedent_name).each do |post| Post.raw_tag_match(antecedent_name).find_each do |post|
post.save escaped_antecedent_name = Regexp.escape(antecedent_name)
fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{consequent_name} ").strip
CurrentUser.scoped(creator, creator_ip_addr) do
post.update_attributes(
:tag_string => fixed_tags
)
end
end end
end end
end end

View File

@@ -13,6 +13,7 @@ class TagAliasTest < ActiveSupport::TestCase
end end
teardown do teardown do
MEMCACHE.flush_all
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end
@@ -28,7 +29,7 @@ class TagAliasTest < ActiveSupport::TestCase
assert_equal("zzz", @correction.statistics_hash["antecedent_cache"]) assert_equal("zzz", @correction.statistics_hash["antecedent_cache"])
assert_nil(@correction.statistics_hash["consequent_cache"]) assert_nil(@correction.statistics_hash["consequent_cache"])
assert_equal(-3, @correction.statistics_hash["antecedent_count"]) assert_equal(-3, @correction.statistics_hash["antecedent_count"])
assert_nil(@correction.statistics_hash["consequent_count"]) assert_equal(1, @correction.statistics_hash["consequent_count"])
end end
should "render to json" do should "render to json" do

View File

@@ -73,5 +73,13 @@ class TagAliasTest < ActiveSupport::TestCase
assert_not_equal(tag_alias.creator_id, post.uploader_id) assert_not_equal(tag_alias.creator_id, post.uploader_id)
assert_equal(tag_alias.creator_id, post.versions.last.updater_id) assert_equal(tag_alias.creator_id, post.versions.last.updater_id)
end end
should "push the antecedent's category to the consequent" do
tag1 = FactoryGirl.create(:tag, :name => "aaa", :category => 1)
tag2 = FactoryGirl.create(:tag, :name => "bbb")
ta = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
tag2.reload
assert_equal(1, tag2.category)
end
end end
end end