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.2.1'
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.configure {|configuration| ... } ⇒ Object
Allow the paranoid
Documentto use an alternate field name for deleted_at. - .reset ⇒ Object
Instance Method Summary collapse
-
#delete! ⇒ true, false
Delete the paranoid
Documentfrom the database completely. -
#destroy! ⇒ true, false
Delete the paranoid
Documentfrom the database completely. -
#destroyed? ⇒ true, false
(also: #deleted?)
Determines if this document is destroyed.
-
#persisted? ⇒ true, false
Override the persisted method to allow for the paranoia gem.
-
#remove_with_paranoia(options = {}) ⇒ true
Delete the
Document, will set the deleted_at timestamp and not actually delete it. -
#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.
24 25 26 |
# File 'lib/mongoid/paranoia.rb', line 24 def configuration @configuration end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
Allow the paranoid Document to use an alternate field name for deleted_at.
41 42 43 |
# File 'lib/mongoid/paranoia.rb', line 41 def self.configure yield(configuration) end |
.reset ⇒ Object
31 32 33 |
# File 'lib/mongoid/paranoia.rb', line 31 def self.reset @configuration = Configuration.new end |
Instance Method Details
#delete! ⇒ true, false
Delete the paranoid Document from the database completely.
116 117 118 |
# File 'lib/mongoid/paranoia.rb', line 116 def delete! remove_without_paranoia end |
#destroy! ⇒ true, false
Delete the paranoid Document from the database completely. This will run the destroy callbacks.
65 66 67 68 69 70 71 |
# File 'lib/mongoid/paranoia.rb', line 65 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.
128 129 130 |
# File 'lib/mongoid/paranoia.rb', line 128 def destroyed? (@destroyed ||= false) || !!deleted_at end |
#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”
83 84 85 |
# File 'lib/mongoid/paranoia.rb', line 83 def persisted? !new_record? end |
#remove_with_paranoia(options = {}) ⇒ true
Delete the Document, will set the deleted_at timestamp and not actually delete it.
98 99 100 101 102 103 104 |
# File 'lib/mongoid/paranoia.rb', line 98 def remove_with_paranoia( = {}) 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.
146 147 148 149 150 151 152 153 154 |
# File 'lib/mongoid/paranoia.rb', line 146 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
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mongoid/paranoia.rb', line 161 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.
157 158 159 |
# File 'lib/mongoid/paranoia.rb', line 157 def to_param new_record? ? nil : to_key.join('-') end |