Skip to content

Conversation

markhgbrewster
Copy link

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.

self.class.generations.select {
|key, value| value.include?(self)
}.keys.first
end
Copy link
Collaborator

@felixbuenemann felixbuenemann Oct 5, 2016

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 }
Copy link
Collaborator

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 }
Copy link
Collaborator

@felixbuenemann felixbuenemann Oct 5, 2016

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.

@markhgbrewster
Copy link
Author

markhgbrewster commented Oct 7, 2016

@felixbuenemann thanks for the feedback. I have made the changes requested. Let me know what you think.

Copy link
Collaborator

@felixbuenemann felixbuenemann left a 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
Copy link
Collaborator

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.

@markhgbrewster
Copy link
Author

Thanks again. I have updated the comment as requested.

@felixbuenemann felixbuenemann merged commit f0048bc into amerine:master Oct 9, 2016
@felixbuenemann
Copy link
Collaborator

@markhgbrewster Thanks for your contribution. I've released acts_as_tree 2.6.0 with your changes.

@markhgbrewster
Copy link
Author

Thanks @felixbuenemann

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

Successfully merging this pull request may close these issues.

2 participants