diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index c97ea3543..94ef79717 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -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 :antecedent_is_not_aliased validate :consequent_is_not_aliased module DescendantMethods @@ -126,6 +127,14 @@ class TagImplication < ActiveRecord::Base 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 # We don't want to implicate a -> b if b is already aliased to c if TagAlias.exists?(["antecedent_name = ?", consequent_name]) diff --git a/db/migrate/20131228230219_update_aliased_implication_antecedents.rb b/db/migrate/20131228230219_update_aliased_implication_antecedents.rb new file mode 100644 index 000000000..321bda540 --- /dev/null +++ b/db/migrate/20131228230219_update_aliased_implication_antecedents.rb @@ -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