Fix implication antecedents with aliases
This commit is contained in:
@@ -8,6 +8,7 @@ class TagImplication < ActiveRecord::Base
|
|||||||
validates_presence_of :creator_id, :antecedent_name, :consequent_name
|
validates_presence_of :creator_id, :antecedent_name, :consequent_name
|
||||||
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
|
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
|
||||||
validate :absence_of_circular_relation
|
validate :absence_of_circular_relation
|
||||||
|
validate :antecedent_is_not_aliased
|
||||||
validate :consequent_is_not_aliased
|
validate :consequent_is_not_aliased
|
||||||
|
|
||||||
module DescendantMethods
|
module DescendantMethods
|
||||||
@@ -126,6 +127,14 @@ class TagImplication < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def antecedent_is_not_aliased
|
||||||
|
# We don't want to implicate a -> b if a is already aliased to c
|
||||||
|
if TagAlias.exists?(["antecedent_name = ?", antecedent_name])
|
||||||
|
self.errors[:base] << "Antecedent tag must not be aliased to another tag"
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def consequent_is_not_aliased
|
def consequent_is_not_aliased
|
||||||
# We don't want to implicate a -> b if b is already aliased to c
|
# We don't want to implicate a -> b if b is already aliased to c
|
||||||
if TagAlias.exists?(["antecedent_name = ?", consequent_name])
|
if TagAlias.exists?(["antecedent_name = ?", consequent_name])
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
class UpdateAliasedImplicationAntecedents < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
execute "set statement_timeout = 0"
|
||||||
|
TagImplication.find_each do |ti|
|
||||||
|
ta = TagAlias.where("antecedent_name = ? AND status != ?", ti.antecedent_name, "pending").first
|
||||||
|
if ta
|
||||||
|
if ta.consequent_name == ti.consequent_name
|
||||||
|
ti.destroy
|
||||||
|
elsif TagImplication.where("antecedent_name = ? AND consequent_name = ?", ta.consequent_name, ti.consequent_name).exists?
|
||||||
|
ti.destroy
|
||||||
|
else
|
||||||
|
ti.antecedent_name = ta.consequent_name
|
||||||
|
ti.save
|
||||||
|
end
|
||||||
|
ta.update_posts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user