Module: Mongoid::Scopable::ClassMethods

Defined in:
lib/mongoid/scopable.rb

Instance Method Summary collapse

Instance Method Details

#default_scopable?true | false

Is the class able to have the default scope applied?

Examples:

Can the default scope be applied?

Band.default_scopable?

Returns:

  • (true | false)

    If the default scope can be applied.



95
96
97
# File 'lib/mongoid/scopable.rb', line 95

def default_scopable?
  default_scoping? && !Threaded.without_default_scope?(self)
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.

Examples:

Define a default scope with a criteria.

class Band
  include Mongoid::Document
  field :active, type: Boolean
  default_scope where(active: true)
end

Define a default scope with a proc.

class Band
  include Mongoid::Document
  field :active, type: Boolean
  default_scope ->{ where(active: true) }
end

Parameters:

  • value (Proc | Criteria) (defaults to: nil)

    The default scope.

Returns:

  • (Proc)

    The default scope.

Raises:



83
84
85
86
87
# File 'lib/mongoid/scopable.rb', line 83

def default_scope(value = nil)
  value = Proc.new { yield } if block_given?
  check_scope_validity(value)
  self.default_scoping = process_default_scope(value)
end

#queryableCriteria

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.

Examples:

Get a queryable.

Model.queryable

Returns:



107
108
109
110
111
# File 'lib/mongoid/scopable.rb', line 107

def queryable
  crit = Threaded.current_scope(self) || Criteria.new(self)
  crit.embedded = true if (crit.klass.embedded? && !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.

Examples:

Create named scopes.


class Person
  include Mongoid::Document
  field :active, type: Boolean
  field :count, type: Integer

  scope :active, -> { where(active: true) }
  scope :at_least, ->(count){ where(:count.gt => count) }
end

Parameters:

  • name (Symbol)

    The name of the scope.

  • value (Proc)

    The conditions of the scope.

Raises:



132
133
134
135
136
137
138
139
140
141
# File 'lib/mongoid/scopable.rb', line 132

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

Note:

This will force the default scope to be applied.

Get a criteria for the document with normal scoping.

Examples:

Get the criteria.

Band.scoped(skip: 10)

Parameters:

  • options (Hash) (defaults to: nil)

    Query options for the criteria.

Options Hash (options):

  • :skip (Integer)

    Optional number of documents to skip.

  • :limit (Integer)

    Optional number of documents to limit.

  • :sort (Array)

    Optional sorting options.

Returns:



158
159
160
# File 'lib/mongoid/scopable.rb', line 158

def scoped(options = nil)
  queryable.scoped(options)
end

#scopesHash

Returns a hash of all the scopes defined for this class, including scopes defined on ancestor classes.

Examples:

Get the defined scopes for a class

class Band
  include Mongoid::Document
  field :active, type: Boolean

  scope :active, -> { where(active: true) }
end
Band.scopes

Returns:

  • (Hash)

    The scopes defined for this class



51
52
53
54
55
56
57
58
59
# File 'lib/mongoid/scopable.rb', line 51

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

#unscopedCriteria | Object

Note:

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.

Examples:

Get the unscoped criteria.

Band.unscoped

Yield to block with no scoping.

Band.unscoped do
  Band.where(name: "Depeche Mode")
end

Returns:

  • (Criteria | Object)

    The unscoped criteria or result of the block.



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mongoid/scopable.rb', line 177

def unscoped
  if block_given?
    without_default_scope do
      with_scope(nil) do
        yield(self)
      end
    end
  else
    queryable.unscoped
  end
end

#with_default_scopeCriteria Also known as: criteria

Get a criteria with the default scope applied, if possible.

Examples:

Get a criteria with the default scope.

Model.with_default_scope

Returns:



195
196
197
# File 'lib/mongoid/scopable.rb', line 195

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.

Examples:

Yield to the criteria.

Person.with_scope(criteria)

Parameters:

  • criteria (Criteria)

    The criteria to apply.

Returns:



209
210
211
212
213
214
215
216
217
# File 'lib/mongoid/scopable.rb', line 209

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_scopeObject

Execute the block without applying the default scope.

Examples:

Execute without the default scope.

Band.without_default_scope do
  Band.where(name: "Depeche Mode")
end

Returns:

  • (Object)

    The result of the block.



227
228
229
230
231
232
# File 'lib/mongoid/scopable.rb', line 227

def without_default_scope
  Threaded.begin_without_default_scope(self)
  yield
ensure
  Threaded.exit_without_default_scope(self)
end