Module: Mongoid::NamedScope::ClassMethods
- Defined in:
- lib/mongoid/named_scope.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#criteria(embedded = false, scoped = true) ⇒ Criteria
Gets either the last scope on the stack or creates a new criteria.
-
#scope(name, conditions = {}, &block) ⇒ Object
(also: #named_scope)
Creates a named_scope for the
Document
, similar to ActiveRecord’s named_scopes. -
#scope_stack ⇒ Array<Criteria>
Initializes and returns the current scope stack.
-
#scoped(embedded = false) ⇒ Criteria
Get a criteria object for the class, scoped to the default if defined.
-
#unscoped(embedded = false) ⇒ Criteria
Get a criteria object for the class, ignoring default scoping.
-
#with_scope(criteria) ⇒ Criteria
Pushes the provided criteria onto the scope stack, and removes it after the provided block is yielded.
Instance Method Details
#criteria(embedded = false, scoped = true) ⇒ Criteria
Gets either the last scope on the stack or creates a new criteria.
26 27 28 29 30 |
# File 'lib/mongoid/named_scope.rb', line 26 def criteria( = false, scoped = true) scope_stack.last || Criteria.new(self, ).tap do |crit| return crit.fuse(default_scoping) if default_scoping && scoped end end |
#scope(name, conditions = {}, &block) ⇒ Object Also known as: named_scope
Creates a named_scope for the Document
, similar to ActiveRecord’s named_scopes. NamedScopes
are proxied Criteria
objects that can be chained.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mongoid/named_scope.rb', line 52 def scope(name, conditions = {}, &block) name = name.to_sym valid_scope_name?(name) scopes[name] = Scope.new(conditions, &block) (class << self; self; end).class_eval <<-EOT def #{name}(*args) scope = scopes[:#{name}] scope.extend(criteria.fuse(scope.conditions.scoped(*args))) end EOT end |
#scope_stack ⇒ Array<Criteria>
Initializes and returns the current scope stack.
87 88 89 |
# File 'lib/mongoid/named_scope.rb', line 87 def scope_stack Threaded.scope_stack[object_id] ||= [] end |
#scoped(embedded = false) ⇒ Criteria
Get a criteria object for the class, scoped to the default if defined.
75 76 77 |
# File 'lib/mongoid/named_scope.rb', line 75 def scoped( = false) criteria(, true) end |
#unscoped(embedded = false) ⇒ Criteria
Get a criteria object for the class, ignoring default scoping.
101 102 103 |
# File 'lib/mongoid/named_scope.rb', line 101 def unscoped( = false) criteria(, false) end |
#with_scope(criteria) ⇒ Criteria
Pushes the provided criteria onto the scope stack, and removes it after the provided block is yielded.
116 117 118 119 120 121 122 123 124 |
# File 'lib/mongoid/named_scope.rb', line 116 def with_scope(criteria) scope_stack = self.scope_stack scope_stack << criteria begin yield criteria ensure scope_stack.pop end end |