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 73
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 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
#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) 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 93
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 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.
# 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