Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 rails
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION.yml
Octocat-spinner-32 enum_field.gemspec
Octocat-spinner-32 init.rb
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

Something went wrong with that request. Please try again.