ActsAsTemporary

On occasion I've had the need to store data for a short period of time within an application's database but did not want it to artificially inflate the ID numbers. This gem uses a TemporaryObject model that stores the "definition" (read: attributes) of a model. It can then be recalled at a later date. Upon saving a recalled object the temporary version is deleted.

Requirements

This project builds upon ActiveRecord. In theory this should work with nothing outside of a Rails application

Installation

This project is distributed as a gem and it should be as simple as adding the following line to your Gemfile.

gem 'acts_as_temporary', '~> 0.0.1'

You'll need to copy and run the migration from the engine.

$ cd _/your/rails/app_
$ rake acts_as_temporary_engine:install:migrations
$ rake db:migrate

Configuration

In your environment.rb file you can set the shelf life of a temporary object with the following configuration definition:

config.acts_as_temporary_shelf_life = 1.day # Set the duration to something that makes sense

By default the shelf life of a temporary object is 365.days.

Usage

Class Methods

can_be_temporary

Within the model you would like to be able to temporarily store you just need to add the following line.

can_be_temporary

For example, if you wish to make the Registration model one that can be temporary the model would look like this to start.

class Registration < ActiveRecord::Base
  can_be_temporary
end

clear_stale_objects

Rolls through the temporary objects table and clears any old temporary objects. By default anything older than 24 hours is considered stale.

Instance Methods

#store

The #store instance method takes the instance and stores it as a temporary object. The temporary object's ID is then stored within the objects @temporary_id instance variable.

#recall

The #recall instance method takes the ID of a temporary object and attempts to instantiate that data.

#is_temporary?

Returns true if the current object has a @temporary_id.

Deleting Temporary Objects

There are two methods for deleting temporary objects. The first, and most common, is to simply call save and the second is to call drop_temporary on an object that has been recalled.

#save

If the object saves without error the temporary object will be destroyed.

#drop_temporary

Deletes the associated temporary object from the database without saving the calling object

Example

registration = Registration.new(params[:registration])
registration.is_temporary? # => false

registration.store # => this object's data is stored in the database and the ID of the temporary object is interally assigned
registration.id # => nil
registration.temporary_id # => 14123
registration.is_temporary? # => true

registration = Registration.recall(14123)
registration.id # => nil
registration.is_temporary? # => true

registration.save # => true
registration.is_temporary? # => false

This project uses MIT-LICENSE.