-
Notifications
You must be signed in to change notification settings - Fork 88
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
Get previous / next item in tree #62
Comments
What would be the previous or next item in the tree? |
In the "flat" full tree ( |
The |
I don't mean one should use So in the flat view:
When doing You know what I mean? |
What would be an example use case for such a functionality? |
Btw. you could model the enum = Enumerator.new do |y|
Foo.walk_tree do |node, _|
y << node
end
end
# Get next tree node:
enum.next |
Use case: I have a small CMS which has a tree hierarchy of pages. I'd like users to be able to browse through all available pages using "Previous" and "Next" links. |
That would be much more efficient and easier to achieve with a nested set, because your previous node would be There's a popular gem awesome_nested_set which implements nested sets. It should be fairly easy to migrate to awsome_nested_set by adding an indexed lft and rgt integer column to your db and calling the gems |
Thanks for this useful hint. Still, I already use your gem, and I have to stick to it for several reasons. How would you implement a previous/next feature? I doesn't need to be very fancy. |
There isn't really a straight-forward way to do this. The first problem you will have is that there isn't really a defined order of nodes, so if you have a parent node a, and child nodes b, c, d the database can choose any order to return unless you also add an order column. If you have established a stable node order, you would have to check the position of the current node within self_and_siblings. For the previous case you would chose either the preceding node or if it is already the first sibling node go to the parent. For the next case go to the first child node of the current node or next node in the siblings or if the current node is already the last position go to the next sibling of the parent. This will be much more complex to write and much slower than woking with a nested set, so what are the reasons you need to stick with acts_as_tree? |
Is there already a method for finding the previous or next item in the tree?
The text was updated successfully, but these errors were encountered: