123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Reloadable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/reloadable.rb

Overview

This module handles reloading behavior of documents.

Instance Method Summary

Instance Method Details

#_reloadHash (private)

Reload the document, determining if it’s embedded or not and what behavior to use.

Examples:

Reload the document.

document._reload

Returns:

  • (Hash)

    The reloaded attributes.

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 73

def _reload
  embedded? ? reload_embedded_document : reload_root_document
end

#check_for_deleted_document!(attributes) (private)

Checks to see if the given attributes argument indicates that the object has been deleted. If the attributes are nil or an empty ::Hash, then we assume it has been deleted.

If Mongoid.raise_not_found_error is false, this will do nothing.

Parameters:

  • attributes (Hash | nil)

    The attributes hash retrieved from the database

Raises:

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 58

def check_for_deleted_document!(attributes)
  return unless Mongoid.raise_not_found_error
  return unless attributes.nil? || attributes.empty?

  shard_keys = atomic_selector.with_indifferent_access.slice(*shard_key_fields, :_id)
  raise Errors::DocumentNotFound.new(self.class, _id, shard_keys)
end

#reloadDocument

Reloads the Document attributes from the database. If the document has not been saved then an error will get raised if the configuration option was set. This can reload root documents or embedded documents.

Examples:

Reload the document.

person.reload

Returns:

  • (Document)

    The document, reloaded.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 16

def reload
  reloaded = _reload
  check_for_deleted_document!(reloaded)

  reset_object!(reloaded)

  run_callbacks(:find) unless _find_callbacks.empty?
  run_callbacks(:initialize) unless _initialize_callbacks.empty?
  self
end

#reload_embedded_documentHash (private)

Reload the embedded document.

Examples:

Reload the document.

document.reload_embedded_document

Returns:

  • (Hash)

    The reloaded attributes.

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 93

def reload_embedded_document
  Mongoid::Attributes::Embedded.traverse(
    collection(_root).find(_root.atomic_selector, session: _session).read(mode: :primary).first,
    atomic_position
  )
end

#reload_root_documentHash (private)

Reload the root document.

Examples:

Reload the document.

document.reload_root_document

Returns:

  • (Hash)

    The reloaded attributes.

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 83

def reload_root_document
  {}.merge(collection.find(atomic_selector, session: _session).read(mode: :primary).first || {})
end

#reset_object!(attributes) (private)

Resets the current object using the given attributes.

Parameters:

  • attributes (Hash)

    The attributes to use to replace the current attributes hash.

[ GitHub ]

  
# File 'lib/mongoid/reloadable.rb', line 33

def reset_object!(attributes)
  reset_atomic_updates!

  @attributes = attributes
  @attributes_before_type_cast = @attributes.dup
  @changed_attributes = {}
  @previous_changes = {}
  @previous_attributes = {}
  @previously_new_record = false

  reset_readonly
  apply_defaults
  reload_relations
end