Module: ComfortableMexicanSofa::ActsAsTree::InstanceMethods
- Defined in:
- lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants ⇒ Object
Returns all children and children of children.
- #parent_id=(id) ⇒ Object
-
#root ⇒ Object
Returns the root node of the tree.
-
#root? ⇒ Boolean
Checks if this node is a root.
-
#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]
59 60 61 62 63 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 59 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#descendants ⇒ Object
Returns all children and children of children
66 67 68 69 70 71 72 73 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 66 def descendants nodes = [] self.children.each do |c| nodes << c nodes << c.descendants end nodes.flatten end |
#parent_id=(id) ⇒ Object
102 103 104 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 102 def parent_id=(id) self.parent = self.class.find_by(:id => id) end |
#root ⇒ Object
Returns the root node of the tree.
76 77 78 79 80 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 76 def root node = self node = node.parent while node.parent node end |
#root? ⇒ Boolean
Checks if this node is a root
83 84 85 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 83 def root? !self.parent_id end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
97 98 99 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 97 def self_and_siblings parent ? parent.children : self.class.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
90 91 92 |
# File 'lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb', line 90 def siblings self_and_siblings - [self] end |