-
Notifications
You must be signed in to change notification settings - Fork 89
Gererations methods #56
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
Gererations methods #56
Conversation
self.class.generations.select { | ||
|key, value| value.include?(self) | ||
}.keys.first | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this method should be simplified and renamed to level
:
def level
ancestors.size
end
Your current method is doing much more work then it needs to:
- Walk from each node to the root node to get its level
- Group the result into a hash of arrays
- Iterate through the hash
- Walk through every array member to compare with the current node
- Return the first matching key
# | ||
# Class.generations { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] } | ||
def self.generations | ||
all.group_by{ |node| node.ancestors.size } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to all.group_by(&:level)
.
# | ||
# root1child1.generation [root1child1, root1child2, root2child1, root2child2] | ||
def self_and_generation | ||
self.class.select {|node| node.ancestors.size == self.ancestors.size } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the block to node.level == level
.
@felixbuenemann thanks for the feedback. I have made the changes requested. Let me know what you think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good aside from a minor ntpick regarding the comments.
self.class.select {|node| node.level == self.level } | ||
end | ||
|
||
# Returns level (key) for the current nods generation in the generations hash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment needs to be updated, for example:
Returns the level (depth) of the current node
I don't think the fact that the level is also a valid key in the generations hash matters in this context.
Thanks again. I have updated the comment as requested. |
@markhgbrewster Thanks for your contribution. I've released acts_as_tree 2.6.0 with your changes. |
Thanks @felixbuenemann |
I have added 4 methods to enable grouping and return of groups of nodes by their level in the tree structure. Nodes at the same level in the tree structure would be considered a generation Previously needed similar functionality when us acts_as_tree as part of a permission functionality I was extending. I am not sure if this something that is within the scope of the gem but I enjoyed doing it and I would be interested to hear what you think about it.