public
Description:
Homepage:
Clone URL: git://github.com/giraffesoft/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 [giraffesoft]
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 Fri Jun 26 12:19:09 -0700 2009 Version bump to 0.2.0 [giraffesoft]
file enum_field.gemspec Fri Jun 26 12:19:17 -0700 2009 Regenerated gemspec for version 0.2.0 [giraffesoft]
file init.rb Mon Jan 21 10:00:37 -0800 2008 add the rest of the plugin [giraffesoft]
directory lib/ Wed Jun 17 16:33:33 -0700 2009 Make sure that EnumField is loaded when gem is ... [hardbap]
directory rails/ Tue Feb 10 14:39:55 -0800 2009 Make the thing load properly as a gem plugin [webmat]
directory test/ Wed Feb 18 18:03:25 -0800 2009 Added tests for unicode and special chars [tsaleh]
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