Module: ActsAsTree::InstanceMethods

Defined in:
lib/acts_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


249
250
251
252
253
# File 'lib/acts_as_tree.rb', line 249

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

#descendantsObject

Returns list of descendants, starting from current node, not including current node.

root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]


258
259
260
261
262
# File 'lib/acts_as_tree.rb', line 258

def descendants
  children.each_with_object(children.to_a) {|child, arr|
    arr.concat child.descendants
  }.uniq
end

#generationObject

Returns all the nodes at the same level in the tree as the current node.

root1child1.generation # => [root1child2, root2child1, root2child2]


295
296
297
# File 'lib/acts_as_tree.rb', line 295

def generation
  self_and_generation - [self]
end

#leaf?Boolean

Returns true if node has no children, false otherwise

subchild1.leaf? # => true
child1.leaf?    # => false

Returns:

  • (Boolean)


352
353
354
# File 'lib/acts_as_tree.rb', line 352

def leaf?
  children.size.zero?
end

#levelObject

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


318
319
320
321
322
323
324
# File 'lib/acts_as_tree.rb', line 318

def level
  if self.class.column_names.include?('level')
    super
  else
    tree_level
  end
end

#rootObject

Returns the root node of the tree.



272
273
274
275
276
# File 'lib/acts_as_tree.rb', line 272

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

Returns:

  • (Boolean)


344
345
346
# File 'lib/acts_as_tree.rb', line 344

def root?
  parent.nil?
end

#self_and_ancestorsObject

Returns ancestors and current node itself.

subchild1.self_and_ancestors # => [subchild1, child1, root]


336
337
338
# File 'lib/acts_as_tree.rb', line 336

def self_and_ancestors
  [self] + self.ancestors
end

#self_and_childrenObject

Returns children (without subchildren) and current node itself.

root.self_and_children # => [root, child1]


329
330
331
# File 'lib/acts_as_tree.rb', line 329

def self_and_children
  [self] + self.children
end

#self_and_descendantsObject

Returns list of descendants, starting from current node, including current node.

root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]


267
268
269
# File 'lib/acts_as_tree.rb', line 267

def self_and_descendants
  [self] + descendants
end

#self_and_generationObject

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]


302
303
304
# File 'lib/acts_as_tree.rb', line 302

def self_and_generation
  self.class.select {|node| node.tree_level == self.tree_level }
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


288
289
290
# File 'lib/acts_as_tree.rb', line 288

def self_and_siblings
  parent ? parent.children : self.class.roots
end

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


281
282
283
# File 'lib/acts_as_tree.rb', line 281

def siblings
  self_and_siblings - [self]
end

#tree_levelObject

Returns the level (depth) of the current node

root1child1.tree_level # => 1


309
310
311
# File 'lib/acts_as_tree.rb', line 309

def tree_level
  self.ancestors.size
end