module ActsAsTree::InstanceMethods

Public Instance Methods

ancestors() click to toggle source

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
descendants() click to toggle source

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
generation() click to toggle source

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
leaf?() click to toggle source

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
level() click to toggle source

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
Calls superclass method
# File lib/acts_as_tree.rb, line 318
def level
  if self.class.column_names.include?('level')
    super
  else
    tree_level
  end
end
root() click to toggle source

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
root?() click to toggle source

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
self_and_ancestors() click to toggle source

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
self_and_children() click to toggle source

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
self_and_descendants() click to toggle source

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
self_and_generation() click to toggle source

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
self_and_siblings() click to toggle source

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
siblings() click to toggle source

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
tree_level() click to toggle source

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

update_parents_counter_cache() click to toggle source
# File lib/acts_as_tree.rb, line 359
def update_parents_counter_cache
end