Module: Mongoid::NamedScope::ClassMethods

Defined in:
lib/mongoid/named_scope.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#criteria(embedded = false, scoped = true) ⇒ Criteria

Gets either the last scope on the stack or creates a new criteria.

Examples:

Get the last or new.

Person.scoping(true)

Parameters:

  • embedded (true, false) (defaults to: false)

    Is this scope for an embedded doc?

  • scoped (true, false) (defaults to: true)

    Are we applying default scoping?

Returns:

  • (Criteria)

    The last scope or a new one.

Since:

  • 2.0.0



26
27
28
29
30
# File 'lib/mongoid/named_scope.rb', line 26

def criteria(embedded = false, scoped = true)
  scope_stack.last || Criteria.new(self, embedded).tap do |crit|
    return crit.fuse(default_scoping) if default_scoping && scoped
  end
end

#scope(name, conditions = {}, &block) ⇒ Object Also known as: named_scope

Creates a named_scope for the Document, similar to ActiveRecord’s named_scopes. NamedScopes are proxied Criteria objects that can be chained.

Examples:

Create named scopes.


class Person
  include Mongoid::Document
  field :active, :type => Boolean
  field :count, :type => Integer

  scope :active, :where => { :active => true }
  scope :count_gt_one, :where => { :count.gt => 1 }
  scope :at_least_count, lambda { |count| { :where => { :count.gt => count } } }
end

Parameters:

  • name (Symbol)

    The name of the scope.

  • conditions (Hash, Criteria) (defaults to: {})

    The conditions of the scope.

Since:

  • 1.0.0



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/named_scope.rb', line 52

def scope(name, conditions = {}, &block)
  name = name.to_sym
  valid_scope_name?(name)
  scopes[name] = Scope.new(conditions, &block)
  (class << self; self; end).class_eval <<-EOT
    def #{name}(*args)
      scope = scopes[:#{name}]
      scope.extend(criteria.fuse(scope.conditions.scoped(*args)))
    end
  EOT
end

#scope_stackArray<Criteria>

Initializes and returns the current scope stack.

Examples:

Get the scope stack.

Person.scope_stack

Returns:

Since:

  • 1.0.0



87
88
89
# File 'lib/mongoid/named_scope.rb', line 87

def scope_stack
  Threaded.scope_stack[object_id] ||= []
end

#scoped(embedded = false) ⇒ Criteria

Get a criteria object for the class, scoped to the default if defined.

Examples:

Get a scoped criteria.

Person.scoped

Parameters:

  • embedded (true, false) (defaults to: false)

    Is the criteria for embedded docs?

Returns:

Since:

  • 2.0.0



75
76
77
# File 'lib/mongoid/named_scope.rb', line 75

def scoped(embedded = false)
  criteria(embedded, true)
end

#unscoped(embedded = false) ⇒ Criteria

Get a criteria object for the class, ignoring default scoping.

Examples:

Get an unscoped criteria.

Person.scoped

Parameters:

  • embedded (true, false) (defaults to: false)

    Is the criteria for embedded docs?

Returns:

Since:

  • 2.0.0



101
102
103
# File 'lib/mongoid/named_scope.rb', line 101

def unscoped(embedded = false)
  criteria(embedded, false)
end

#with_scope(criteria) ⇒ Criteria

Pushes the provided criteria onto the scope stack, and removes it after the provided block is yielded.

Examples:

Yield to the criteria.

Person.with_scope(criteria)

Parameters:

  • criteria (Criteria)

    The criteria to apply.

Returns:

Since:

  • 1.0.0



116
117
118
119
120
121
122
123
124
# File 'lib/mongoid/named_scope.rb', line 116

def with_scope(criteria)
  scope_stack = self.scope_stack
  scope_stack << criteria
  begin
    yield criteria
  ensure
    scope_stack.pop
  end
end