module ComfortableMexicanSofa::ActsAsTree::InstanceMethods

Public Instance Methods

ancestors() click to toggle source

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]
# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 65
def ancestors
  node  = self
  nodes = []
  nodes << node = node.parent while node.parent
  nodes
end
descendants() click to toggle source

Returns all children and children of children

# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 73
def descendants
  nodes = []
  children.each do |c|
    nodes << c
    nodes << c.descendants
  end
  nodes.flatten
end
parent_id=(id) click to toggle source

BUG: github.com/rails/rails/issues/14369 It's still a bug. Remove it to see failing test

# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 110
def parent_id=(id)
  self.parent = self.class.find_by(id: id)
end
root() click to toggle source

Returns the root node of the tree.

# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 83
def root
  node = self
  node = node.parent while node.parent
  node
end
root?() click to toggle source

Checks if this node is a root

# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 90
def root?
  !parent_id
end
self_and_siblings() click to toggle source

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]
# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 104
def self_and_siblings
  parent ? parent.children : self.class.roots
end
siblings() click to toggle source

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]
# File lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb, line 97
def siblings
  self_and_siblings - [self]
end