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.apply_default_scope if scoped
  end
end

#inherited(subclass) ⇒ Object

When inheriting, we want to copy the scopes from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.

Examples:

Inherit from this class.

Person.inherited(Doctor)

Parameters:

  • subclass (Class)

    The inheriting class.

Since:

  • 2.0.0.rc.6



137
138
139
140
# File 'lib/mongoid/named_scope.rb', line 137

def inherited(subclass)
  super
  subclass.scopes = scopes.dup
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
63
# 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}]
      conditions = scope.conditions.as_conditions(*args)
      scope.extend(criteria.fuse(conditions))
    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



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

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



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

def scoped(embedded = false)
  criteria(embedded).scoped
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



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

def unscoped(embedded = false)
  criteria(embedded).unscoped
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



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

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