module Mongoid::Document::ClassMethods

Class-level methods for Document objects.

Public Instance Methods

_mongoid_clear_types() click to toggle source

Clear the @_type cache. This is generally called when changing the discriminator key/value on a class.

@example Get the types.

document._mongoid_clear_types

@api private

# File lib/mongoid/document.rb, line 463
def _mongoid_clear_types
  @_types = nil
  superclass._mongoid_clear_types if hereditary?
end
_types() click to toggle source

Returns all types to query for when using this class as the base.

@example Get the types.

document._types

@return [ Array<Class> ] All subclasses of the current document.

# File lib/mongoid/document.rb, line 452
def _types
  @_types ||= (descendants + [ self ]).uniq.map(&:discriminator_value)
end
construct_document(attrs = nil, options = {}) click to toggle source

Allocates and constructs a document.

@param [ Hash ] attrs The attributes to set up the document with. @param [ Hash ] options The options to use.

@option options [ true | false ] :execute_callbacks Flag specifies

whether callbacks should be run.

@note A Ruby 2.x bug prevents the options hash from being keyword

arguments. Once we drop support for Ruby 2.x, we can reimplement
the options hash as keyword arguments.
See https://bugs.ruby-lang.org/issues/15753

@return [ Document ] A new document.

@api private

# File lib/mongoid/document.rb, line 441
def construct_document(attrs = nil, options = {})
  execute_callbacks = options.fetch(:execute_callbacks, Threaded.execute_callbacks?)
  with_callbacks(execute_callbacks) { new(attrs) }
end
i18n_scope() click to toggle source

Set the i18n scope to overwrite ActiveModel.

@return [ Symbol ] :mongoid

# File lib/mongoid/document.rb, line 471
def i18n_scope
  :mongoid
end
instantiate(attrs = nil, selected_fields = nil, &block) click to toggle source

Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.

@example Create the document.

Person.instantiate(:title => 'Sir', :age => 30)

@param [ Hash ] attrs The hash of attributes to instantiate with. @param [ Integer ] selected_fields The selected fields from the

criteria.

@return [ Document ] A new document.

# File lib/mongoid/document.rb, line 386
def instantiate(attrs = nil, selected_fields = nil, &block)
  instantiate_document(attrs, selected_fields, &block)
end
instantiate_document(attrs = nil, selected_fields = nil, options = {}, &block) click to toggle source

Instantiate the document.

@param [ Hash ] attrs The hash of attributes to instantiate with. @param [ Integer ] selected_fields The selected fields from the

criteria.

@param [ Hash ] options The options to use.

@option options [ true | false ] :execute_callbacks Flag specifies

whether callbacks should be run.

@yield [ Mongoid::Document ] If a block is given, yields the newly

instantiated document to it.

@return [ Document ] A new document.

@note A Ruby 2.x bug prevents the options hash from being keyword

arguments. Once we drop support for Ruby 2.x, we can reimplement
the options hash as keyword arguments.

@api private

# File lib/mongoid/document.rb, line 410
def instantiate_document(attrs = nil, selected_fields = nil, options = {}, &block)
  execute_callbacks = options.fetch(:execute_callbacks, Threaded.execute_callbacks?)
  attributes = attrs&.to_h || {}

  doc = allocate
  doc.__selected_fields = selected_fields
  doc.instance_variable_set(:@attributes, attributes)
  doc.instance_variable_set(:@attributes_before_type_cast, attributes.dup)

  doc._handle_callbacks_after_instantiation(execute_callbacks, &block)

  doc.remember_storage_options!
  doc
end
logger() click to toggle source

Returns the logger

@example Get the logger.

Person.logger

@return [ Logger ] The configured logger or a default Logger instance.

# File lib/mongoid/document.rb, line 481
def logger
  Mongoid.logger
end
with_callbacks(execute_callbacks) { || ... } click to toggle source

Indicate whether callbacks should be invoked by default or not, within the block. Callbacks may always be explicitly invoked by passing ‘execute_callbacks: true` where available.

@param [ true | false ] execute_callbacks Whether callbacks should be

suppressed or not.
# File lib/mongoid/document.rb, line 367
def with_callbacks(execute_callbacks)
  saved, Threaded.execute_callbacks =
    Threaded.execute_callbacks?, execute_callbacks
  yield
ensure
  Threaded.execute_callbacks = saved
end