Class/Module Index [+]

Quicksearch

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

Public Instance Methods

delete(options = {}) click to toggle source
Alias for: remove
delete!() click to toggle source

Delete the paranoid Document from the database completely.

@example Hard delete the document.

document.delete!

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

@since 1.0.0

# File lib/mongoid/paranoia.rb, line 44
def delete!
  Persistence::Operations.remove(self).persist
end
deleted?() click to toggle source
Alias for: destroyed?
destroy!() click to toggle source

Delete the paranoid Document from the database completely. This will run the destroy 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 32
def destroy!
  run_callbacks(:destroy) { delete! }
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 79
def destroyed?
  (@destroyed ||= false) || !!deleted_at
end
Also aliased as: deleted?
remove(options = {}) click to toggle source

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

# File lib/mongoid/paranoia.rb, line 59
def remove(options = {})
  cascade!
  time = self.deleted_at = Time.now
  paranoid_collection.find(atomic_selector).
    update({ "$set" => { paranoid_field => time }})
  @destroyed = true
  IdentityMap.remove(self)
  clear_timeless_option
  true
end
Also aliased as: delete
restore() 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

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

@since 1.0.0

# File lib/mongoid/paranoia.rb, line 93
def restore
  paranoid_collection.find(atomic_selector).
    update({ "$unset" => { paranoid_field => true }})
  attributes.delete("deleted_at")
  @destroyed = false
  true
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 102
def to_param
  new_record? ? nil : to_key.join('-')
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.