Module: ActsAsTree::InstanceMethods
- Defined in:
- lib/acts_as_tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants ⇒ Object
Returns list of descendants, starting from current node, not including current node.
-
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise.
-
#root ⇒ Object
Returns the root node of the tree.
-
#root? ⇒ Boolean
Returns true if node has no parent, false otherwise.
-
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
-
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
-
#self_and_descendants ⇒ Object
Returns list of descendants, starting from current node, including 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]
218 219 220 221 222 |
# File 'lib/acts_as_tree.rb', line 218 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#descendants ⇒ Object
Returns list of descendants, starting from current node, not including current node.
root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]
227 228 229 230 231 |
# File 'lib/acts_as_tree.rb', line 227 def descendants children.each_with_object(children.to_a) {|child, arr| arr.concat child.descendants }.uniq end |
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise
subchild1.leaf? # => true
child1.leaf? # => false
287 288 289 |
# File 'lib/acts_as_tree.rb', line 287 def leaf? children.size.zero? end |
#root ⇒ Object
Returns the root node of the tree.
241 242 243 244 245 |
# File 'lib/acts_as_tree.rb', line 241 def root node = self node = node.parent while node.parent node end |
#root? ⇒ Boolean
Returns true if node has no parent, false otherwise
subchild1.root? # => false
root.root? # => true
279 280 281 |
# File 'lib/acts_as_tree.rb', line 279 def root? parent.nil? end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
271 272 273 |
# File 'lib/acts_as_tree.rb', line 271 def self_and_ancestors [self] + self.ancestors end |
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
root.self_and_children # => [root, child1]
264 265 266 |
# File 'lib/acts_as_tree.rb', line 264 def self_and_children [self] + self.children end |
#self_and_descendants ⇒ Object
Returns list of descendants, starting from current node, including current node.
root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]
236 237 238 |
# File 'lib/acts_as_tree.rb', line 236 def self_and_descendants [self] + descendants end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
257 258 259 |
# File 'lib/acts_as_tree.rb', line 257 def self_and_siblings parent ? parent.children : self.class.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
250 251 252 |
# File 'lib/acts_as_tree.rb', line 250 def siblings self_and_siblings - [self] end |