Module: Mongoid::Scopable::ClassMethods
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Extended In:
| |
| Defined in: | lib/mongoid/scopable.rb |
Instance Attribute Summary
-
#default_scopable? ⇒ true | false
readonly
Is the class able to have the default scope applied?
Instance Method Summary
-
#criteria
Alias for #with_default_scope.
-
#default_scope(value = nil, &block) ⇒ Proc
Add a default scope to the model.
-
#queryable ⇒ Criteria
Internal use only
Internal use only
Get a queryable, either the last one on the scope stack or a fresh one.
-
#scope(name, value, &block)
Create a scope that can be accessed from the class level or chained to criteria by the provided name.
-
#scoped(options = nil) ⇒ Criteria
Get a criteria for the document with normal scoping.
-
#scopes ⇒ Hash
Returns a hash of all the scopes defined for this class, including scopes defined on ancestor classes.
-
#unscoped ⇒ Criteria | Object
Get the criteria without any scoping applied.
-
#with_default_scope ⇒ Criteria
(also: #criteria)
Get a criteria with the default scope applied, if possible.
-
#with_scope(criteria) ⇒ Criteria
Pushes the provided criteria onto the scope stack, and removes it after the provided block is yielded.
-
#without_default_scope ⇒ Object
Execute the block without applying the default scope.
-
#check_scope_name(name)
private
Internal use only
Internal use only
Warns or raises exception if overriding another scope or method.
-
#check_scope_validity(value)
private
Internal use only
Internal use only
Checks if the intended scope is a valid object, either a criteria or proc with a criteria.
-
#define_scope_method(name) ⇒ Method
private
Internal use only
Internal use only
Defines the actual class method that will execute the scope when called.
-
#process_default_scope(value)
private
Internal use only
Internal use only
Process the default scope value.
Instance Attribute Details
#default_scopable? ⇒ true | false (readonly)
Is the class able to have the default scope applied?
# File 'lib/mongoid/scopable.rb', line 90
def default_scopable? default_scoping? && !Threaded.without_default_scope?(self) end
Instance Method Details
#check_scope_name(name) (private)
Warns or raises exception if overriding another scope or method.
# File 'lib/mongoid/scopable.rb', line 242
def check_scope_name(name) return unless _declared_scopes[name] || respond_to?(name, true) raise Errors::ScopeOverwrite.new(self.name, name) if Mongoid.scope_overwrite_exception Mongoid.logger.warn( "Creating scope :#{name} which conflicts with #{self.name}.#{name}. " + "Calls to `Mongoid::Criteria##{name}` will delegate to " + "`Mongoid::Criteria##{name}` for criteria with klass #{self.name} " + 'and will ignore the declared scope.' ) end
#check_scope_validity(value) (private)
Checks if the intended scope is a valid object, either a criteria or proc with a criteria.
# File 'lib/mongoid/scopable.rb', line 265
def check_scope_validity(value) return if value.respond_to?(:call) raise Errors::InvalidScope.new(self, value) end
#criteria
Alias for #with_default_scope.
# File 'lib/mongoid/scopable.rb', line 193
alias criteria with_default_scope
#default_scope(value = nil, &block) ⇒ Proc
Add a default scope to the model. This scope will be applied to all criteria unless #unscoped is specified.
# File 'lib/mongoid/scopable.rb', line 78
def default_scope(value = nil, &block) value = proc(&block) if block_given? check_scope_validity(value) self.default_scoping = process_default_scope(value) end
#define_scope_method(name) ⇒ Method (private)
Defines the actual class method that will execute the scope when called.
# File 'lib/mongoid/scopable.rb', line 282
def define_scope_method(name) singleton_class.class_eval do define_method(name) do |*args, **kwargs| scoping = _declared_scopes[name] scope = instance_exec(*args, **kwargs, &scoping[:scope]) extension = scoping[:extension] to_merge = scope || queryable criteria = if Mongoid.allow_scopes_to_unset_default_scope to_merge else to_merge.empty_and_chainable? ? to_merge : with_default_scope.merge(to_merge) end criteria.extend(extension) criteria end end end
#process_default_scope(value) (private)
Process the default scope value. If one already exists, we merge the new one into the old one.
# File 'lib/mongoid/scopable.rb', line 311
def process_default_scope(value) if existing = default_scoping -> { existing.call.merge(value.to_proc.call) } else value.to_proc end end
#queryable ⇒ Criteria
Get a queryable, either the last one on the scope stack or a fresh one.
# File 'lib/mongoid/scopable.rb', line 102
def queryable crit = Threaded.current_scope(self) || Criteria.new(self) crit. = true if crit.klass. && !crit.klass.cyclic? crit end
#scope(name, value, &block)
Create a scope that can be accessed from the class level or chained to criteria by the provided name.
# File 'lib/mongoid/scopable.rb', line 127
def scope(name, value, &block) normalized = name.to_sym check_scope_validity(value) check_scope_name(normalized) _declared_scopes[normalized] = { scope: value, extension: Module.new(&block) } define_scope_method(normalized) end
#scoped(options = nil) ⇒ Criteria
This will force the default scope to be applied.
Get a criteria for the document with normal scoping.
# File 'lib/mongoid/scopable.rb', line 153
def scoped( = nil) queryable.scoped() end
#scopes ⇒ Hash
Returns a hash of all the scopes defined for this class, including scopes defined on ancestor classes.
# File 'lib/mongoid/scopable.rb', line 48
def scopes defined_scopes = {} ancestors.reverse_each do |klass| defined_scopes.merge!(klass._declared_scopes) if klass.respond_to?(:_declared_scopes) end defined_scopes.freeze end
#unscoped ⇒ Criteria | Object
This will force the default scope, as well as any scope applied using “.with_scope“, to be removed.
Get the criteria without any scoping applied.
# File 'lib/mongoid/scopable.rb', line 172
def unscoped if block_given? without_default_scope do with_scope(nil) do yield(self) end end else queryable.unscoped end end
#with_default_scope ⇒ Criteria Also known as: #criteria
Get a criteria with the default scope applied, if possible.
# File 'lib/mongoid/scopable.rb', line 190
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.
# File 'lib/mongoid/scopable.rb', line 204
def with_scope(criteria) previous = Threaded.current_scope(self) Threaded.set_current_scope(criteria, self) begin yield criteria ensure Threaded.set_current_scope(previous, self) end end
#without_default_scope ⇒ Object
Execute the block without applying the default scope.
# File 'lib/mongoid/scopable.rb', line 222
def without_default_scope Threaded.begin_without_default_scope(self) yield ensure Threaded.exit_without_default_scope(self) end