Sha256: 5918bf5a185ddcd226376f331655e9bf16f4867f023b7327fd94ce8faab9eaaa

Contents?: true

Size: 1.73 KB

Versions: 24

Compression:

Stored size: 1.73 KB

Contents

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

      # 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.find(selector).update(updates)
              conflicts.each_pair do |key, value|
                collection.find(selector).update({ key => value })
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
mongoid-3.0.23 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.22 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.21 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.20 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.19 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.18 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.17 lib/mongoid/persistence/operations/update.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/persistence/operations/update.rb
mongoid-3.0.16 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.15 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.14 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.13 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.12 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.11 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.10 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.9 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.6 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.5 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.4 lib/mongoid/persistence/operations/update.rb
mongoid-3.0.3 lib/mongoid/persistence/operations/update.rb