Sha256: b744dac73e1a460f09f5a6b4fc15db101a745117a29ee59883cbf6d47dcd11b8

Contents?: true

Size: 1.46 KB

Versions: 32

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8
module Mongoid
  module Persistable

    # Defines behaviour for persistence operations that upsert documents.
    #
    # @since 4.0.0
    module Upsertable

      # Perform an upsert of the document. If the document does not exist in the
      # database, then Mongo will insert a new one, otherwise the fields will get
      # overwritten with new values on the existing document.
      #
      # @example Upsert the document.
      #   document.upsert
      #
      # @param [ Hash ] options The validation options.
      #
      # @return [ true ] True.
      #
      # @since 3.0.0
      def upsert(options = {})
        prepare_upsert(options) do
          collection.find(atomic_selector).update_one(as_document, upsert: true)
        end
      end

      private

      # Prepare the upsert for execution.
      #
      # @api private
      #
      # @example Prepare the upsert
      #   document.prepare_upsert do
      #     collection.find(selector).update(as_document)
      #   end
      #
      # @param [ Hash ] options The options hash.
      #
      # @return [ true, false ] If the operation succeeded.
      #
      # @since 4.0.0
      def prepare_upsert(options = {})
        return false if performing_validations?(options) && invalid?(:upsert)
        result = run_callbacks(:upsert) do
          yield(self)
          true
        end
        self.new_record = false
        post_process_persist(result, options) and result
      end
    end
  end
end

Version data entries

32 entries across 29 versions & 3 rubygems

Version Path
mongoid-5.4.1 lib/mongoid/persistable/upsertable.rb
mongoid-5.4.0 lib/mongoid/persistable/upsertable.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/persistable/upsertable.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/persistable/upsertable.rb
mongoid-5.2.1 lib/mongoid/persistable/upsertable.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/persistable/upsertable.rb
mongoid-6.1.0 lib/mongoid/persistable/upsertable.rb
mongoid-5.2.0 lib/mongoid/persistable/upsertable.rb
mongoid-5.2.0.rc0 lib/mongoid/persistable/upsertable.rb
mongoid-6.1.0.rc0 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.3 lib/mongoid/persistable/upsertable.rb
mongoid-5.1.6 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.2 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.1 lib/mongoid/persistable/upsertable.rb
mongoid-5.1.5 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.0 lib/mongoid/persistable/upsertable.rb
mongoid-5.1.4 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.0.rc0 lib/mongoid/persistable/upsertable.rb
mongoid-6.0.0.beta lib/mongoid/persistable/upsertable.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-5.1.3/lib/mongoid/persistable/upsertable.rb