Module: Mongoid::Paranoia
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/paranoia.rb
Overview
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)
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete! ⇒ true, false
Delete the paranoid
Document
from the database completely. -
#destroy! ⇒ true, false
Delete the paranoid
Document
from the database completely. -
#destroyed? ⇒ true, false
Determines if this document is destroyed.
-
#remove(options = {}) ⇒ true
(also: #delete)
Delete the
Document
, will set the deleted_at timestamp and not actually delete it. -
#restore ⇒ Time
Restores a previously soft-deleted document.
Instance Method Details
#delete! ⇒ true, false
Delete the paranoid Document
from the database completely.
41 42 43 |
# File 'lib/mongoid/paranoia.rb', line 41 def delete! Persistence::Operations.remove(self).persist end |
#destroy! ⇒ true, false
Delete the paranoid Document
from the database completely. This will run the destroy callbacks.
29 30 31 |
# File 'lib/mongoid/paranoia.rb', line 29 def destroy! run_callbacks(:destroy) { delete! } end |
#destroyed? ⇒ true, false
Determines if this document is destroyed.
79 80 81 |
# File 'lib/mongoid/paranoia.rb', line 79 def destroyed? @destroyed || !!deleted_at end |
#remove(options = {}) ⇒ true Also known as: delete
Delete the Document
, will set the deleted_at timestamp and not actually delete it.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/paranoia.rb', line 56 def remove( = {}) time = self.deleted_at = Time.now paranoid_collection.update( atomic_selector, { "$set" => { paranoid_field => time }}, Safety.() ) cascade! @destroyed = true IdentityMap.remove(self) Threaded. true end |
#restore ⇒ Time
Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.
92 93 94 95 96 97 98 |
# File 'lib/mongoid/paranoia.rb', line 92 def restore paranoid_collection.update( atomic_selector, { "$unset" => { paranoid_field => true }} ) attributes.delete("deleted_at") end |