aliases: allow aliases to be reversed in one step.

Allow reversing an alias without having to remove the old alias first.
When aliasing A -> B, then if B -> A already exists it will
automatically be removed first.
This commit is contained in:
evazion
2020-12-02 12:44:43 -06:00
parent 9e37f5a588
commit 6485a053a5
3 changed files with 27 additions and 8 deletions

View File

@@ -2,6 +2,8 @@ class TagAlias < TagRelationship
validates_uniqueness_of :antecedent_name, scope: :status, conditions: -> { active }
validate :absence_of_transitive_relation
before_create :delete_conflicting_alias
def self.to_aliased(names)
names = Array(names).map(&:to_s)
return [] if names.empty?
@@ -13,13 +15,22 @@ class TagAlias < TagRelationship
TagMover.new(antecedent_name, consequent_name, user: User.system).move!
end
# 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.
def absence_of_transitive_relation
return if is_rejected?
# 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 TagAlias.active.exists?(antecedent_name: consequent_name)
errors[:base] << "A tag alias for #{consequent_name} already exists"
tag_alias = TagAlias.active.find_by(antecedent_name: consequent_name)
if tag_alias.present? && tag_alias.consequent_name != antecedent_name
errors[:base] << "#{tag_alias.antecedent_name} is already aliased to #{tag_alias.consequent_name}"
end
end
# 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?
end
end