Skip to content

A tiny rails plugin to assist in simulating enum db types in a db-agnostic fashion

License

Notifications You must be signed in to change notification settings

FOMNick/enum_simulator

 
 

Repository files navigation

EnumSimulator

A simple plugin for abstracting out standard ActiveRecord enumerated attributes

Installation

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

Example

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

About

A tiny rails plugin to assist in simulating enum db types in a db-agnostic fashion

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%