Tag Aliases: automatically remove redundant implications

This commit is contained in:
nonamethanks
2021-03-19 15:26:14 +01:00
parent 0249c290fd
commit 917b08639f
2 changed files with 15 additions and 4 deletions

View File

@@ -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

View File

@@ -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