Module: Mongoid::Scopable::ClassMethods
- Defined in:
- build/mongoid-6.1/lib/mongoid/scopable.rb
Overview
Instance Method Summary collapse
-
#default_scopable? ⇒ true, false
Is the class able to have the default scope applied?.
-
#default_scope(value = nil) ⇒ 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.
-
#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?
104 105 106 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 104 def default_scopable? default_scoping? && !Threaded.executing?(:without_default_scope) end |
#default_scope(value = nil) ⇒ Proc
Add a default scope to the model. This scope will be applied to all criteria unless #unscoped is specified.
90 91 92 93 94 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 90 def default_scope(value = nil) value = Proc.new { yield } if block_given? 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.
118 119 120 121 122 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 118 def queryable crit = Threaded.current_scope(self) || Criteria.new(self) crit. = true if (crit.klass. && !crit.klass.cyclic?) crit 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.
145 146 147 148 149 150 151 152 153 154 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 145 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.
173 174 175 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 173 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.
56 57 58 59 60 61 62 63 64 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 56 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.
193 194 195 196 197 198 199 200 201 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 193 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.
211 212 213 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 211 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.
227 228 229 230 231 232 233 234 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 227 def with_scope(criteria) Threaded.set_current_scope(criteria, self) begin yield criteria ensure Threaded.set_current_scope(nil, self) end end |
#without_default_scope ⇒ Object
Execute the block without applying the default scope.
246 247 248 249 250 251 |
# File 'build/mongoid-6.1/lib/mongoid/scopable.rb', line 246 def without_default_scope Threaded.begin_execution("without_default_scope") yield ensure Threaded.exit_execution("without_default_scope") end |