Module: ActsAsTree::InstanceMethods

Defined in:
lib/acts_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


67
68
69
70
71
# File 'lib/acts_as_tree.rb', line 67

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

#rootObject

Returns the root node of the tree.



74
75
76
77
78
# File 'lib/acts_as_tree.rb', line 74

def root
  node = self
  node = node.parent while node.parent
  node
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


90
91
92
# File 'lib/acts_as_tree.rb', line 90

def self_and_siblings
  parent ? parent.children : self.class.roots
end

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


83
84
85
# File 'lib/acts_as_tree.rb', line 83

def siblings
  self_and_siblings - [self]
end