diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index b3cb9c4f2..bc0dde4f5 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -2,7 +2,7 @@ class TagAlias < TagRelationship validates_uniqueness_of :antecedent_name, scope: :status, conditions: -> { active } validate :absence_of_transitive_relation - before_create :delete_conflicting_alias + before_create :delete_conflicting_relationships def self.to_aliased(names) names = Array(names).map(&:to_s) @@ -37,8 +37,12 @@ class TagAlias < TagRelationship # Allow aliases to be reversed. If A -> B already exists, but we're trying to # create B -> A, then automatically delete A -> B so we can make B -> A. - def delete_conflicting_alias - tag_alias = TagAlias.active.find_by(antecedent_name: consequent_name, consequent_name: antecedent_name) - tag_alias.reject! if tag_alias.present? + # Also automatically remove implications that are being replaced by aliases + def delete_conflicting_relationships + tag_relationships = [] + tag_relationships << TagAlias.active.find_by(antecedent_name: consequent_name, consequent_name: antecedent_name) + tag_relationships << TagImplication.active.find_by(antecedent_name: antecedent_name, consequent_name: consequent_name) + tag_relationships << TagImplication.active.find_by(antecedent_name: consequent_name, consequent_name: antecedent_name) + tag_relationships.each { |rel| rel.reject! if rel.present? } end end diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 4b5a44bc8..c4d557fde 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -286,5 +286,12 @@ class TagAliasTest < ActiveSupport::TestCase assert_equal(4, tag1.reload.category) assert_equal(3, tag2.reload.category) end + + should "automatically remove a redundant implication" do + ti = create(:tag_implication, antecedent_name: "new_asuka", consequent_name: "asuka", status: "active") + create(:tag_alias, antecedent_name: "new_asuka", consequent_name: "asuka", status: "active") + + assert_equal("deleted", ti.reload.status) + end end end