module ActsAsTree::InstanceMethods
Public Instance Methods
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
# File lib/acts_as_tree.rb, line 249 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end
Returns list of descendants, starting from current node, not including current node.
root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]
# File lib/acts_as_tree.rb, line 258 def descendants children.each_with_object(children.to_a) {|child, arr| arr.concat child.descendants }.uniq end
Returns all the nodes at the same level in the tree as the current node.
root1child1.generation # => [root1child2, root2child1, root2child2]
# File lib/acts_as_tree.rb, line 295 def generation self_and_generation - [self] end
Returns true if node has no children, false otherwise
subchild1.leaf? # => true child1.leaf? # => false
# File lib/acts_as_tree.rb, line 352 def leaf? children.size.zero? end
Returns the level (depth) of the current node unless level is a column on the node. Allows backwards compatibility with older versions of the gem.
Allows integration with apps using level as a column name.
root1child1.level # => 1
# File lib/acts_as_tree.rb, line 318 def level if self.class.column_names.include?('level') super else tree_level end end
Returns the root node of the tree.
# File lib/acts_as_tree.rb, line 272 def root node = self node = node.parent while node.parent node end
Returns true if node has no parent, false otherwise
subchild1.root? # => false root.root? # => true
# File lib/acts_as_tree.rb, line 344 def root? parent.nil? end
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
# File lib/acts_as_tree.rb, line 336 def self_and_ancestors [self] + self.ancestors end
Returns children (without subchildren) and current node itself.
root.self_and_children # => [root, child1]
# File lib/acts_as_tree.rb, line 329 def self_and_children [self] + self.children end
Returns list of descendants, starting from current node, including current node.
root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]
# File lib/acts_as_tree.rb, line 267 def self_and_descendants [self] + descendants end
Returns a reference to the current node and all the nodes at the same level as it in the tree.
root1child1.self_and_generation # => [root1child1, root1child2, root2child1, root2child2]
# File lib/acts_as_tree.rb, line 302 def self_and_generation self.class.select {|node| node.tree_level == self.tree_level } end
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
# File lib/acts_as_tree.rb, line 288 def self_and_siblings parent ? parent.children : self.class.roots end
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
# File lib/acts_as_tree.rb, line 281 def siblings self_and_siblings - [self] end
Returns the level (depth) of the current node
root1child1.tree_level # => 1
# File lib/acts_as_tree.rb, line 309 def tree_level self.ancestors.size end
Private Instance Methods
# File lib/acts_as_tree.rb, line 359 def update_parents_counter_cache end