Module: Mongoid::Paranoia

Extended by:
ActiveSupport::Concern
Includes:
Mongoid::Persistable::Deletable
Defined in:
lib/mongoid/paranoia.rb,
lib/mongoid/paranoia/version.rb,
lib/mongoid/core_ext/document.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)

Examples:

Make a document paranoid.

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

Defined Under Namespace

Modules: Document

Constant Summary collapse

VERSION =
'2.0.0'.freeze

Instance Method Summary collapse

Instance Method Details

#delete!true, false

Delete the paranoid Document from the database completely.

Examples:

Hard delete the document.

document.delete!

Returns:

  • (true, false)

    If the operation succeeded.

Since:

  • 1.0.0



76
77
78
# File 'lib/mongoid/paranoia.rb', line 76

def delete!
  remove_without_paranoia
end

#destroy!true, false

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

Examples:

Hard destroy the document.

document.destroy!

Returns:

  • (true, false)

    If the operation succeeded.

Since:

  • 1.0.0



38
39
40
# File 'lib/mongoid/paranoia.rb', line 38

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

#destroyed?true, false Also known as: deleted?

Determines if this document is destroyed.

Examples:

Is the document destroyed?

person.destroyed?

Returns:

  • (true, false)

    If the document is destroyed.

Since:

  • 1.0.0



88
89
90
# File 'lib/mongoid/paranoia.rb', line 88

def destroyed?
  (@destroyed ||= false) || !!deleted_at
end

#persisted?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mongoid/paranoia.rb', line 93

def persisted?
  !new_record? && !(@destroyed ||= false)
end

#remove_with_paranoia(_options = {}) ⇒ true Also known as: delete

TODO:

Remove Mongoid 4 support.

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

Examples:

Soft remove the document.

document.remove

Parameters:

  • options (Hash)

    The database options.

Returns:

  • (true)

    True.

Since:

  • 1.0.0



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/paranoia.rb', line 54

def remove_with_paranoia(_options = {})
  cascade!
  time = self.deleted_at = Time.now
  query = paranoid_collection.find(atomic_selector)
  query.respond_to?(:update_one) ?
    query.update_one('$set' => { paranoid_field => time }) :
    query.update('$set' => { paranoid_field => time })

  @destroyed = true
  true
end

#restoreTime

TODO:

Remove Mongoid 4 support.

Restores a previously soft-deleted document. Handles this by removing the deleted_at flag.

Examples:

Restore the document from deleted state.

document.restore

Returns:

  • (Time)

    The time the document had been deleted.

Since:

  • 1.0.0



107
108
109
110
111
112
113
114
115
116
# File 'lib/mongoid/paranoia.rb', line 107

def restore
  query = paranoid_collection.find(atomic_selector)
  query.respond_to?(:update_one) ?
    query.update_one('$unset' => { paranoid_field => true }) :
    query.update('$unset' => { paranoid_field => true })

  attributes.delete('deleted_at')
  @destroyed = false
  true
end

#to_paramObject

Returns a string representing the documents’s key suitable for use in URLs.



119
120
121
# File 'lib/mongoid/paranoia.rb', line 119

def to_param
  new_record? ? nil : to_key.join('-')
end