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.4.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



23
24
25
# File 'lib/mongoid/paranoia.rb', line 23

def configuration
  @configuration
end

Class Method Details

.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:



40
41
42
# File 'lib/mongoid/paranoia.rb', line 40

def self.configure
  yield(configuration)
end

.resetObject



30
31
32
# File 'lib/mongoid/paranoia.rb', line 30

def self.reset
  @configuration = Configuration.new
end

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



117
118
119
# File 'lib/mongoid/paranoia.rb', line 117

def delete!
  orig_remove
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



64
65
66
67
68
69
70
# File 'lib/mongoid/paranoia.rb', line 64

def destroy!
  run_callbacks(:destroy) do
    run_callbacks(:remove) do
      delete!
    end
  end
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



129
130
131
# File 'lib/mongoid/paranoia.rb', line 129

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

#orig_removetrue

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



97
# File 'lib/mongoid/paranoia.rb', line 97

alias orig_remove :remove

#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



82
83
84
# File 'lib/mongoid/paranoia.rb', line 82

def persisted?
  !new_record?
end

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



99
100
101
102
103
104
105
# File 'lib/mongoid/paranoia.rb', line 99

def remove(_ = {})
  return false unless catch(:abort) { apply_delete_dependencies! }
  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



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_relationsObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mongoid/paranoia.rb', line 162

def restore_relations
  self.relations.each_pair do |name, association|
    next unless association.dependent == :destroy
    relation = self.send(name)
    if relation.present? && relation.paranoid?
      Array.wrap(relation).each do |doc|
        doc.restore(:recursive => true)
      end
    end
  end
end

#to_paramObject

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