public
Fork of giraffesoft/enum_field
Description:
Homepage:
Clone URL: git://github.com/technicalpickles/enum_field.git
name age message
file .gitignore Mon Feb 09 16:51:41 -0800 2009 Ignore a few basic things [webmat]
file MIT-LICENSE Mon Jan 21 09:56:43 -0800 2008 fill in some basic meta data [jamesgolick]
file README.rdoc Tue Feb 10 12:26:55 -0800 2009 add a missing 'out of this world' choice [webmat]
file Rakefile Tue Feb 10 10:40:49 -0800 2009 Rename Readme to rdoc [webmat]
file VERSION.yml Tue Apr 07 09:46:43 -0700 2009 Version bump to 0.1.2 [technicalpickles]
file enum_field.gemspec Tue Apr 07 09:47:03 -0700 2009 Regenerated gemspec for version 0.1.2 [technicalpickles]
file init.rb Mon Jan 21 10:00:37 -0800 2008 add the rest of the plugin [jamesgolick]
directory lib/ Tue Apr 07 12:02:38 -0700 2009 Support allow_nil option. [technicalpickles]
directory rails/ Tue Feb 10 14:39:55 -0800 2009 Make the thing load properly as a gem plugin [webmat]
directory test/ Tue Apr 07 12:02:38 -0700 2009 Support allow_nil option. [technicalpickles]
README.rdoc

EnumField

Macro to emulate a MySQL enum_field type thing.

Usage

This plugin encapsulates a validates_inclusion_of and automatically gives you a few more goodies automatically. That’s it!

    class Computer < ActiveRecord:Base
      enum_field :status, ['on', 'off', 'standby', 'sleep', 'out of this world']

      # Optionally with a message to replace the default one
      # enum_field :status, ['on', 'off', 'standby', 'sleep', 'out of this world'], :message => "incorrect status"

      #...
    end

This will give you a few things:

  • add a validates_inclusion_of with a simple error message ("invalid #{field}") or your custom message
  • define the following query methods, in the name of expressive code:
    • on?
    • off?
    • standby?
    • sleep?
    • out_of_this_world?
  • define the STATUSES constant, which contains the acceptable values

License

Copyright © 2008 James Golick, released under the MIT license