module NoBrainer::Tree::ClassMethods

This module implements class methods that will be available on the document that includes NoBrainer::Tree

Public Instance Methods

leaves() click to toggle source

Returns all leaves (be careful, currently involves two queries)

@example

Node.leaves

@return [NoBrainer::Criteria] NoBrainer criteria to retrieve all leave nodes

# File lib/nobrainer/tree.rb, line 143
def leaves
  where(:id.nin => pluck(:id, :_type, :parent_id).collect(&:parent_id).compact)
end
root() click to toggle source

Returns the first root document

@example

Node.root

@return [NoBrainer::Document] The first root document

# File lib/nobrainer/tree.rb, line 121
def root
  roots.first
end
roots() click to toggle source

Returns all root documents

@example

Node.roots

@return [NoBrainer::Criteria] NoBrainer criteria to retrieve all root documents

# File lib/nobrainer/tree.rb, line 132
def roots
  where(:or => [:parent_id.defined => false, :parent_id => nil])
end