Module: ActiveRecord::Acts::Tree::InstanceMethods

Defined in:
lib/active_record/acts/tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


68
69
70
71
72
# File 'lib/active_record/acts/tree.rb', line 68

def ancestors
  node, nodes = self, []
  nodes << node = node.parent until not node.has_parent?
  nodes
end

#rootObject



74
75
76
77
78
# File 'lib/active_record/acts/tree.rb', line 74

def root
  node = self
  node = node.parent until not node.has_parent?
  node
end

#self_and_siblingsObject



84
85
86
# File 'lib/active_record/acts/tree.rb', line 84

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

#siblingsObject



80
81
82
# File 'lib/active_record/acts/tree.rb', line 80

def siblings
  self_and_siblings - [self]
end