There may be times when you don't want documents to actually get deleted from the database, but "flagged" as deleted. Mongoid-paranoia provides a Paranoia module to give you just that.
Old API from Mongoid 3.0 is extracted in 3.0.0-stable branch and doesn't work with Mongoid 4 anymore.
Add this line to your application's Gemfile:
gem 'mongoid-paranoia', github: 'simi/mongoid-paranoia' # before first releasevalidates_uniqueness_of :title
validates :title, :uniqueness => truevalidates :title, uniqueness: { conditions: -> { where(deleted_at: nil) } }class Person
include Mongoid::Document
include Mongoid::Paranoia
end
person.delete # Sets the deleted_at field to the current time, ignoring callbacks.
person.delete! # Permanently deletes the document, ignoring callbacks.
person.destroy # Sets the deleted_at field to the current time, firing callbacks.
person.destroy! # Permanently deletes the document, firing callbacks.
person.restore # Brings the "deleted" document back to life.The documents that have been "flagged" as deleted (soft deleted) can be accessed at any time by calling the deleted class method on the class.
Person.deleted # Returns documents that have been "flagged" as deleted.You can also access all documents (both deleted and non-deleted) at any time by using the unscoped class method:
Person.unscoped.all # Returns all documents, both deleted and non-deletedbefore_restore, after_restore and around_restore callbacks are added to your model. They work similarly to the before_destroy, after_destroy and around_destroy callbacks.
class User
include Mongoid::Document
include Mongoid::Paranoia
before_restore :before_restore_action
after_restore :after_restore_action
around_restore :around_restore_action
private
def before_restore_action
puts "BEFORE"
end
def after_restore_action
puts "AFTER"
end
def around_restore_action
puts "AROUND - BEFORE"
yield # restoring
puts "AROUND - AFTER"
end
end- get rid of monkey_patches.rb
- review persisted? behaviour
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
