Module: ActiveRecord::Acts::Tree::InstanceMethods
- Defined in:
- lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants(depth = nil) ⇒ Object
Returns list of descendants.
-
#root ⇒ Object
Returns the root node of the tree.
-
#self_and_descendants(depth = nil) ⇒ Object
Returns list of descendants and a reference to the current node.
-
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
-
#siblings ⇒ Object
Returns all siblings of the current node.
Instance Method Details
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
63 64 65 66 67 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 63 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#descendants(depth = nil) ⇒ Object
Returns list of descendants.
root.descendants # => [child1, subchild1, subchild2]
72 73 74 75 76 77 78 79 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 72 def descendants(depth=nil) depth ||= 0 result = children.dup unless depth == 1 result += children.collect {|child| child.descendants(depth-1)}.flatten end result end |
#root ⇒ Object
Returns the root node of the tree.
89 90 91 92 93 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 89 def root node = self node = node.parent while node.parent node end |
#self_and_descendants(depth = nil) ⇒ Object
Returns list of descendants and a reference to the current node.
root.self_and_descendants # => [root, child1, subchild1, subchild2]
84 85 86 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 84 def self_and_descendants(depth=nil) [self] + descendants(depth) end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
105 106 107 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 105 def self_and_siblings parent ? parent.children : self.class.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
98 99 100 |
# File 'lib/plugins/acts_as_tree/lib/active_record/acts/tree.rb', line 98 def siblings self_and_siblings - [self] end |