module Mongoid::Paranoia
Include this module to get soft deletion of root level documents. This will add a deleted_at field to the Document
, managed automatically. Potentially incompatible with unique indices. (if collisions with deleted items)
@example Make a document paranoid.
class Person include Mongoid::Document include Mongoid::Paranoia end
Constants
- VERSION
Public Instance Methods
Delete the paranoid Document
from the database completely.
@example Hard delete the document.
document.delete!
@return [ true, false ] If the operation succeeded.
@since 1.0.0
# File lib/mongoid/paranoia.rb, line 76 def delete! remove_without_paranoia end
Delete the paranoid Document
from the database completely. This will run the destroy callbacks.
@example Hard destroy the document.
document.destroy!
@return [ true, false ] If the operation succeeded.
@since 1.0.0
# File lib/mongoid/paranoia.rb, line 38 def destroy! run_callbacks(:destroy) { delete! } end
Determines if this document is destroyed.
@example Is the document destroyed?
person.destroyed?
@return [ true, false ] If the document is destroyed.
@since 1.0.0
# File lib/mongoid/paranoia.rb, line 88 def destroyed? (@destroyed ||= false) || !!deleted_at end
# File lib/mongoid/paranoia.rb, line 93 def persisted? !new_record? && !(@destroyed ||= false) end
Delete the Document
, will set the deleted_at timestamp and not actually delete it.
@example Soft remove the document.
document.remove
@param [ Hash ] options The database options.
@return [ true ] True.
@todo Remove Mongoid
4 support. @since 1.0.0
# File lib/mongoid/paranoia.rb, line 54 def remove_with_paranoia(_options = {}) cascade! time = self.deleted_at = Time.now query = paranoid_collection.find(atomic_selector) query.respond_to?(:update_one) ? query.update_one('$set' => { paranoid_field => time }) : query.update('$set' => { paranoid_field => time }) @destroyed = true true end
Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.
@example Restore the document from deleted state.
document.restore
@return [ Time ] The time the document had been deleted.
@todo Remove Mongoid
4 support. @since 1.0.0
# File lib/mongoid/paranoia.rb, line 107 def restore query = paranoid_collection.find(atomic_selector) query.respond_to?(:update_one) ? query.update_one('$unset' => { paranoid_field => true }) : query.update('$unset' => { paranoid_field => true }) attributes.delete('deleted_at') @destroyed = false true end
Returns a string representing the documents’s key suitable for use in URLs.
# File lib/mongoid/paranoia.rb, line 119 def to_param new_record? ? nil : to_key.join('-') end
Private Instance Methods
Get the collection to be used for paranoid operations.
@example Get the paranoid collection.
document.paranoid_collection
@return [ Collection ] The root collection.
@since 2.3.1
# File lib/mongoid/paranoia.rb, line 133 def paranoid_collection embedded? ? _root.collection : collection end
Get the field to be used for paranoid operations.
@example Get the paranoid field.
document.paranoid_field
@return [ String ] The deleted at field.
@since 2.3.1
# File lib/mongoid/paranoia.rb, line 145 def paranoid_field embedded? ? "#{atomic_position}.deleted_at" : 'deleted_at' end