Module: Mongoid::Paranoia
- Extended by:
- ActiveSupport::Concern
- Includes:
- Mongoid::Persistable::Deletable
- Defined in:
- lib/mongoid/paranoia.rb,
lib/mongoid/paranoia/version.rb,
lib/mongoid/core_ext/document.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: Document
Constant Summary collapse
- VERSION =
'1.0.0'
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_with_paranoia(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.
73 74 75 |
# File 'lib/mongoid/paranoia.rb', line 73 def delete! remove_without_paranoia end |
#destroy! ⇒ true, false
Delete the paranoid Document
from the database completely. This will run the destroy callbacks.
39 40 41 |
# File 'lib/mongoid/paranoia.rb', line 39 def destroy! run_callbacks(:destroy) { delete! } end |
#destroyed? ⇒ true, false Also known as: deleted?
Determines if this document is destroyed.
85 86 87 |
# File 'lib/mongoid/paranoia.rb', line 85 def destroyed? (@destroyed ||= false) || !!deleted_at end |
#persisted? ⇒ Boolean
90 91 92 |
# File 'lib/mongoid/paranoia.rb', line 90 def persisted? !new_record? && !(@destroyed ||= false) end |
#remove_with_paranoia(options = {}) ⇒ true Also known as: delete
Delete the Document
, will set the deleted_at timestamp and not actually delete it.
54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/paranoia.rb', line 54 def remove_with_paranoia( = {}) cascade! time = self.deleted_at = Time.now paranoid_collection.find(atomic_selector). update({ "$set" => { paranoid_field => time }}) @destroyed = true true end |
#restore ⇒ Time
Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.
103 104 105 106 107 108 109 |
# File 'lib/mongoid/paranoia.rb', line 103 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.
112 113 114 |
# File 'lib/mongoid/paranoia.rb', line 112 def to_param new_record? ? nil : to_key.join('-') end |