Module: Mongoid::Criteria::Scopable

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criteria/scopable.rb

Overview

Mixin module included in Mongoid::Criteria which adds functionality related to default query scopes and named scopes.

Instance Method Summary collapse

Instance Method Details

#apply_default_scopeCriteria

Applies the default scope to the criteria.

Examples:

Apply the default scope.

criteria.apply_default_scope

Returns:



18
19
20
21
22
23
# File 'lib/mongoid/criteria/scopable.rb', line 18

def apply_default_scope
  klass.without_default_scope do
    merge!(klass.default_scoping.call)
  end
  self.scoping_options = true, false
end

#apply_scope(scope) ⇒ Criteria

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.

Applies a scope to the current criteria.

This method does not modify the receiver but it may return a new object or the receiver depending on the argument: if the scope argument is nil, the receiver is returned without modification, otherwise a new criteria object is returned.

Parameters:

  • scope (Proc | Symbol | Criteria | nil)

    The scope to apply.

Returns:

  • (Criteria)

    The criteria with the scope applied.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongoid/criteria/scopable.rb', line 37

def apply_scope(scope)
  case scope
  when Proc
    instance_exec(&scope)
  when Symbol
    send(scope)
  when Criteria
    merge(scope)
  else
    self
  end
end

#remove_scoping(other) ⇒ Criteria

Given another criteria, remove the other criteria’s scoping from this criteria.

Examples:

Remove the scoping.

criteria.remove_scoping(other)

Parameters:

  • other (Criteria)

    The other criteria.

Returns:

  • (Criteria)

    The criteria with scoping removed.



59
60
61
62
63
64
65
66
67
# File 'lib/mongoid/criteria/scopable.rb', line 59

def remove_scoping(other)
  if other
    reject_matching(other, :selector, :options)
    other.inclusions.each do |meta|
      inclusions.delete_one(meta)
    end
  end
  self
end

#scoped(options = nil) ⇒ Criteria

Forces the criteria to be scoped, unless its inside an unscoped block.

Examples:

Force the criteria to be scoped.

criteria.scoped(skip: 10)

Parameters:

  • options (Hash) (defaults to: nil)

    Additional query options.

Returns:



77
78
79
80
81
82
83
84
# File 'lib/mongoid/criteria/scopable.rb', line 77

def scoped(options = nil)
  crit = clone
  crit.options.merge!(options || {})
  if klass.default_scopable? && !scoped?
    crit.apply_default_scope
  end
  crit
end

#scoped?true | false

Has the criteria had the default scope applied?

Examples:

Is the default scope applied?

criteria.scoped?

Returns:

  • (true | false)

    If the default scope is applied.



92
93
94
# File 'lib/mongoid/criteria/scopable.rb', line 92

def scoped?
  !!(defined?(@scoped) ? @scoped : nil)
end

#scoping_optionsArray

Get the criteria scoping options, as a pair (scoped, unscoped).

Examples:

Get the scoping options.

criteria.scoping_options

Returns:

  • (Array)

    Scoped, unscoped.



127
128
129
# File 'lib/mongoid/criteria/scopable.rb', line 127

def scoping_options
  [ (defined?(@scoped) ? @scoped : nil), (defined?(@unscoped) ? @unscoped : nil) ]
end

#scoping_options=(options) ⇒ Array

Set the criteria scoping options, as a pair (scoped, unscoped).

Examples:

Set the scoping options.

criteria.scoping_options = true, false

Parameters:

  • options (Array)

    Scoped, unscoped.

Returns:

  • (Array)

    The new scoping options.



139
140
141
# File 'lib/mongoid/criteria/scopable.rb', line 139

def scoping_options=(options)
  @scoped, @unscoped = options
end

#unscopedCriteria

Clears all scoping from the criteria.

Examples:

Clear all scoping from the criteria.

criteria.unscoped

Returns:



102
103
104
105
106
107
108
109
# File 'lib/mongoid/criteria/scopable.rb', line 102

def unscoped
  crit = clone
  unless unscoped?
    crit.scoping_options = false, true
    crit.selector.clear; crit.options.clear
  end
  crit
end

#unscoped?true | false

Is the criteria unscoped?

Examples:

Is the criteria unscoped?

criteria.unscoped?

Returns:

  • (true | false)

    If the criteria is force unscoped.



117
118
119
# File 'lib/mongoid/criteria/scopable.rb', line 117

def unscoped?
  !!(defined?(@unscoped) ? @unscoped : nil)
end

#with_default_scopeCriteria

Get the criteria with the default scope applied, if the default scope is able to be applied. Cases in which it cannot are: If we are in an unscoped block, if the criteria is already forced unscoped, or the default scope has already been applied.

Examples:

Get the criteria with the default scope.

criteria.with_default_scope

Returns:



152
153
154
155
156
157
158
# File 'lib/mongoid/criteria/scopable.rb', line 152

def with_default_scope
  crit = clone
  if klass.default_scopable? && !unscoped? && !scoped?
    crit.apply_default_scope
  end
  crit
end