Module: Mongoid::Paranoia
- Extended by:
- ActiveSupport::Concern
- Includes:
- Mongoid::Persistable::Deletable
- Defined in:
- lib/mongoid/paranoia.rb,
lib/mongoid/paranoia/version.rb,
lib/mongoid/paranoia/configuration.rb,
lib/mongoid/paranoia/monkey_patches.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 Classes: Configuration
Constant Summary collapse
- VERSION =
'0.3.0'.freeze
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.configure {|configuration| ... } ⇒ Object
Allow the paranoid
Document
to use an alternate field name for deleted_at. - .reset ⇒ Object
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.
-
#orig_remove ⇒ true
Delete the
Document
, will set the deleted_at timestamp and not actually delete it. -
#persisted? ⇒ true, false
Override the persisted method to allow for the paranoia gem.
- #remove(_ = {}) ⇒ Object (also: #delete)
-
#restore(opts = {}) ⇒ Object
Restores a previously soft-deleted document.
- #restore_relations ⇒ Object
-
#to_param ⇒ Object
Returns a string representing the documents’s key suitable for use in URLs.
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
23 24 25 |
# File 'lib/mongoid/paranoia.rb', line 23 def configuration @configuration end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
Allow the paranoid Document
to use an alternate field name for deleted_at.
40 41 42 |
# File 'lib/mongoid/paranoia.rb', line 40 def self.configure yield(configuration) end |
.reset ⇒ Object
30 31 32 |
# File 'lib/mongoid/paranoia.rb', line 30 def self.reset @configuration = Configuration.new end |
Instance Method Details
#delete! ⇒ true, false
Delete the paranoid Document
from the database completely.
117 118 119 |
# File 'lib/mongoid/paranoia.rb', line 117 def delete! orig_remove end |
#destroy! ⇒ true, false
Delete the paranoid Document
from the database completely. This will run the destroy callbacks.
64 65 66 67 68 69 70 |
# File 'lib/mongoid/paranoia.rb', line 64 def destroy! run_callbacks(:destroy) do run_callbacks(:remove) do delete! end end end |
#destroyed? ⇒ true, false Also known as: deleted?
Determines if this document is destroyed.
129 130 131 |
# File 'lib/mongoid/paranoia.rb', line 129 def destroyed? (@destroyed ||= false) || !!deleted_at end |
#orig_remove ⇒ true
Delete the Document
, will set the deleted_at timestamp and not actually delete it.
97 |
# File 'lib/mongoid/paranoia.rb', line 97 alias orig_remove :remove |
#persisted? ⇒ true, false
Override the persisted method to allow for the paranoia gem. If a paranoid record is selected, then we only want to check if it’s a new record, not if it is “destroyed”
82 83 84 |
# File 'lib/mongoid/paranoia.rb', line 82 def persisted? !new_record? end |
#remove(_ = {}) ⇒ Object Also known as: delete
99 100 101 102 103 104 105 |
# File 'lib/mongoid/paranoia.rb', line 99 def remove(_ = {}) cascade! time = self.deleted_at = Time.now _paranoia_update('$set' => { paranoid_field => time }) @destroyed = true true end |
#restore(opts = {}) ⇒ Object
Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.
For resoring associated documents use :recursive => true TODO: @return [ Time ] The time the document had been deleted.
147 148 149 150 151 152 153 154 155 |
# File 'lib/mongoid/paranoia.rb', line 147 def restore(opts = {}) run_callbacks(:restore) do _paranoia_update("$unset" => { paranoid_field => true }) attributes.delete("deleted_at") @destroyed = false restore_relations if opts[:recursive] true end end |
#restore_relations ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mongoid/paranoia.rb', line 162 def restore_relations self.relations.each_pair do |name, | next unless [:dependent] == :destroy relation = self.send(name) if relation.present? && relation.paranoid? Array.wrap(relation).each do |doc| doc.restore(:recursive => true) end end end end |
#to_param ⇒ Object
Returns a string representing the documents’s key suitable for use in URLs.
158 159 160 |
# File 'lib/mongoid/paranoia.rb', line 158 def to_param new_record? ? nil : to_key.join('-') end |