Module: Mongoid::Document::ClassMethods

Defined in:
lib/mongoid/document.rb

Overview

Class-level methods for Document objects.

Instance Method Summary collapse

Instance Method Details

#_mongoid_clear_typesObject

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.

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

Examples:

Get the types.

document._mongoid_clear_types


461
462
463
464
# File 'lib/mongoid/document.rb', line 461

def _mongoid_clear_types
  @_types = nil
  superclass._mongoid_clear_types if hereditary?
end

#_typesArray<Class>

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

Examples:

Get the types.

document._types

Returns:

  • (Array<Class>)

    All subclasses of the current document.



450
451
452
# File 'lib/mongoid/document.rb', line 450

def _types
  @_types ||= (descendants + [ self ]).uniq.map(&:discriminator_value)
end

#construct_document(attrs = nil, options = {}) ⇒ Document

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.

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 bugs.ruby-lang.org/issues/15753

Allocates and constructs a document.

Parameters:

  • attrs (Hash) (defaults to: nil)

    The attributes to set up the document with.

  • options (Hash) (defaults to: {})

    The options to use.

Options Hash (options):

  • :execute_callbacks (true | false)

    Flag specifies whether callbacks should be run.

Returns:



439
440
441
442
# File 'lib/mongoid/document.rb', line 439

def construct_document(attrs = nil, options = {})
  execute_callbacks = options.fetch(:execute_callbacks, Threaded.execute_callbacks?)
  with_callbacks(execute_callbacks) { new(attrs) }
end

#i18n_scopeSymbol

Set the i18n scope to overwrite ActiveModel.

Returns:

  • (Symbol)

    :mongoid



469
470
471
# File 'lib/mongoid/document.rb', line 469

def i18n_scope
  :mongoid
end

#instantiate(attrs = nil, selected_fields = nil, &block) ⇒ Document

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

Examples:

Create the document.

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

Parameters:

  • attrs (Hash) (defaults to: nil)

    The hash of attributes to instantiate with.

  • selected_fields (Integer) (defaults to: nil)

    The selected fields from the criteria.

Returns:



384
385
386
# File 'lib/mongoid/document.rb', line 384

def instantiate(attrs = nil, selected_fields = nil, &block)
  instantiate_document(attrs, selected_fields, &block)
end

#instantiate_document(attrs = nil, selected_fields = nil, options = {}) {|Mongoid::Document| ... } ⇒ Document

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.

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.

Instantiate the document.

Parameters:

  • attrs (Hash) (defaults to: nil)

    The hash of attributes to instantiate with.

  • selected_fields (Integer) (defaults to: nil)

    The selected fields from the criteria.

  • options (Hash) (defaults to: {})

    The options to use.

Options Hash (options):

  • :execute_callbacks (true | false)

    Flag specifies whether callbacks should be run.

Yields:

  • (Mongoid::Document)

    If a block is given, yields the newly instantiated document to it.

Returns:



408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/mongoid/document.rb', line 408

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

#loggerLogger

Returns the logger

Examples:

Get the logger.

Person.logger

Returns:

  • (Logger)

    The configured logger or a default Logger instance.



479
480
481
# File 'lib/mongoid/document.rb', line 479

def logger
  Mongoid.logger
end

#with_callbacks(execute_callbacks) ⇒ Object

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.

Parameters:

  • execute_callbacks (true | false)

    Whether callbacks should be suppressed or not.



365
366
367
368
369
370
371
# File 'lib/mongoid/document.rb', line 365

def with_callbacks(execute_callbacks)
  saved, Threaded.execute_callbacks =
    Threaded.execute_callbacks?, execute_callbacks
  yield
ensure
  Threaded.execute_callbacks = saved
end