diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 9a09b6f42..12fb6bbc3 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -4,8 +4,8 @@ class TagAlias < TagRelationship has_many :tag_rel_undos, as: :tag_rel after_save :create_mod_action - validates :antecedent_name, uniqueness: { conditions: -> { duplicate_relevant } } - validate :absence_of_transitive_relation + validates :antecedent_name, uniqueness: { conditions: -> { duplicate_relevant } }, unless: :is_deleted? + validate :absence_of_transitive_relation, unless: :is_deleted? module ApprovalMethods def approve!(update_topic: true, approver: CurrentUser.user) diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 10381c252..9c74539f8 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -9,11 +9,13 @@ class TagImplication < TagRelationship after_destroy :update_descendant_names_for_parents after_save :update_descendant_names_for_parents after_save :create_mod_action, if: :saved_change_to_status? - validates :antecedent_name, uniqueness: { scope: [:consequent_name], conditions: -> { duplicate_relevant } } - validate :absence_of_circular_relation - validate :absence_of_transitive_relation - validate :antecedent_is_not_aliased - validate :consequent_is_not_aliased + with_options unless: :is_deleted? do + validates :antecedent_name, uniqueness: { scope: [:consequent_name], conditions: -> { duplicate_relevant } } + validate :absence_of_circular_relation + validate :absence_of_transitive_relation + validate :antecedent_is_not_aliased + validate :consequent_is_not_aliased + end module DescendantMethods extend ActiveSupport::Concern diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 4db2df243..ff8eb50de 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -154,13 +154,31 @@ class TagAliasTest < ActiveSupport::TestCase should "error on approve if its not valid anymore" do create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "active") - ta = build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", creator: @admin) + ta = build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "pending", creator: @admin) ta.save(validate: false) with_inline_jobs { ta.approve!(approver: @admin) } assert_match "error", ta.reload.status end + should "allow rejecting if an active duplicate exists" do + create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "active") + ta = build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "pending", creator: @admin) + ta.save(validate: false) + ta.reject! + + assert_equal "deleted", ta.reload.status + end + + should "allow rejecting if an active transitive exists" do + create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "active") + ta = build(:tag_alias, antecedent_name: "bbb", consequent_name: "aaa", status: "pending", creator: @admin) + ta.save(validate: false) + ta.reject! + + assert_equal "deleted", ta.reload.status + end + context "with an associated forum topic" do setup do @admin = create(:admin_user) diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index fd5184636..b692c7c73 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -121,6 +121,15 @@ class TagImplicationTest < ActiveSupport::TestCase assert_includes(ti.errors[:base], "Consequent tag must not be aliased to another tag") end + should "allow rejecting if active aliases exist" do + create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") + ti = build(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", status: "pending", creator: @user) + ti.save(validate: false) + + ti.reject! + assert_equal("deleted", ti.reload.status) + end + should "calculate all its descendants" do ti1 = create(:tag_implication, antecedent_name: "bbb", consequent_name: "ccc") assert_equal(%w[ccc], ti1.descendant_names)