acts_as_tree icon indicating copy to clipboard operation
acts_as_tree copied to clipboard

Graph like behaviour

Open yamanyar opened this issue 13 years ago • 0 comments

I can store a relation that is a graph in fact. Is it expected? I think it should throw an exception when there is a recycling path (For example A->B->A->B....). Here is the test (please note that this test passes; however i am expecting it to fail):

category = Category.new
category.name="Parent"
category.save
child = category.children.create("name" => "child_1")
child.children = [category]
assert category.save
loadedChild = Category.find_by_name("child_1")
assert_equal "Parent", loadedChild.parent.name
assert_equal "Parent", loadedChild.children[0].name

I added following code snippet to my domain object: (i am new to ruby so may be this is not the best way)

Although this seems alright with unit tests; when i test manually from browser; server dies due to infinite loop i guess.

validate :validate_recycling

def validate_recycling
begin
check_recycling []
rescue ArgumentError => msg
errors.add(:base, msg.message)
end
end

def check_recycling(parents)
raise ArgumentError, "#{self.name} is recycling!" if parents.include?(self)
copy = parents.dup
copy.push self
children.each { |child| child.check_recycling(copy) }
end

I guess the problem is before to validation, following lines (i found in the source code of the gem i installed) may run forever:

nodes << node = node.parent while node.parent

yamanyar avatar May 30 '11 07:05 yamanyar