Sha256: ed218e3ce1a504f03555a1a293d018808bc1eb26f481843d4b4ca1922a251d77

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

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

      # Update is a persistence command responsible for taking a document that
      # has already been saved to the database and saving it, depending on
      # whether or not the document has been modified.
      #
      # Before persisting the command will check via dirty attributes if the
      # document has changed, if not, it will simply return true. If it has it
      # will go through the validation steps, run callbacks, and set the changed
      # fields atomically on the document. The underlying query resembles the
      # following MongoDB query:
      #
      #   collection.update(
      #     { "_id" : 1,
      #     { "$set" : { "field" : "value" },
      #     false,
      #     false
      #   );
      #
      # For embedded documents it will use the positional locator:
      #
      #   collection.update(
      #     { "_id" : 1, "addresses._id" : 2 },
      #     { "$set" : { "addresses.$.field" : "value" },
      #     false,
      #     false
      #   );
      #
      class Update
        include Modification, Operations

        # Persist the document that is to be updated to the database. This will
        # only write changed fields via MongoDB's $set modifier operation.
        #
        # @example Update the document.
        #   Update.persist
        #
        # @return [ true, false ] If the save passed.
        def persist
          prepare do
            unless updates.empty?
              collection.update(selector, updates, options)
              collection.update(selector, conflicts, options) if conflicts
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongoid-2.1.9 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.8 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.7 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.6 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.5 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.4 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.3 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.2 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.1 lib/mongoid/persistence/operations/update.rb
mongoid-2.1.0 lib/mongoid/persistence/operations/update.rb