Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph like behaviour #5

Closed
yamanyar opened this issue May 29, 2011 · 3 comments
Closed

Graph like behaviour #5

yamanyar opened this issue May 29, 2011 · 3 comments

Comments

@yamanyar
Copy link

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

@yamanyar
Copy link
Author

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

@swanandp
Copy link
Collaborator

@yamanyar - I think you got confused with acts_as_tree. There is no parent-children behaviour in acts_as_list. Or am I missing something?

@yamanyar
Copy link
Author

Yes you are right; I was asked to post my question to here and I didn't check that this repository was for acts-as_tree.

Sorry for the wrong post
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants