Gmane
From: Trevor Squires <trevor@...>
Subject: [ANN] New Plugin - ActiveRecord Enumerations
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2005-10-25 20:47:33 GMT (2 years, 49 weeks, 4 days, 21 hours and 47 minutes ago)
Hey folks,

I'm pleased to announce the release of a new rails plugin for 
ActiveRecord.  acts_as_enumerated, has_enumerated and 
ActiveRecord::VirtualEnumerations.

What is it?

As part of the work I've been doing at Site5.com[1] (who have very 
graciously allowed me to release this code into the wild) I put 
together an extension to treat an ActiveRecord model as an enumeration. 
  I.e.:

class BookingStatus < ActiveRecord::Base
   acts_as_enumerated, :order => 'position ASC'
end

booking.status = BookingStatus[:confirmed]

Values for acts_as_enumerated models are cached (reducing hits to the 
database) and you can define 'virtual' acts_as_enumeration classes, 
eliminating the need to clutter your models directory with classes that 
do nothing more than acts_as_enumerated.

The has_enumerated macro allows your models to refer to 
acts_as_enumerated instances without resorting to using belongs_to 
(which can require you to jump through hoops to avoid the database hit 
when retrieving the enumeration value).

I should point out that this isn't the first time something like this 
has been done.  Michael Shuerig sent a mail [2] to the list a while 
back (when I was first looking at this feature) outlining how he was 
adding enumerations to his Boilerplate library.  Boilerplate's 
enumerations differ from this plugin in some pretty significant ways 
but Michael's technique of hooking into Module.const_missing was the 
missing piece of the puzzle I needed to eliminate clutter in the models 
directory - I'm very grateful for that.

How is it installed?

Well, the new rails 'plugins' feature makes packaging and releasing 
extensions a *lot* more straight forward.

First of all, you need to be running a plugin-compatible version of 
rails (0.14 and above).  Then you simply:

cd vendor/plugins
svn export 
http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk 
enumerations_mixin

The README_ENUMERATIONS file should be enough to get you started - when 
I manage to scrape together some spare time I'll insert some 
(hopefully) more helpful rdoc.

What's missing?

Well, proper documentation, for a start.  Apart from that it's fully 
functional.

Regards,
Trevor

[1] - in a shameless plug, Site5 is poised to release a rather nifty 
rails app.  See http://engineering.site5.com for a teaser.
[2] - Michael's email is here: 
http://thread.gmane.org/gmane.comp.lang.ruby.rails/19193

--
Trevor Squires
http://somethinglearned.com