module ActsAsRecursiveTree::Model

Public Instance Methods

ancestors(&block) click to toggle source

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]

# File lib/acts_as_recursive_tree/model.rb, line 12
def ancestors(&block)
  base_class.ancestors_of(self, &block)
end
descendants(&block) click to toggle source

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

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

# File lib/acts_as_recursive_tree/model.rb, line 29
def descendants(&block)
  base_class.descendants_of(self, &block)
end
leaf?() click to toggle source

Returns true if node has no children, false otherwise

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

# File lib/acts_as_recursive_tree/model.rb, line 90
def leaf?
  children.none?
end
leaves() click to toggle source

Returns all Leaves

# File lib/acts_as_recursive_tree/model.rb, line 74
def leaves
  base_class.leaves_of(self)
end
root() click to toggle source

Returns the root node of the tree.

# File lib/acts_as_recursive_tree/model.rb, line 44
def root
  self_and_ancestors.where(_recursive_tree_config.parent_key => nil).first
end
root?() click to toggle source

Returns true if node has no parent, false otherwise

subchild1.root? # => false root.root? # => true

# File lib/acts_as_recursive_tree/model.rb, line 82
def root?
  attributes[_recursive_tree_config.parent_key.to_s].blank?
end
self_and_ancestors(&block) click to toggle source

Returns ancestors and current node itself.

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

# File lib/acts_as_recursive_tree/model.rb, line 20
def self_and_ancestors(&block)
  base_class.self_and_ancestors_of(self, &block)
end
self_and_children() click to toggle source

Returns children (without subchildren) and current node itself.

root.self_and_children # => [root, child1]

# File lib/acts_as_recursive_tree/model.rb, line 60
def self_and_children
  table = self.class.arel_table
  id    = attributes[_recursive_tree_config.primary_key.to_s]

  base_class.where(
    table[_recursive_tree_config.primary_key].eq(id).or(
      table[_recursive_tree_config.parent_key].eq(id)
    )
  )
end
self_and_descendants(&block) click to toggle source

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

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

# File lib/acts_as_recursive_tree/model.rb, line 38
def self_and_descendants(&block)
  base_class.self_and_descendants_of(self, &block)
end
siblings() click to toggle source

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]

# File lib/acts_as_recursive_tree/model.rb, line 52
def siblings
  self_and_siblings.where.not(id: id)
end

Private Instance Methods

base_class() click to toggle source
# File lib/acts_as_recursive_tree/model.rb, line 94
def base_class
  self.class.base_class
end