Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
An ActiveRecord plugin for atomic archiving and unarchiving of object trees. Inspired by ActsAsParanoid and PermanentRecord
Ruby Shell

This branch is 1 commit ahead, 148 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/expected_behavior
test
.gitignore
.rvmrc
MIT-LICENSE
README
Rakefile
init.rb

README

ActsAsArchival
==============
Plugin for atomically archiving model records in activerecord models.

We had the problem that acts_as_paranoid and similar plugins/gems always work on a record by record basis and made it very difficult to restore records atomically (or archive them, for that matter).

Because the archive and unarchive methods are in transactions, and every archival record involved gets the same archive number upon archiving, you can easily restore or remove an entire set of records without having to worry about partial deletion or restoration.

Additionally, other plugins generally screw with how destroy/delete work.  We don't because we actually want to be able to destroy records.

Install
=======
Rails 3:
rails plugin install http://github.com/expectedbehavior/acts_as_archival.git

Rails 2:
script/plugin install http://github.com/expectedbehavior/acts_as_archival.git -r rails2

Any models you want to be archival should have the columns archive_number(string) and archived_at(datetime).
i.e. script/generate migration AddAAAToPost archive_number:string archived_at:datetime

Any dependent-destroy objects connected to an AAA model will be archived with its parent.

Example
=======

class Hole < ActiveRecord::Base
  acts_as_archival
  has_many :moles, :dependent => :destroy
end

class Mole < ActiveRecord::Base
  acts_as_archival
end

-----

>> Hole.archived.size               # => 0
>> Hole.is_archival?                # => true
>> h = Hole.create                   
>> Hole.unarchived.size             # => 1
>> h.is_archival?                   # => true
>> h.archived?                      # => false
>> h.muskrats.create                  
>> h.archive                        # archive the hole and its muskrat.
>> h.archive_number                 # => 8c9f03f9d....
>> h.muskrats.first.archive_number  # => 8c9f03f9d....
>> h.archived?                      # => 8c9f03f9d....
>> Hole.archived.size               # => 1
>> Hole.unarchived.size             # => 0
>> h.unarchive                       
>> Hole.archived.size               # => 0
>> Hole.unarchived.size             # => 1

Caveats
=======
* This will only work on associations that are dependent destroy.  It should be trival to change that or make it optional.
* It will only work for Rails 2.2 and up, because we are using named_scope.  You can check out permanent records for a way to conditionally add the functionality to older Rails installations.

Testing
=======

Because this plugin makes use of transactions we're testing it on MySQL instead of the more convenient sqlite. Running the tests should be as easy as

cd ./test/aaa_test_app/
bundle
script/db_setup -c      # makes the databases with the correct permissions
cd ../..                # back in the AAA root
rake

Thanks
======
ActsAsParanoid and PermanentRecords were both inspirations for this
http://github.com/technoweenie/acts_as_paranoid
http://github.com/fastestforward/permanent_records/

Contributors
============
* Joel Meador
* Michael Kuehl
* Matthew Gordon
* Vojtech Salbaba
* David Jones
* Dave Woodward
-- Thanks, guys!

Copyright (c) 2009-2011 [Expected Behavior, LLC], released under the MIT license
Something went wrong with that request. Please try again.