Module: IsTree::ActiveRecord::Tree::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



40
41
42
43
44
# File 'lib/is_tree/active_record/tree.rb', line 40

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

#leaf?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/is_tree/active_record/tree.rb', line 53

def leaf?
  self.children.empty?
end

#rootObject

Returns the root node of the tree.



47
48
49
50
51
# File 'lib/is_tree/active_record/tree.rb', line 47

def root
  node = self
  node = node.parent while node.parent
  node
end

#root?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/is_tree/active_record/tree.rb', line 57

def root?
  self.parent.nil?
end

#self_and_siblingsObject



65
66
67
# File 'lib/is_tree/active_record/tree.rb', line 65

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

#siblingsObject



61
62
63
# File 'lib/is_tree/active_record/tree.rb', line 61

def siblings
  self.self_and_siblings.reject{|node| node == self}
end