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)
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
(also: #deleted?)
Determines if this document is destroyed.
- #persisted? ⇒ Boolean
-
#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.
-
#to_param ⇒ Object
Returns a string representing the documents’s key suitable for use in URLs.
Instance Method Details
#delete! ⇒ true, false
Delete the paranoid Document
from the database completely.
45 46 47 |
# File 'lib/mongoid/paranoia.rb', line 45 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.
33 34 35 |
# File 'lib/mongoid/paranoia.rb', line 33 def destroy! run_callbacks(:destroy) { delete! } end |
#destroyed? ⇒ true, false Also known as: deleted?
Determines if this document is destroyed.
80 81 82 |
# File 'lib/mongoid/paranoia.rb', line 80 def destroyed? (@destroyed ||= false) || !!deleted_at end |
#persisted? ⇒ Boolean
85 86 87 |
# File 'lib/mongoid/paranoia.rb', line 85 def persisted? !new_record? && !(@destroyed ||= false) end |
#remove(options = {}) ⇒ true Also known as: delete
Delete the Document
, will set the deleted_at timestamp and not actually delete it.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongoid/paranoia.rb', line 60 def remove( = {}) cascade! time = self.deleted_at = Time.now paranoid_collection.find(atomic_selector). update({ "$set" => { paranoid_field => time }}) @destroyed = true IdentityMap.remove(self) clear_timeless_option true end |
#restore ⇒ Time
Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.
98 99 100 101 102 103 104 |
# File 'lib/mongoid/paranoia.rb', line 98 def restore paranoid_collection.find(atomic_selector). update({ "$unset" => { paranoid_field => true }}) attributes.delete("deleted_at") @destroyed = false true end |
#to_param ⇒ Object
Returns a string representing the documents’s key suitable for use in URLs.
107 108 109 |
# File 'lib/mongoid/paranoia.rb', line 107 def to_param new_record? ? nil : to_key.join('-') end |