Sha256: 0ba6f901ff255268c8f7938a4e40cd1b03b1d6201815740d617ffb3fd27244fd

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 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
        @document.run_callbacks(:create) do
          @document.run_callbacks(:save) do
            if parent.new_record?
              parent.insert
            else
              update = { @document._inserter => { @document._position => @document.raw_attributes } }
              @collection.update(parent._selector, update, @options.merge(:multi => false))
              @document.new_record = false
            end
          end
        end
        @document
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
mongoid-2.0.0.beta.13 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.11 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.10 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.8 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.9 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.12 lib/mongoid/persistence/insert_embedded.rb
mongoid-locomotive-2.0.0.beta9 lib/mongoid/persistence/insert_embedded.rb