Sha256: 183914d32dd179fbba584b482d72df13e15a0a3a1375984c3a8c67dc643845d7

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for persistence operations that save documents.
    module Savable

      # Save the document - will perform an insert if the document is new, and
      # update if not.
      #
      # @example Save the document.
      #   document.save
      #
      # @param [ Hash ] options Options to pass to the save.
      #
      # @return [ true | false ] True is success, false if not.
      def save(options = {})
        if new_record?
          !insert(options).new_record?
        else
          update_document(options)
        end
      end

      # Save the document - will perform an insert if the document is new, and
      # update if not. If a validation error occurs an error will get raised.
      #
      # @example Save the document.
      #   document.save!
      #
      # @param [ Hash ] options Options to pass to the save.
      #
      # @raise [ Errors::Validations ] If validation failed.
      # @raise [ Errors::Callback ] If a callback returns false.
      #
      # @return [ true | false ] True if validation passed.
      def save!(options = {})
        unless save(options)
          fail_due_to_validation! unless errors.empty?
          fail_due_to_callback!(:save!)
        end
        true
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongoid-8.0.10 lib/mongoid/persistable/savable.rb
mongoid-8.0.9 lib/mongoid/persistable/savable.rb
mongoid-8.0.8 lib/mongoid/persistable/savable.rb
mongoid-8.0.7 lib/mongoid/persistable/savable.rb
mongoid-8.0.6 lib/mongoid/persistable/savable.rb
mongoid-8.0.5 lib/mongoid/persistable/savable.rb
mongoid-8.0.4 lib/mongoid/persistable/savable.rb
mongoid-8.0.3 lib/mongoid/persistable/savable.rb
mongoid-8.0.2 lib/mongoid/persistable/savable.rb