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.7.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)
rubocop:disable Naming/PredicateMethod.
-
#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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mongoid/paranoia.rb', line 105 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.
129 130 131 |
# File 'lib/mongoid/paranoia.rb', line 129 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"
69 70 71 |
# File 'lib/mongoid/paranoia.rb', line 69 def persisted? !new_record? end |
#remove(_ = {}) ⇒ Object Also known as: delete
rubocop:disable Naming/PredicateMethod
86 87 88 89 90 91 |
# File 'lib/mongoid/paranoia.rb', line 86 def remove(_ = {}) # rubocop:disable Naming/PredicateMethod 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 |
# File 'lib/mongoid/paranoia.rb', line 162 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.
158 159 160 |
# File 'lib/mongoid/paranoia.rb', line 158 def to_param new_record? ? nil : to_key.join('-') end |