This commit is contained in:
Toks
2013-10-06 16:56:39 -04:00
parent 5005614e97
commit c5b5ca9825
7 changed files with 76 additions and 10 deletions

View File

@@ -72,6 +72,7 @@ class TagAlias < ActiveRecord::Base
def process!
update_column(:status, "processing")
move_aliases_and_implications
clear_all_cache
ensure_category_consistency
update_posts
@@ -107,13 +108,28 @@ class TagAlias < ActiveRecord::Base
end
def absence_of_transitive_relation
# We don't want a -> b && b -> c chains
if self.class.exists?(["antecedent_name = ?", consequent_name]) || self.class.exists?(["consequent_name = ?", antecedent_name])
# We don't want a -> b && b -> c chains if the b -> c alias was created first.
# If the a -> b alias was created first, the new one will be allowed and the old one will be moved automatically instead.
if self.class.exists?(["antecedent_name = ?", consequent_name])
self.errors[:base] << "Tag alias can not create a transitive relation with another tag alias"
false
end
end
def move_aliases_and_implications
aliases = TagAlias.where(["consequent_name = ?", antecedent_name])
aliases.each do |ta|
ta.consequent_name = self.consequent_name
ta.save
end
implications = TagImplication.where(["consequent_name = ?", antecedent_name])
implications.each do |ti|
ti.consequent_name = self.consequent_name
ti.save
end
end
def ensure_tags_exist
Tag.find_or_create_by_name(antecedent_name)
Tag.find_or_create_by_name(consequent_name)

View File

@@ -8,6 +8,7 @@ class TagImplication < ActiveRecord::Base
validates_presence_of :creator_id, :antecedent_name, :consequent_name
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
validate :absence_of_circular_relation
validate :consequent_is_not_aliased
module DescendantMethods
extend ActiveSupport::Concern
@@ -125,6 +126,14 @@ class TagImplication < ActiveRecord::Base
end
end
def consequent_is_not_aliased
# We don't want to implicate a -> b if b is already aliased to c
if TagAlias.exists?(["antecedent_name = ?", consequent_name])
self.errors[:base] << "Consequent tag must not be aliased to another tag"
false
end
end
def update_posts
Post.without_timeout do
Post.raw_tag_match(antecedent_name).find_each do |post|