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.6.0'
Class Method Summary collapse
- .configuration ⇒ Object
-
.configure {|configuration| ... } ⇒ Object
Allow the paranoid
Documentto use an alternate field name for deleted_at. - .reset ⇒ Object
Instance Method Summary collapse
-
#destroy!(options = {}) ⇒ 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(_ = {}) ⇒ 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 Method Details
.configuration ⇒ Object
23 24 25 |
# File 'lib/mongoid/paranoia.rb', line 23 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
Allow the paranoid Document to use an alternate field name for deleted_at.
37 38 39 |
# File 'lib/mongoid/paranoia.rb', line 37 def configure yield(configuration) end |
.reset ⇒ Object
27 28 29 |
# File 'lib/mongoid/paranoia.rb', line 27 def reset @configuration = Configuration.new end |
Instance Method Details
#destroy!(options = {}) ⇒ true, false
Delete the paranoid Document from the database completely. This will run the destroy and remove callbacks.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mongoid/paranoia.rb', line 99 def destroy!( = {}) raise Errors::ReadonlyDocument.new(self.class) if readonly? self.flagged_for_destroy = true result = run_callbacks(:destroy) do run_callbacks(:remove) do if catch(:abort) { apply_destroy_dependencies! } delete!( || {}) else false end end end self.flagged_for_destroy = false result end |
#destroyed? ⇒ true, false Also known as: deleted?
Determines if this document is destroyed.
123 124 125 |
# File 'lib/mongoid/paranoia.rb', line 123 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”
63 64 65 |
# File 'lib/mongoid/paranoia.rb', line 63 def persisted? !new_record? end |
#remove(_ = {}) ⇒ Object Also known as: delete
80 81 82 83 84 85 |
# File 'lib/mongoid/paranoia.rb', line 80 def remove(_ = {}) 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.
141 142 143 144 145 146 147 148 149 |
# File 'lib/mongoid/paranoia.rb', line 141 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
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mongoid/paranoia.rb', line 156 def restore_relations relations.each_pair do |name, association| next unless association.dependent == :destroy relation = send(name) next unless relation.present? && relation.paranoid? Array.wrap(relation).each do |doc| doc.restore(recursive: true) end end end |
#to_param ⇒ Object
Returns a string representing the documents’s key suitable for use in URLs.
152 153 154 |
# File 'lib/mongoid/paranoia.rb', line 152 def to_param new_record? ? nil : to_key.join('-') end |