implications: fix validation of circular implications.

Fix bug where circular chains like `a -> b && b -> c && c -> a` were
allowed.
This commit is contained in:
evazion
2018-12-26 17:30:07 -06:00
parent 51b08d2243
commit 365e04b5f9
2 changed files with 8 additions and 5 deletions

View File

@@ -77,7 +77,7 @@ class TagImplication < TagRelationship
module ValidationMethods
def absence_of_circular_relation
# We don't want a -> b && b -> a chains
if TagImplication.active.exists?(["antecedent_name = ? and consequent_name = ?", consequent_name, antecedent_name])
if descendants.include?(antecedent_name)
errors[:base] << "Tag implication can not create a circular relation with another tag implication"
end
end

View File

@@ -80,10 +80,13 @@ class TagImplicationTest < ActiveSupport::TestCase
should "not validate when a circular relation is created" do
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa")
ti2.save
assert(ti2.errors.any?, "Tag implication should not have validated.")
assert_equal("Tag implication can not create a circular relation with another tag implication", ti2.errors.full_messages.join(""))
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
ti3 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa")
assert(ti1.valid?)
assert(ti2.valid?)
refute(ti3.valid?)
assert_equal("Tag implication can not create a circular relation with another tag implication", ti3.errors.full_messages.join(""))
end
should "not validate when a transitive relation is created" do