Sha256: 5dee3f502c233cf25f812e76b6e72be917329c206679b988295a44c539bd9e39

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Persistable

    # Defines behavior for persistence operations that destroy documents.
    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.
      def destroy(options = nil)
        raise Errors::ReadonlyDocument.new(self.class) if readonly?
        self.flagged_for_destroy = true
        result = run_callbacks(:destroy) do
          if catch(:abort) { apply_destroy_dependencies! }
            delete(options || {})
          else
            false
          end
        end
        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.
        def destroy_all(conditions = nil)
          where(conditions || {}).destroy
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mongoid-8.1.8 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.7 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.6 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.8 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.5 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.4 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.7 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.3 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.2 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.6 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.1 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.5 lib/mongoid/persistable/destroyable.rb
mongoid-8.1.0 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.4 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.3 lib/mongoid/persistable/destroyable.rb
mongoid-8.0.2 lib/mongoid/persistable/destroyable.rb