Module: Mongoid::Scoping::ClassMethods

Defined in:
lib/mongoid/scoping.rb

Instance Method Summary collapse

Instance Method Details

#default_scopable?true, false

Is the class able to have the default scope applied?

Examples:

Can the default scope be applied?

Band.default_scopable?

Returns:

  • (true, false)

    If the default scope can be applied.

Since:

  • 3.0.0



54
55
56
# File 'lib/mongoid/scoping.rb', line 54

def default_scopable?
  default_scoping? && !Threaded.executing?(:without_default_scope)
end

#default_scope(value) ⇒ Proc

Add a default scope to the model. This scope will be applied to all criteria unless #unscoped is specified.

Examples:

Define a default scope with a criteria.

class Band
  include Mongoid::Document
  field :active, type: Boolean
  default_scope where(active: true)
end

Define a default scope with a proc.

class Band
  include Mongoid::Document
  field :active, type: Boolean
  default_scope ->{ where(active: true) }
end

Parameters:

  • scope (Proc, Criteria)

    The default scope.

Returns:

  • (Proc)

    The default scope.

Raises:

Since:

  • 1.0.0



41
42
43
44
# File 'lib/mongoid/scoping.rb', line 41

def default_scope(value)
  check_scope_validity(value)
  self.default_scoping = process_default_scope(value)
end

#queryableCriteria

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a queryable, either the last one on the scope stack or a fresh one.

Examples:

Get a queryable.

Model.queryable

Returns:

Since:

  • 3.0.0



68
69
70
# File 'lib/mongoid/scoping.rb', line 68

def queryable
  scope_stack.last || Criteria.new(self)
end

#scope(name, value, &block) ⇒ Object

Create a scope that can be accessed from the class level or chained to criteria by the provided name.

Examples:

Create named scopes.


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

  scope :active, where(active: true)
  scope :at_least, ->(count){ where(:count.gt => count) }
end

Parameters:

  • name (Symbol)

    The name of the scope.

  • conditions (Proc, Criteria)

    The conditions of the scope.

Raises:

Since:

  • 1.0.0



93
94
95
96
97
98
99
100
101
102
# File 'lib/mongoid/scoping.rb', line 93

def scope(name, value, &block)
  normalized = name.to_sym
  check_scope_validity(value)
  check_scope_name(normalized)
  scopes[normalized] = {
    scope: strip_default_scope(value),
    extension: Module.new(&block)
  }
  define_scope_method(normalized)
end

#scope_stackArray<Criteria>

Initializes and returns the current scope stack.

Examples:

Get the scope stack.

Person.scope_stack

Returns:

  • (Array<Criteria>)

    The scope stack.

Since:

  • 1.0.0



112
113
114
# File 'lib/mongoid/scoping.rb', line 112

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

#scoped(options = nil) ⇒ Criteria

Note:

This will force the default scope to be applied.

Get a criteria for the document with normal scoping.

Examples:

Get the criteria.

Band.scoped(skip: 10)

Parameters:

  • options (Hash) (defaults to: nil)

    Query options for the criteria.

Options Hash (options):

  • :skip (Integer)

    Optional number of documents to skip.

  • :limit (Integer)

    Optional number of documents to limit.

  • :sort (Array)

    Optional sorting options.

Returns:

Since:

  • 3.0.0



133
134
135
# File 'lib/mongoid/scoping.rb', line 133

def scoped(options = nil)
  queryable.scoped(options)
end

#unscopedCriteria, Object

Note:

This will force the default scope to be removed.

Get the criteria without the default scoping applied.

Examples:

Get the unscoped criteria.

Band.unscoped

Yield to block with no default scoping.

Band.unscoped do
  Band.where(name: "Depeche Mode")
end

Returns:

  • (Criteria, Object)

    The unscoped criteria or result of the block.

Since:

  • 3.0.0



153
154
155
156
157
158
159
160
161
# File 'lib/mongoid/scoping.rb', line 153

def unscoped
  if block_given?
    without_default_scope do
      yield(self)
    end
  else
    queryable.unscoped
  end
end

#with_default_scopeCriteria Also known as: criteria

Get a criteria with the default scope applied, if possible.

Examples:

Get a criteria with the default scope.

Model.with_default_scope

Returns:

Since:

  • 3.0.0



171
172
173
# File 'lib/mongoid/scoping.rb', line 171

def with_default_scope
  queryable.with_default_scope
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



187
188
189
190
191
192
193
194
# File 'lib/mongoid/scoping.rb', line 187

def with_scope(criteria)
  scope_stack.push(criteria)
  begin
    yield criteria
  ensure
    scope_stack.pop
  end
end

#without_default_scopeObject

Execute the block without applying the default scope.

Examples:

Execute without the default scope.

Band.without_default_scope do
  Band.where(name: "Depeche Mode")
end

Returns:

  • (Object)

    The result of the block.

Since:

  • 3.0.0



206
207
208
209
210
211
# File 'lib/mongoid/scoping.rb', line 206

def without_default_scope
  Threaded.begin_execution("without_default_scope")
  yield
ensure
  Threaded.exit_execution("without_default_scope")
end