Git Product home page Git Product logo

permanent_records's Introduction

PermanentRecords (Rails 3+)

http://github.com/JackDanger/permanent_records/

This gem prevents any of your ActiveRecord data from being destroyed. Any model that you've given a "deleted_at" datetime column will have that column set rather than let the record be deleted.

What methods does it give me?

User.find(3).destroy          # Sets the 'deleted_at' attribute to Time.now 
                              # and returns a frozen record.
                              
User.find(3).destroy(:force)  # Executes the real destroy method, the record 
                              # will be removed from the database.
                              
User.destroy_all              # Soft-deletes all User records.

User.delete_all               # bye bye everything (no soft-deleting here)

There are also two scopes provided for easily searching deleted and not deleted records:

User.deleted.find(...)        # Only returns deleted records.

User.not_deleted.find(...)    # Only returns non-deleted records.

Note: Your normal finds will, by default, include deleted records. You'll have to manually use the not_deleted scope to avoid this:

User.find(1)                  # Will find record number 1, even if it's deleted.

User.not_deleted.find(1)      # This is probably what you want, it doesn't find deleted records.

Can I easily undelete records?

Yes. All you need to do is call the 'revive' method.

User.find(3).destroy         # The user is now deleted.

User.find(3).revive          # The user is back to it's original state.

And if you had dependent records that were set to be destroyed along with the parent record:

class User < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
end

User.find(3).destroy         # All the comments are destroyed as well.

User.find(3).revive          # All the comments that were just destroyed 
                             # are now back in pristine condition.

Forcing deletion works the same way: if you hard delete a record, its dependent records will also be hard deleted.

Can I use default scopes?

default_scope where(:deleted_at => nil)

If you use such a default scope, you will need to simulate the deleted scope with a method

def self.deleted
  self.unscoped.where('deleted_at IS NOT NULL')
end

Is Everything Automated?

Yes. You don't have to change ANY of your code to get permanent archiving of all your data with this gem. When you call destroy on any record (or destroy_all on a class or association) your records will all have a deleted_at timestamp set on them.

Productionizing

If you operate a system where destroying or reviving a record takes more than about 3 seconds then you'll want to customize PermanentRecords.dependent_record_window = 10.seconds or some other value that works for you.

Patches welcome, forks celebrated.

Copyright (c) 2013 Jack Danger Canty @ http://jåck.com released under the MIT license

permanent_records's People

Contributors

jackdanger avatar davidsulc avatar jqr avatar begriffs avatar ejholmes avatar adrianpike avatar alexnisnevich avatar jbusser avatar jteneycke avatar siong1987 avatar tanordheim avatar ichi avatar mherold 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.