Module: Mongoid::Tree::ClassMethods

Defined in:
lib/mongoid/tree.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#leavesMongoid::Criteria

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

Examples:

Node.leaves

Returns:

  • (Mongoid::Criteria)

    Mongoid criteria to retrieve all leave nodes


154
155
156
# File 'lib/mongoid/tree.rb', line 154

def leaves
  where(:_id.nin => only(:parent_id).collect(&:parent_id))
end

#rootMongoid::Document

Returns the first root document

Examples:

Node.root

Returns:

  • (Mongoid::Document)

    The first root document


132
133
134
# File 'lib/mongoid/tree.rb', line 132

def root
  roots.first
end

#rootsMongoid::Criteria

Returns all root documents

Examples:

Node.roots

Returns:

  • (Mongoid::Criteria)

    Mongoid criteria to retrieve all root documents


143
144
145
# File 'lib/mongoid/tree.rb', line 143

def roots
  where(:parent_id => nil)
end