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)


50
51
52
# File 'lib/is_tree/active_record/tree.rb', line 50

def leaf?
  self.children.empty?
end

#rootObject



46
47
48
# File 'lib/is_tree/active_record/tree.rb', line 46

def root
  self.ancestors.last
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  self.parent.nil?
end

#self_and_siblingsObject



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

def self_and_siblings
  self.parent_id ? self.class.scoped_by_parent_id(self.parent_id) : self.class.roots
end

#siblingsObject



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

def siblings
  self.self_and_siblings.scoped(:conditions => ["id <> :id",{:id => self.id}])
end