Skip to content

jthopple/enum_field

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnumField

Macro to help with enumerations in an Active Record column.

Usage

This gem 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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%