Sha256: afc862c0e2939ac518a798f6a74afe1e3c8877152e1f67d9a6f44821819847e1

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8
module Mongoid
  module Persistence
    module Operations
      module Embedded

        # Insert is a persistence command responsible for taking a document that
        # has not been saved to the database and saving it. This specific class
        # handles the case when the document is embedded in another.
        #
        # The underlying query resembles the following MongoDB query:
        #
        #   collection.update(
        #     { "_id" : 1 },
        #     { "$push" : { "field" : "value" } },
        #     false
        #   );
        class Insert
          include Insertion
          include Operations
          include Mongoid::Atomic::Positionable

          # Insert the new document in the database. If the document's parent is a
          # new record, we will call save on the parent, otherwise we will $push
          # the document onto the parent.
          #
          # @example Insert an embedded document.
          #   Insert.persist
          #
          # @return [ Document ] The document to be inserted.
          def persist
            prepare do
              raise Errors::NoParent.new(document.class.name) unless parent
              if parent.new_record?
                parent.insert
              else
                selector = parent.atomic_selector
                collection.find(selector).update(positionally(selector, inserts))
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-3.1.7 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.6 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.5 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.4 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.3 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.2 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.1 lib/mongoid/persistence/operations/embedded/insert.rb
mongoid-3.1.0 lib/mongoid/persistence/operations/embedded/insert.rb