Sha256: 7c3952c3f7cf4a6eaa27e4c401f6b5560b56d4833a2da56df98dc73488eceedc

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 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.
    #
    # The underlying query resembles the following MongoDB query:
    #
    #   collection.insert(
    #     { "_id" : 1, "field" : "value" },
    #     false
    #   );
    class Insert < Command
      # Insert the new document in the database. This delegates to the standard
      # MongoDB collection's insert command.
      #
      # Example:
      #
      # <tt>Insert.persist</tt>
      #
      # Returns:
      #
      # The +Document+, whether the insert succeeded or not.
      def persist
        return @document if @validate && !@document.valid?
        @document.run_callbacks(:before_create)
        @document.run_callbacks(:before_save)
        if insert
          @document.new_record = false
          # TODO: All child document new_record flags must get set to false
          # here or somewhere - this will cause problems.
          @document.move_changes
          @document.run_callbacks(:after_create)
          @document.run_callbacks(:after_save)
        end
        @document
      end

      protected
      # Insert the document into the database.
      def insert
        if @document.embedded?
          Persistence::InsertEmbedded.new(@document, @validate).persist
        else
          @collection.insert(@document.raw_attributes, @options)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
mongoid-1.9.5 lib/mongoid/persistence/insert.rb
mongoid-with-auth-1.9.4 lib/mongoid/persistence/insert.rb
mongoid-rails2-1.9.4 lib/mongoid/persistence/insert.rb
mongoid-rails2-1.9.3 lib/mongoid/persistence/insert.rb
mongoid-1.9.2 lib/mongoid/persistence/insert.rb
sskirby-mongoid-1.9.1 lib/mongoid/persistence/insert.rb
mongoid-1.9.1 lib/mongoid/persistence/insert.rb
mongoid-1.9.0 lib/mongoid/persistence/insert.rb