123456789_123456789_123456789_123456789_123456789_

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

Instance Method Details

#atomic_insertsHash (private)

This method is for internal use only.

Get the atomic insert for embedded documents, either a push or set.

Examples:

Get the inserts.

document.inserts

Returns:

  • (Hash)

    The insert ops.

[ GitHub ]

  
# 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.

Examples:

Insert a document.

document.insert

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to insert.

Returns:

  • (Document)

    The persisted document.

[ GitHub ]

  
# File 'lib/mongoid/persistable/creatable.rb', line 20

def insert(options = {})
  prepare_insert(options) do
    if embedded?
      insert_as_embedded
    else
      insert_as_root
    end
  end
end

#insert_as_embeddedDocument (private)

This method is for internal use only.

Insert the embedded document.

Examples:

Insert the document as embedded.

document.insert_as_embedded

Returns:

Raises:

[ GitHub ]

  
# File 'lib/mongoid/persistable/creatable.rb', line 52

def insert_as_embedded
  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_rootDocument (private)

This method is for internal use only.

Insert the root document.

Examples:

Insert the document as root.

document.insert_as_root

Returns:

[ GitHub ]

  
# File 'lib/mongoid/persistable/creatable.rb', line 72

def insert_as_root
  collection.insert_one(as_attributes, session: _session)
end

#post_process_inserttrue (private)

This method is for internal use only.

Post process an insert, which sets the new record attribute to false and flags all the children as persisted.

Examples:

Post process the insert.

document.post_process_insert

Returns:

  • (true)

    true.

[ GitHub ]

  
# File 'lib/mongoid/persistable/creatable.rb', line 85

def post_process_insert
  self.new_record = false
  remember_storage_options!
  flag_descendants_persisted
  true
end

#prepare_insert(options = {}) ⇒ Document (private)

This method is for internal use only.

Prepare the insert for execution. Validates and runs callbacks, etc.

Examples:

Prepare for insertion.

document.prepare_insert do
  collection.insert(as_document)
end

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Returns:

Raises:

[ GitHub ]

  
# File 'lib/mongoid/persistable/creatable.rb', line 104

def prepare_insert(options = {})
  raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly
  return self if performing_validations?(options) &&
    invalid?(options[: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, options)
              end
            end
          end
        end
      end
    end
  end
  self
end