Atomically archive object trees in your 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
delete work. We don't because we actually want to be able
to destroy records.
Rails 3.2 and up
gem install acts_as_archival
or in your Gemfile
rails plugin install http://github.com/expectedbehavior/acts_as_archival.git -r rails3.0x
script/plugin install http://github.com/expectedbehavior/acts_as_archival.git -r rails2
Any models you want to be archival should have the columns
rails g migration AddAAAToPost archive_number archived_at:datetime
Any dependent-destroy model connected to an AAA model will be archived with its parent.
class Hole < ActiveRecord::Base acts_as_archival has_many :rats, :dependent => :destroy end class Rat < ActiveRecord::Base acts_as_archival end
>> Hole.archived.size # => 0 >> Hole.is_archival? # => true >> hole = Hole.create >> Hole.unarchived.size # => 1 >> hole.is_archival? # => true >> hole.archived? # => false >> hole.rats.create >> hole.archive # archive hole and rat >> hole.archive_number # => 8c9f03f9d.... >> hole.rats.first.archive_number # => 8c9f03f9d.... >> hole.rats.first.archived? # => 8c9f03f9d.... >> hole.archived? # => 8c9f03f9d.... >> Rat.archived.size # => 1 >> Hole.archived.size # => 1 >> Hole.unarchived.size # => 0 >> Rat.unarchived.size # => 0 >> hole.unarchive >> Hole.archived.size # => 0 >> Hole.unarchived.size # => 1 >> Rat.archived.size # => 0 >> Rat.unarchived.size # => 1
- 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
scope. You can check out permanent records for a way to conditionally add the functionality to older Rails installations.
- This will only work (well) on databases with transactions (mysql, postgres, etc.).
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:
bundle test/script/db_setup # makes the databases with the correct permissions (for mySQL) rake
When defining an AAA model, it is is possible to make it unmodifiable
when it is archived by passing
:readonly_when_archived => true to the
acts_as_archival call in your model.
class CantTouchThis < ActiveRecord::Base acts_as_archival :readonly_when_archived => true end >> record = CantTouchThis.create(:foo => "bar") >> record.archive # => true >> record.foo = "I want this to work" >> record.save # => false >> record.errors.full_messages.first # => "Cannot modify an archived record."
It would be cool if someone could check if this thing works on postgres and if not, submit a patch / let us know about it!
ActsAsParanoid and PermanentRecords were both inspirations for this:
- Joel Meador
- Michael Kuehl
- Matthew Gordon
- Vojtech Salbaba
- David Jones
- Dave Woodward
- Miles Sterrett
- James Hill
- Marten Claes
Copyright (c) 2009-2013 Expected Behavior, LLC, released under the MIT license