Module: ActiveRecord::Acts::Tree::InstanceMethods
- Defined in:
- lib/active_record/acts/tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#root ⇒ Object
Returns the root node of the tree.
-
#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]
67 68 69 70 71 |
# File 'lib/active_record/acts/tree.rb', line 67 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#root ⇒ Object
Returns the root node of the tree.
74 75 76 77 78 |
# File 'lib/active_record/acts/tree.rb', line 74 def root node = self node = node.parent while node.parent node end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
90 91 92 |
# File 'lib/active_record/acts/tree.rb', line 90 def self_and_siblings parent ? parent.children : self.class.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
83 84 85 |
# File 'lib/active_record/acts/tree.rb', line 83 def siblings self_and_siblings - [self] end |