Sha256: 8a5e1a499d806b4d06df67ac14fb57bdbb33d6ee3aa60b0c6db1c1aed646a836

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 KB

Contents

# encoding: utf-8
module Mongoid
  module Persistable

    # Defines behaviour 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 = {})
        self.flagged_for_destroy = true
        result = run_callbacks(:destroy) { delete(options) }
        self.flagged_for_destroy = false
        result
      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)
          selector = conditions || {}
          documents = where(selector)
          destroyed = documents.count
          documents.each { |doc| doc.destroy }
          destroyed
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
mongoid-4.0.0.alpha2 lib/mongoid/persistable/destroyable.rb
mongoid-4.0.0.alpha1 lib/mongoid/persistable/destroyable.rb
sepastian-mongoid-rails4-4.0.1.alpha lib/mongoid/persistable/destroyable.rb
sepastian-mongoid-rails4-4.0.0.alpha lib/mongoid/persistable/destroyable.rb
mongoid_heroku_stable-4.0.0 lib/mongoid/persistable/destroyable.rb
mongoid_rails4-4.0.0 lib/mongoid/persistable/destroyable.rb