Module: Mongoid::Criteria::Scopable
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
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 Attribute Summary
-
#scoped? ⇒ true | false
readonly
Has the criteria had the default scope applied?
-
#scoping_options ⇒ Array
rw
Get the criteria scoping options, as a pair (scoped, unscoped).
-
#scoping_options=(options) ⇒ Array
rw
::Set
the criteria scoping options, as a pair (scoped, unscoped). -
#unscoped ⇒ Criteria
readonly
Clears all scoping from the criteria.
-
#unscoped? ⇒ true | false
readonly
Is the criteria unscoped?
Instance Method Summary
-
#apply_default_scope ⇒ Criteria
Applies the default scope to the criteria.
-
#apply_scope(scope) ⇒ Criteria
Internal use only
Internal use only
Applies a scope to the current criteria.
-
#remove_scoping(other) ⇒ Criteria
Given another criteria, remove the other criteria’s scoping from this criteria.
-
#scoped(options = nil) ⇒ Criteria
readonly
Forces the criteria to be scoped, unless its inside an unscoped block.
-
#with_default_scope ⇒ Criteria
Get the criteria with the default scope applied, if the default scope is able to be applied.
- #reject_matching(other, *methods) private
Instance Attribute Details
#scoped? ⇒ true
| false
(readonly)
Has the criteria had the default scope applied?
# File 'lib/mongoid/criteria/scopable.rb', line 92
def scoped? !!(defined?(@scoped) ? @scoped : nil) end
#scoping_options ⇒ Array (rw)
Get the criteria scoping options, as a pair (scoped, unscoped).
# File 'lib/mongoid/criteria/scopable.rb', line 127
def [ (defined?(@scoped) ? @scoped : nil), (defined?(@unscoped) ? @unscoped : nil) ] end
#scoping_options=(options) ⇒ Array (rw)
::Set
the criteria scoping options, as a pair (scoped, unscoped).
# File 'lib/mongoid/criteria/scopable.rb', line 139
def ( ) @scoped, @unscoped = end
#unscoped ⇒ Criteria (readonly)
Clears all scoping from the criteria.
# File 'lib/mongoid/criteria/scopable.rb', line 102
def unscoped crit = clone unless unscoped? crit. = false, true crit.selector.clear; crit. .clear end crit end
#unscoped? ⇒ true
| false
(readonly)
Is the criteria unscoped?
# File 'lib/mongoid/criteria/scopable.rb', line 117
def unscoped? !!(defined?(@unscoped) ? @unscoped : nil) end
Instance Method Details
#apply_default_scope ⇒ Criteria
Applies the default scope to the criteria.
#apply_scope(scope) ⇒ Criteria
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.
#reject_matching(other, *methods) (private)
[ GitHub ]# File 'lib/mongoid/criteria/scopable.rb', line 162
def reject_matching(other, *methods) methods.each do |method| send(method).reject! do |key, value| other.send(method).has_key?(key) && other.send(method)[key] == value end end end
#remove_scoping(other) ⇒ Criteria
Given another criteria, remove the other criteria’s scoping from this criteria.
# File 'lib/mongoid/criteria/scopable.rb', line 59
def remove_scoping(other) if other reject_matching(other, :selector, : ) other.inclusions.each do || inclusions.delete_one( ) end end self end
#scoped(options = nil) ⇒ Criteria (readonly)
Forces the criteria to be scoped, unless its inside an unscoped block.
# File 'lib/mongoid/criteria/scopable.rb', line 77
def scoped( = nil) crit = clone crit. .merge!( || {}) if klass.default_scopable? && !scoped? crit.apply_default_scope end crit end
#with_default_scope ⇒ Criteria
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.
# 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