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)

Examples:

Make a document paranoid.

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

Defined Under Namespace

Modules: Document Classes: Configuration

Constant Summary collapse

VERSION =
'0.6.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationObject



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.

Examples:

Mongoid::Paranoia.configure do |c|
  c.paranoid_field = :myFieldName
end

Yields:



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

def configure
  yield(configuration)
end

.resetObject



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.

Examples:

Hard destroy the document.

document.destroy!

Returns:

  • (true, false)

    If the operation succeeded.

Raises:

  • (Errors::ReadonlyDocument)

Since:

  • 1.0.0



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongoid/paranoia.rb', line 99

def destroy!(options = {})
  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!(options || {})
      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.

Examples:

Is the document destroyed?

person.destroyed?

Returns:

  • (true, false)

    If the document is destroyed.

Since:

  • 1.0.0



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”

Examples:

document.persisted?

Returns:

  • (true, false)

    If the operation succeeded.

Since:

  • 4.0.0



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.

Examples:

Restore the document from deleted state.

document.restore

Restore the associated documents from deleted state.

document.restore(:recursive => true)

Since:

  • 1.0.0



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_relationsObject



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_paramObject

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