Module: Mongoid::Document::ClassMethods

Defined in:
lib/mongoid/document.rb

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


371
372
373
374
# File 'lib/mongoid/document.rb', line 371

def _mongoid_clear_types
  @_type = 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.



360
361
362
# File 'lib/mongoid/document.rb', line 360

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

#construct_document(attrs = nil, execute_callbacks: true) ⇒ 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.

Allocates and constructs a document.

Parameters:

  • attrs (Hash) (defaults to: nil)

    The attributes to set up the document with.

  • execute_callbacks (true | false) (defaults to: true)

    Flag specifies whether callbacks should be run.

Returns:



349
350
351
352
# File 'lib/mongoid/document.rb', line 349

def construct_document(attrs = nil, execute_callbacks: true)
  doc = allocate
  doc.send(:construct_document, attrs, execute_callbacks: execute_callbacks)
end

#i18n_scopeSymbol

Set the i18n scope to overwrite ActiveModel.

Returns:

  • (Symbol)

    :mongoid



379
380
381
# File 'lib/mongoid/document.rb', line 379

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.

  • execute_callbacks (true | false)

    Flag specifies whether callbacks should be run.

Returns:



297
298
299
# File 'lib/mongoid/document.rb', line 297

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

#instantiate_document(attrs = nil, selected_fields = nil, execute_callbacks: true) ⇒ 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.

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.

  • execute_callbacks (true | false) (defaults to: true)

    Flag specifies whether callbacks should be run.

Returns:



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/mongoid/document.rb', line 312

def instantiate_document(attrs = nil, selected_fields = nil, execute_callbacks: true)
  attributes = if Mongoid.legacy_attributes
    attrs
  else
    attrs&.to_h
  end || {}

  doc = allocate
  doc.__selected_fields = selected_fields
  doc.instance_variable_set(:@attributes, attributes)
  # TODO: remove the to_h when the legacy_attributes flag is removed.
  # The to_h ensures that we don't accidentally make attributes_before_type_cast
  # a BSON::Document.
  doc.instance_variable_set(:@attributes_before_type_cast, attributes&.to_h.dup)

  if execute_callbacks
    doc.apply_defaults
    yield(doc) if block_given?
    doc.run_callbacks(:find) unless doc._find_callbacks.empty?
    doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty?
  else
    yield(doc) if block_given?
    doc.pending_callbacks += [:apply_defaults, :find, :initialize]
  end

  doc
end

#loggerLogger

Returns the logger

Examples:

Get the logger.

Person.logger

Returns:

  • (Logger)

    The configured logger or a default Logger instance.



389
390
391
# File 'lib/mongoid/document.rb', line 389

def logger
  Mongoid.logger
end