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:
@@ -77,7 +77,7 @@ class TagImplication < TagRelationship
|
|||||||
module ValidationMethods
|
module ValidationMethods
|
||||||
def absence_of_circular_relation
|
def absence_of_circular_relation
|
||||||
# We don't want a -> b && b -> a chains
|
# 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"
|
errors[:base] << "Tag implication can not create a circular relation with another tag implication"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -80,10 +80,13 @@ class TagImplicationTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
should "not validate when a circular relation is created" do
|
should "not validate when a circular relation is created" do
|
||||||
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
|
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
|
||||||
ti2 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa")
|
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
|
||||||
ti2.save
|
ti3 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa")
|
||||||
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(""))
|
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
|
end
|
||||||
|
|
||||||
should "not validate when a transitive relation is created" do
|
should "not validate when a transitive relation is created" do
|
||||||
|
|||||||
Reference in New Issue
Block a user