Sha256: d8c00e491c14e9d37ea11e12d557ff80feffd524f8e6328a6d1feea389ea39bb

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Persistence #:nodoc:

    # 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.insert(
    #     { "_id" : 1, "field" : "value" },
    #     false
    #   );
    class InsertEmbedded < Command

      # 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:
      #
      # <tt>Insert.persist</tt>
      #
      # Returns:
      #
      # The +Document+, whether the insert succeeded or not.
      def persist
        return document if validate && document.invalid?(:create)
        parent = document._parent
        if parent.new_record?
          parent.insert
        else
          update = { document._inserter => { document._position => document.as_document } }
          collection.update(parent._selector, update, options.merge(:multi => false))
          document.new_record = false
          document.move_changes
        end
        document
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mongoid-braxton-2.0.2 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.2 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.1 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.rc.8 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.rc.7 lib/mongoid/persistence/insert_embedded.rb