GithubHelp home page GithubHelp logo

mongoid_paranoia's Introduction

Paranoid Documents for Mongoid

Build Status Gem Version Gitter chat

Mongoid::Paranoia enables a "soft delete" of Mongoid documents. Instead of being removed from the database, paranoid docs are flagged with a deleted_at timestamp and are ignored from queries by default.

The Mongoid::Paranoia functionality was originally supported in Mongoid itself, but was dropped from version 4.0.0 onwards. This gem was extracted from the Mongoid 3.0.0-stable branch. This gem should not be used with Mongoid versions 3.x and prior. Current master branch targeted on Mongoid 6.0. With release 0.3.0 Mongoid 4 and 5 versions will be dropped.

Caution: This repo/gem mongoid_paranoia (underscored) is different than mongoid-paranoia (hyphenated). The goal of mongoid-paranoia (hyphenated) is to stay API compatible and it only accepts security fixes.

Installation

Add this line to your application's Gemfile:

gem 'mongoid_paranoia'

Usage

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.
person.restore(:recursive => true) # Brings "deleted" associated documents back to life recursively

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-deleted

You can also configure the paranoid field naming on a global basis. Within the context of a Rails app this is done via an initializer.

# config/initializers/mongoid_paranoid.rb

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

Validations

You need override uniqueness validates

validates :title, uniqueness: { conditions: -> { where(deleted_at: nil) } }

Callbacks

Restore

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

Remove

before_remove, after_remove and around_remove are added to your model. They are called when record is deleted permanently .

Example

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

TODO

Authors

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

mongoid_paranoia's People

Contributors

simi avatar johnnyshields avatar tagliala avatar fudoshiki avatar zhefeng avatar dblock avatar bartoszkopinski avatar erich avatar gregmolnar avatar loopj avatar rmm5t avatar glebtv avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.