Git Product home page Git Product logo

acts_as_archive's Introduction

ActsAsArchive

Don't delete your records, move them to a different table.

Like acts_as_paranoid, but doesn't mess with your SQL queries.

Install

gem install acts_as_archive

Rails 2

config/environment.rb

config.gem 'acts_as_archive'

Rails 3

Gemfile

gem 'acts_as_archive'

Sinatra

require 'acts_as_archive'

class Application < Sinatra::Base
  include ActsAsArchive::Adapters::Sinatra
end

config/acts_as_archive.yml

Create config/acts_as_archive.yml to define the archive class and archive table for each of your models:

Article:
  - class: Article::Archive
    table: archived_articles

It is expected that neither the archive class or archive table exist yet. ActsAsArchive will create these automatically.

Migrate

Run rake db:migrate. Your archive table is created automatically.

That's it!

Use destroy, destroy_all, delete, and delete_all like you normally would.

Records move into the archive table instead of being destroyed.

Automatically archive relationships

If your model's relationship has the :dependent option, and the relationship also uses acts_as_archive, that relationship will archive automatically.

What if my schema changes?

New migrations are automatically applied to the archive table.

No action is necessary on your part.

Query the archive

Use the archive class you specified in the configuration:

Article::Archive.first

Delete records without archiving

Use any of the destroy methods, but add a bang (!):

Article::Archive.first.destroy!
Article.delete_all!([ "id in (?)", [ 1, 2, 3 ] ])

Restore from the archive

Use any of the destroy/delete methods on the archived record to move it back to its original table:

Article::Archive.first.destroy
Article::Archive.delete_all([ "id in (?)", [ 1, 2, 3 ] ])

Any relationships that were automatically archived will be restored as well.

Magic columns

You will find an extra deleted_at datetime column on the archive table.

You may manually add a restored_at datetime column to the origin table if you wish to store restoration time as well.

Migrate from acts_as_paranoid

Add this line to a migration, or run it via script/console:

Article.migrate_from_acts_as_paranoid

This copies all records with non-null deleted_at values to the archive.

Running specs

There is a wiki entry that describes the development setup in-depth.

acts_as_archive's People

Contributors

comboy avatar look avatar varyform avatar whatcould avatar winton avatar

Watchers

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