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.
-
#generation ⇒ Object
Returns all the nodes at the same level in the tree as the current node.
-
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise.
-
#level ⇒ Object
Returns the level (depth) of the current node unless level is a column on the node.
-
#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_generation ⇒ Object
Returns a reference to the current node and all the nodes at the same level as it in 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.
-
#tree_level ⇒ Object
Returns the level (depth) of the current node .
Instance Method Details
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
253 254 255 256 257 |
# File 'lib/acts_as_tree.rb', line 253 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]
262 263 264 265 266 |
# File 'lib/acts_as_tree.rb', line 262 def descendants children.each_with_object(children.to_a) {|child, arr| arr.concat child.descendants }.uniq end |
#generation ⇒ Object
Returns all the nodes at the same level in the tree as the current node.
root1child1.generation # => [root1child2, root2child1, root2child2]
299 300 301 |
# File 'lib/acts_as_tree.rb', line 299 def generation self_and_generation - [self] end |
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise
subchild1.leaf? # => true
child1.leaf? # => false
356 357 358 |
# File 'lib/acts_as_tree.rb', line 356 def leaf? children.size.zero? end |
#level ⇒ Object
Returns the level (depth) of the current node unless level is a column on the node. Allows backwards compatibility with older versions of the gem.
Allows integration with apps using level as a column name.
root1child1.level # => 1
322 323 324 325 326 327 328 |
# File 'lib/acts_as_tree.rb', line 322 def level if self.class.column_names.include?('level') super else tree_level end end |
#root ⇒ Object
Returns the root node of the tree.
276 277 278 279 280 |
# File 'lib/acts_as_tree.rb', line 276 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
348 349 350 |
# File 'lib/acts_as_tree.rb', line 348 def root? parent.nil? end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
340 341 342 |
# File 'lib/acts_as_tree.rb', line 340 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]
333 334 335 |
# File 'lib/acts_as_tree.rb', line 333 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]
271 272 273 |
# File 'lib/acts_as_tree.rb', line 271 def self_and_descendants [self] + descendants end |
#self_and_generation ⇒ Object
Returns a reference to the current node and all the nodes at the same level as it in the tree.
root1child1.self_and_generation # => [root1child1, root1child2, root2child1, root2child2]
306 307 308 |
# File 'lib/acts_as_tree.rb', line 306 def self_and_generation self.class.select {|node| node.tree_level == self.tree_level } end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
292 293 294 |
# File 'lib/acts_as_tree.rb', line 292 def self_and_siblings parent ? parent.children : self.class.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
285 286 287 |
# File 'lib/acts_as_tree.rb', line 285 def siblings self_and_siblings - [self] end |
#tree_level ⇒ Object
Returns the level (depth) of the current node
root1child1.tree_level # => 1
313 314 315 |
# File 'lib/acts_as_tree.rb', line 313 def tree_level self.ancestors.size end |