module Mongoid::Paranoia

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)

@example Make a document paranoid.

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

Constants

VERSION

Public Class Methods

configuration() click to toggle source
# File lib/mongoid/paranoia.rb, line 23
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Allow the paranoid Document to use an alternate field name for deleted_at.

@example

Mongoid::Paranoia.configure do |c|
  c.paranoid_field = :myFieldName
end
# File lib/mongoid/paranoia.rb, line 37
def configure
  yield(configuration)
end
reset() click to toggle source
# File lib/mongoid/paranoia.rb, line 27
def reset
  @configuration = Configuration.new
end

Public Instance Methods

delete(_ = {})
Also aliased as: orig_delete
Alias for: remove
delete!(_ = {})
Alias for: orig_delete
deleted?()
Alias for: destroyed?
destroy!(options = {}) click to toggle source

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

@example Hard destroy the document.

document.destroy!

@return [ true, false ] If the operation succeeded.

@since 1.0.0

# 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?() click to toggle source

Determines if this document is destroyed.

@example Is the document destroyed?

person.destroyed?

@return [ true, false ] If the document is destroyed.

@since 1.0.0

# File lib/mongoid/paranoia.rb, line 123
def destroyed?
  (@destroyed ||= false) || !!deleted_at
end
Also aliased as: deleted?
orig_delete(_ = {})

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

@example Soft remove the document.

document.remove

@param [ Hash ] options The database options.

@return [ true ] True.

@since 1.0.0

Also aliased as: delete!
Alias for: delete
persisted?() click to toggle source

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”

@example

document.persisted?

@return [ true, false ] If the operation succeeded.

@since 4.0.0

# File lib/mongoid/paranoia.rb, line 63
def persisted?
  !new_record?
end
remove(_ = {}) click to toggle source
# 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
Also aliased as: delete
restore(opts = {}) click to toggle source

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

@example Restore the document from deleted state.

document.restore

For resoring associated documents use :recursive => true @example Restore the associated documents from deleted state.

document.restore(:recursive => true)

TODO: @return [ Time ] The time the document had been deleted.

@since 1.0.0

# 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_relations() click to toggle source
# 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_param() click to toggle source

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

# File lib/mongoid/paranoia.rb, line 152
def to_param
  new_record? ? nil : to_key.join('-')
end

Private Instance Methods

_paranoia_update(value) click to toggle source

@return [ Object ] Update result.

# File lib/mongoid/paranoia.rb, line 192
def _paranoia_update(value)
  paranoid_collection.find(atomic_selector).update_one(value)
end
paranoid_collection() click to toggle source

Get the collection to be used for paranoid operations.

@example Get the paranoid collection.

document.paranoid_collection

@return [ Collection ] The root collection.

# File lib/mongoid/paranoia.rb, line 175
def paranoid_collection
  embedded? ? _root.collection : collection
end
paranoid_field() click to toggle source

Get the field to be used for paranoid operations.

@example Get the paranoid field.

document.paranoid_field

@return [ String ] The deleted at field.

# File lib/mongoid/paranoia.rb, line 185
def paranoid_field
  field = Paranoia.configuration.paranoid_field
  embedded? ? "#{atomic_position}.#{field}" : field
end