Module: MongoidExt::Paranoia

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_ext/paranoia.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)

To use:

class Person
  include Mongoid::Document
  include MongoidExt::Paranoia
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete!Object

Delete the paranoid Document from the database completely.

Example:

document.delete!



34
35
36
# File 'lib/mongoid_ext/paranoia.rb', line 34

def delete!
  Mongoid::Persistence::Remove.new(self).persist
end

#destroy!Object

Delete the paranoid Document from the database completely. This will run the destroy callbacks.

Example:

document.destroy!



25
26
27
# File 'lib/mongoid_ext/paranoia.rb', line 25

def destroy!
  run_callbacks(:destroy) { delete! }
end

#remove(options = {}) ⇒ Object Also known as: delete

Delete the Document, will set the deleted_at timestamp and not actually delete it.

Example:

document.remove

Returns:

true



48
49
50
51
52
# File 'lib/mongoid_ext/paranoia.rb', line 48

def remove(options = {})
  self.class.paranoia_klass.create(:document => self.raw_attributes)

  super
end