Introducing Purgatory
December 06, 2013
Hi, I’m Elan, a developer at Financeit. Recently I was tasked with adding 2-person authentication to make sure sensitive changes had two pairs of eyes reviewing and approving them. To do this I needed a way store changes without actually applying them, so they could be applied later after a manager gives the OK.
So I came up with Purgatory, Financeit’s first publicly released gem. Purgatory is a Rails gem that allows you to save changes to an ActiveRecord model so that they can be applied at a later time. While the changes are in this interim state they are considered to be in Purgatory. A Purgatory object stores the changes on the main object (the soul) as well as the user who made the request and the changes. Purgatory can also be used to store the creation of a new record until it is approved.
Here’s an example: say you have a model called BankAccount and you don’t want your users to be able to modify a bank account’s details without a second user approving the changes. To put your changes into purgatory, just do the following:
bank_account.account_number = ‘1234567’
bank_account.transit_number = ‘12345’
bank_account.purgatory!(current_user)
This will add a new Purgatory in the system. To view the proposed changes, just call the ‘requested_changes’ method on the purgatory object. Note that at this point the bank account record has not yet been changed in the system. To apply the changes, you just need to call the ‘approve!’ method:
purgatory = bank_account.purgatories.last
purgatory.approve!(current_user)
The Purgatory gem is available as open source on GitHub here. For information on all of the cool features that Purgatory provides, please take a look at our detailed documentation. Also if you have your own ideas of how to improve Purgatory, you can create an issue on the GitHub project or even fork the project and submit a pull request.