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.
-
#inherited(subclass) ⇒ Object
When inheriting, we want to copy the scopes from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.
-
#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.apply_default_scope if scoped end end |
#inherited(subclass) ⇒ Object
When inheriting, we want to copy the scopes from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.
137 138 139 140 |
# File 'lib/mongoid/named_scope.rb', line 137 def inherited(subclass) super subclass.scopes = scopes.dup 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 63 |
# 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}] conditions = scope.conditions.as_conditions(*args) scope.extend(criteria.fuse(conditions)) end EOT end |
#scope_stack ⇒ Array<Criteria>
Initializes and returns the current scope stack.
88 89 90 |
# File 'lib/mongoid/named_scope.rb', line 88 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.
76 77 78 |
# File 'lib/mongoid/named_scope.rb', line 76 def scoped( = false) criteria().scoped end |
#unscoped(embedded = false) ⇒ Criteria
Get a criteria object for the class, ignoring default scoping.
102 103 104 |
# File 'lib/mongoid/named_scope.rb', line 102 def unscoped( = false) criteria().unscoped end |
#with_scope(criteria) ⇒ Criteria
Pushes the provided criteria onto the scope stack, and removes it after the provided block is yielded.
117 118 119 120 121 122 123 124 125 |
# File 'lib/mongoid/named_scope.rb', line 117 def with_scope(criteria) scope_stack = self.scope_stack scope_stack << criteria begin yield criteria ensure scope_stack.pop end end |