As part of the work I’ve been doing at Site5.com1 (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.
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).
Install with:
$ svn export http://svn.protocool.com/public/plugins/enumerations_mixin/
Created by Trevor Squires.
See also Plugins