module Mongoid::Persistable::Creatable

def atomic_inserts

Returns:
  • (Hash) - The insert ops.

Other tags:
    Example: Get the inserts. -

Other tags:
    Api: - private
def atomic_inserts
  { atomic_insert_modifier => { atomic_position => as_attributes }}
end

def insert(options = {})

Returns:
  • (Document) - The persisted document.

Parameters:
  • options (Hash) -- Options to pass to insert.

Other tags:
    Example: Insert a document. -
def insert(options = {})
  prepare_insert(options) do
    if embedded?
      insert_as_embedded
    else
      insert_as_root
    end
  end
end

def insert_as_embedded

Returns:
  • (Document) - The document.

Other tags:
    Example: Insert the document as embedded. -

Other tags:
    Api: - private
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

def insert_as_root

Returns:
  • (Document) - The document.

Other tags:
    Example: Insert the document as root. -

Other tags:
    Api: - private
def insert_as_root
  collection.insert_one(as_attributes, session: _session)
end

def post_process_insert

Returns:
  • (true) - true.

Other tags:
    Example: Post process the insert. -

Other tags:
    Api: - private
def post_process_insert
  self.new_record = false
  remember_storage_options!
  flag_descendants_persisted
  true
end

def prepare_insert(options = {})

Returns:
  • (Document) - The document.

Parameters:
  • options (Hash) -- The options.

Other tags:
    Example: Prepare for insertion. -

Other tags:
    Api: - private
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