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
-
#reload ⇒ Document
Reloads the
Document
attributes from the database. -
#_reload ⇒ Hash
private
Reload the document, determining if it’s embedded or not and what behavior to use.
-
#check_for_deleted_document!(attributes)
private
Checks to see if the given attributes argument indicates that the object has been deleted.
-
#reload_embedded_document ⇒ Hash
private
Reload the embedded document.
-
#reload_root_document ⇒ Hash
private
Reload the root document.
-
#reset_object!(attributes)
private
Resets the current object using the given attributes.
Instance Method Details
#_reload ⇒ Hash (private)
Reload the document, determining if it’s embedded or not and what behavior to use.
# File 'lib/mongoid/reloadable.rb', line 79
def _reload ? : 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.
# File 'lib/mongoid/reloadable.rb', line 64
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
#reload ⇒ Document
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.
# File 'lib/mongoid/reloadable.rb', line 16
def reload reloaded = _reload check_for_deleted_document!(reloaded) # In an instance where we create a new document, but set the ID to an existing one, # when the document is reloaded, we want to set new_record to false. # This is necessary otherwise saving will fail, as it will try to insert the document, # instead of attempting to update the existing document. @new_record = false unless reloaded.nil? || reloaded.empty? reset_object!(reloaded) run_callbacks(:find) unless _find_callbacks.empty? run_callbacks(:initialize) unless _initialize_callbacks.empty? self end
#reload_embedded_document ⇒ Hash (private)
Reload the embedded document.
# File 'lib/mongoid/reloadable.rb', line 99
def Mongoid::Attributes::Embedded.traverse( collection(_root).find(_root.atomic_selector, session: _session).read(mode: :primary).first, atomic_position ) end
#reload_root_document ⇒ Hash (private)
Reload the root document.
# File 'lib/mongoid/reloadable.rb', line 89
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.
# File 'lib/mongoid/reloadable.rb', line 39
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