Project

animator

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Animator is a cleanly namespaced ActiveRecord plugin that hooks into the existing model life-cycle allowing you to to restore (Animable#reanimate), query (Animable.inanimate), and inspect (Animable#divine) destroyed objects including associations without the tedium and ugliness of default scopes, monkey-patched methods, and complex callbacks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 5.0, >= 3.0
< 5.0, >= 3.0
 Project Readme

Animator

Inspired by the elegance of PaperTrail, Animator is a cleanly namespaced ActiveRecord plugin that hooks into the existing model life-cycle allowing the restoration (Animable#reanimate) and querying (Animable.inanimate) of destroyed objects including associations without the tedium and ugliness of default scopes, monkey-patched methods, and complex callbacks.

Getting Started

Add it to your Gemfile and run the bundle command.

gem 'animator'

Once the gem is installed, run the animator:install generator to create the necessary migration and default initializer.

rails g animator:install

Note: Out of the box, Animator protects every model in the application. To selectively enable Animator, delete the initializer in config/initializers/animator.rb and include Animator::Animable at the top of the desired models manually.

Finally, the database must be migrated to create the eraminhos table before Animator will work properly.

rake db:migrate

Note: In the very unlikely event that eraminhos needs to be preserved for use in the application, Animator may be configured to use an alternative table name by editing the migration and appending Animator::Eraminho.table_name = 'alternative_table_name_for_eraminhos' to the initializer.

Usage

Destroying

There's nothing special here. A simple call to destroy and friends will suffice. Animator infers dependent assocations by recording the transaction in which an object is destroyed.

Note: Animator works by registering an after_destroy callback, and therefore can only preserve data when the destroy callbacks are run. This means that calls to methods like delete cannot be reversed.

Querying

Animator::Animable adds a scope called inanimate that can be used with existing scopes and ActiveRecord::Relation methods to query and filter destroyed objects. Objects retrieved in this way will remain in the destroyed state (destroyed? is true) until they are reanimated.

Reanimating

Animable objects (animable? is true) can be restored with a call to reanimate on the instance or reanimate_all on a relation. The reanimation callbacks will be triggered on the instance.

Animator restores relational objects by reversing the transaction in which the object was destroyed. An individual object may be reanimated by passing false. Animator will reverse the entire transaction or leave the database unchanged.

Troubleshooting

To be consistent with existing ActiveRecord behavior, plain Animator methods return a reference to the object on which they are called regardless of failure. This means destroyed? on each instance must be checked to know if reanimate was successful. The ! versions of the methods will raise informative exceptions upon failure, and in some cases, provide troubleshooting tips.