Module: Mongoid::Criterion::Scoping

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criterion/scoping.rb

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:

Since:

  • 3.0.0



14
15
16
17
18
19
# File 'lib/mongoid/criterion/scoping.rb', line 14

def apply_default_scope
  klass.without_default_scope do
    merge!(klass.default_scoping.call)
  end
  self.scoping_options = true, false
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.

Since:

  • 3.0.0



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mongoid/criterion/scoping.rb', line 32

def remove_scoping(other)
  if other
    selector.reject! do |key, value|
      other.selector[key] == value
    end
    options.reject! do |key, value|
      other.options[key] == value
    end
    other.inclusions.each do |meta|
      inclusions.delete_one(meta)
    end
  end
  self
end

#scoped(options = nil) ⇒ Criteria

Forces the criteria to be scoped, unless it’s 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:

Since:

  • 3.0.0



57
58
59
60
61
62
63
64
# File 'lib/mongoid/criterion/scoping.rb', line 57

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.

Since:

  • 3.0.0



74
75
76
# File 'lib/mongoid/criterion/scoping.rb', line 74

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.

Since:

  • 3.0.0



115
116
117
# File 'lib/mongoid/criterion/scoping.rb', line 115

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.

Since:

  • 3.0.0



129
130
131
# File 'lib/mongoid/criterion/scoping.rb', line 129

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:

Since:

  • 3.0.0



86
87
88
89
90
91
92
93
# File 'lib/mongoid/criterion/scoping.rb', line 86

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.

Since:

  • 3.0.0



103
104
105
# File 'lib/mongoid/criterion/scoping.rb', line 103

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:

Since:

  • 3.0.0



144
145
146
147
148
149
150
# File 'lib/mongoid/criterion/scoping.rb', line 144

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