A simple plugin for abstracting out standard ActiveRecord enumerated attributes
Let bundler do all the work for you by adding this to your Gemfile:
gem 'enum_simulator', :git => 'git://github.com/FOMNick/enum_simulator.git'
And then execute:
bundle install
Or install it manually:
sudo gem install FOMNick-enum_simulator
In order to mark an AR attribute as enumerated, make sure the column in question is a string and large enough to accommodate the string representation of your largest possible value. Then, in the model file, just call “enum”, passing it the symbol of the attribute and an array of symbols you’d like to describe as the possible values:
class IceCream < ActiveRecord::Base enum :flavor, [:chocolate, :vanilla, :strawberry, :rockyroad] end
enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that’s really all it does - it’s just such a common pattern that I figured we might as well have a more succinct way of using it.
Copyright © 2011 Centresource, released under the MIT license