Module: Mongoid::Persistable::Creatable
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveSupport::Concern
|
|
Defined in: | lib/mongoid/persistable/creatable.rb |
Overview
Defines behavior for persistence operations that create new documents.
Instance Method Summary
-
#insert(options = {}) ⇒ Document
Insert a new document into the database.
-
#atomic_inserts ⇒ Hash
private
Internal use only
Internal use only
Get the atomic insert for embedded documents, either a push or set.
-
#insert_as_embedded ⇒ Document
private
Internal use only
Internal use only
Insert the embedded document.
-
#insert_as_root ⇒ Document
private
Internal use only
Internal use only
Insert the root document.
-
#post_process_insert ⇒ true
private
Internal use only
Internal use only
Post process an insert, which sets the new record attribute to false and flags all the children as persisted.
-
#prepare_insert(options = {}) ⇒ Document
private
Internal use only
Internal use only
Prepare the insert for execution.
Instance Method Details
#atomic_inserts ⇒ Hash (private)
Get the atomic insert for embedded documents, either a push or set.
# File 'lib/mongoid/persistable/creatable.rb', line 40
def atomic_inserts { atomic_insert_modifier => { atomic_position => as_attributes }} end
#insert(options = {}) ⇒ Document
Insert a new document into the database. Will return the document itself whether or not the save was successful.
# File 'lib/mongoid/persistable/creatable.rb', line 20
def insert( = {}) prepare_insert( ) do if else insert_as_root end end end
#insert_as_embedded ⇒ Document (private)
Insert the embedded document.
# File 'lib/mongoid/persistable/creatable.rb', line 52
def raise Errors::NoParent.new(self.class.name) unless _parent if _parent.new_record? _parent.insert else selector = _parent.atomic_selector _root.collection.find(selector).update_one( positionally(selector, atomic_inserts), session: _session) end end
#insert_as_root ⇒ Document (private)
Insert the root document.
# File 'lib/mongoid/persistable/creatable.rb', line 72
def insert_as_root collection.insert_one(as_attributes, session: _session) end
#post_process_insert ⇒ true
(private)
Post process an insert, which sets the new record attribute to false and flags all the children as persisted.
# File 'lib/mongoid/persistable/creatable.rb', line 85
def post_process_insert self.new_record = false flag_descendants_persisted true end
#prepare_insert(options = {}) ⇒ Document (private)
Prepare the insert for execution. Validates and runs callbacks, etc.
# File 'lib/mongoid/persistable/creatable.rb', line 104
def prepare_insert( = {}) raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly return self if performing_validations?( ) && invalid?( [:context] || :create) ensure_client_compatibility! run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do run_callbacks(:save, with_children: false) do run_callbacks(:create, with_children: false) do run_callbacks(:persist_parent, with_children: false) do _mongoid_run_child_callbacks(:save) do _mongoid_run_child_callbacks(:create) do result = yield(self) if !result.is_a?(Document) || result.errors.empty? post_process_insert post_process_persist(result, ) end end end end end end end self end