Announcement: New Rails Plugin

Posted by trevor Tue, 25 Oct 2005 20:33:00 GMT

I’m pleased to announce the release of a new rails plugin for ActiveRecord, chock full o’goodness from:

  • acts_as_enumerated
  • has_enumerated
  • ActiveRecord::VirtualEnumerations

What is it?

As part of the work I’ve been doing at Site5.com (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 to the rails 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.

Enjoy!

Update

Years later and this plugin is now hosted on github

Comments are disabled