Module: Mongoid::Scoping::ClassMethods
- Defined in:
- lib/mongoid/scoping.rb
Instance Method Summary collapse
-
#default_scopable? ⇒ true, false
Is the class able to have the default scope applied?.
-
#default_scope(value) ⇒ Proc
Add a default scope to the model.
-
#queryable ⇒ Criteria
private
Get a queryable, either the last one on the scope stack or a fresh one.
-
#scope(name, value, &block) ⇒ Object
Create a scope that can be accessed from the class level or chained to criteria by the provided name.
-
#scope_stack ⇒ Array<Criteria>
Initializes and returns the current scope stack.
-
#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 the default 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.
Instance Method Details
#default_scopable? ⇒ true, false
Is the class able to have the default scope applied?
79 80 81 |
# File 'lib/mongoid/scoping.rb', line 79 def default_scopable? default_scoping? && !Threaded.executing?(:without_default_scope) end |
#default_scope(value) ⇒ Proc
Add a default scope to the model. This scope will be applied to all criteria unless #unscoped is specified.
66 67 68 69 |
# File 'lib/mongoid/scoping.rb', line 66 def default_scope(value) check_scope_validity(value) self.default_scoping = process_default_scope(value) end |
#queryable ⇒ Criteria
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get a queryable, either the last one on the scope stack or a fresh one.
93 94 95 |
# File 'lib/mongoid/scoping.rb', line 93 def queryable scope_stack.last || Criteria.new(self) end |
#scope(name, value, &block) ⇒ Object
Create a scope that can be accessed from the class level or chained to criteria by the provided name.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mongoid/scoping.rb', line 118 def scope(name, value, &block) normalized = name.to_sym check_scope_validity(value) check_scope_name(normalized) _declared_scopes[normalized] = { scope: strip_default_scope(value), extension: Module.new(&block) } define_scope_method(normalized) end |
#scope_stack ⇒ Array<Criteria>
Initializes and returns the current scope stack.
137 138 139 |
# File 'lib/mongoid/scoping.rb', line 137 def scope_stack Threaded.scope_stack[object_id] ||= [] end |
#scoped(options = nil) ⇒ Criteria
This will force the default scope to be applied.
Get a criteria for the document with normal scoping.
158 159 160 |
# File 'lib/mongoid/scoping.rb', line 158 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.
32 33 34 35 36 37 38 39 40 |
# File 'lib/mongoid/scoping.rb', line 32 def scopes defined_scopes = {} ancestors.reverse.each do |klass| if klass.respond_to?(:_declared_scopes) defined_scopes.merge!(klass._declared_scopes) end end defined_scopes.freeze end |
#unscoped ⇒ Criteria, Object
This will force the default scope to be removed.
Get the criteria without the default scoping applied.
178 179 180 181 182 183 184 185 186 |
# File 'lib/mongoid/scoping.rb', line 178 def unscoped if block_given? without_default_scope do yield(self) end else queryable.unscoped end end |
#with_default_scope ⇒ Criteria Also known as: criteria
Get a criteria with the default scope applied, if possible.
196 197 198 |
# File 'lib/mongoid/scoping.rb', line 196 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.
212 213 214 215 216 217 218 219 |
# File 'lib/mongoid/scoping.rb', line 212 def with_scope(criteria) scope_stack.push(criteria) begin yield criteria ensure scope_stack.pop end end |
#without_default_scope ⇒ Object
Execute the block without applying the default scope.
231 232 233 234 235 236 |
# File 'lib/mongoid/scoping.rb', line 231 def without_default_scope Threaded.begin_execution("without_default_scope") yield ensure Threaded.exit_execution("without_default_scope") end |