Module: Mongoid::Scopable::ClassMethods
- Defined in:
- lib/mongoid/scopable.rb
Instance Method Summary collapse
-
#default_scopable? ⇒ true | false
Is the class able to have the default scope applied?.
-
#default_scope(value = nil, &block) ⇒ 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 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.
Instance Method Details
#default_scopable? ⇒ true | false
Is the class able to have the default scope applied?
90 91 92 |
# File 'lib/mongoid/scopable.rb', line 90 def default_scopable? default_scoping? && !Threaded.without_default_scope?(self) end |
#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.
78 79 80 81 82 |
# 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 |
#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.
102 103 104 105 106 |
# 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) ⇒ Object
Create a scope that can be accessed from the class level or chained to criteria by the provided name.
127 128 129 130 131 132 133 134 135 136 |
# 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.
153 154 155 |
# 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.
48 49 50 51 52 53 54 |
# 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.
172 173 174 175 176 177 178 179 180 181 182 |
# 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.
190 191 192 |
# 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.
204 205 206 207 208 209 210 211 212 |
# 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.
222 223 224 225 226 227 |
# 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 |