Sha256: 8398ad1812d121cd24dabdbaa09c5b628606a12c729a654d83013e2c04b5f33e

Contents?: true

Size: 1.71 KB

Versions: 21

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Persistable

    # Defines behavior for persistence operations that destroy documents.
    #
    # @since 4.0.0
    module Destroyable
      extend ActiveSupport::Concern

      # Remove the document from the database with callbacks.
      #
      # @example Destroy a document.
      #   document.destroy
      #
      # @param [ Hash ] options Options to pass to destroy.
      #
      # @return [ true, false ] True if successful, false if not.
      #
      # @since 1.0.0
      def destroy(options = nil)
        raise Errors::ReadonlyDocument.new(self.class) if readonly?
        self.flagged_for_destroy = true
        result = run_callbacks(:destroy) { delete(options || {}) }
        self.flagged_for_destroy = false
        result
      end

      def destroy!(options = {})
        destroy || raise(Errors::DocumentNotDestroyed.new(id, self.class))
      end

      module ClassMethods

        # Delete all documents given the supplied conditions. If no conditions
        # are passed, the entire collection will be dropped for performance
        # benefits. Fires the destroy callbacks if conditions were passed.
        #
        # @example Destroy matching documents from the collection.
        #   Person.destroy_all({ :title => "Sir" })
        #
        # @example Destroy all documents from the collection.
        #   Person.destroy_all
        #
        # @param [ Hash ] conditions Optional conditions to destroy by.
        #
        # @return [ Integer ] The number of documents destroyed.
        #
        # @since 1.0.0
        def destroy_all(conditions = nil)
          where(conditions || {}).destroy
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
mongoid-7.1.11 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.6 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.5 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.10 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.9 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.4 lib/mongoid/persistable/destroyable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/persistable/destroyable.rb
mongoid-7.2.3 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.8 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.2 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.1 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.7 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.0 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.6 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.5 lib/mongoid/persistable/destroyable.rb
mongoid-7.2.0.rc1 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.4 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.2 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.1 lib/mongoid/persistable/destroyable.rb
mongoid-7.1.0 lib/mongoid/persistable/destroyable.rb