Skip to content

Instantly share code, notes, and snippets.

@onemanstartup
Created August 25, 2013 15:55
Show Gist options
  • Save onemanstartup/6334632 to your computer and use it in GitHub Desktop.
Save onemanstartup/6334632 to your computer and use it in GitHub Desktop.
Everything about enums in ruby and rubyonrails from different places

Have a legacy database and need some enumerations in your models to match those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just to fetch a simple description? Or maybe use some integers instead of strings as the code for each value of your enumerations?

455/41/rails 4, activeadmin/2 days ago

I18n and ActiveRecord/Mongoid/MongoMapper rspec, helpers, activeadmin

  • Default Value
  • Formtastic support
  • Select Form Helpers
  • l18n

183/14/rails 4/17 days ago

  • Formtastic support
  • Generators
  • Modify member of enum
  • I18n
  • As a collection
  • Default Value
  • Back Reference to Owning Object
  • Validation
  • Select Form Helpers
  • Documented Easy Code

134/23/ / 6 days ago

  • Most important feature is support for string and integer values in db
  • I18n
  • Easy Code in 2 files mostly
  • Helpers
  • No Default values
class RelationshipStatus < EnumerateIt::Base
  associate_values(
    :single   => [1, 'Single'],
    :married  => [2, 'Married'],
    :widow    => [3, 'Widow'],
    :divorced => [4, 'Divorced']
  )
end
  • When calling methods like to_a and to_json, the returned values will be sorted using the translation for each one of the enumeration values. If you want to overwrite the default sort mode, you can use the sort_mode class method.
class RelationshipStatus < EnumerateIt::Base
  associate_values :married => 1, :single => 2

  sort_by :value
end
  • Not Only AR
class Person
  extend EnumerateIt
  attr_accessor :relationship_status

  has_enumeration_for :relationship_status, :with => RelationshipStatus
end
  • Have Generators

188/31/rails 3/3 month ago

meh,, not simple for ActiveModel (including validations and i18n helpers Mongoid

200/45/rails 3/ year ago

  • ActiveRecord integration

  • ActionView form helpers

  • Custom labels

  • Formtastic

  • Scaffold generator integration

  • Definable enumeration labels

  • Enum helper methods

  • Dynamic predicate methods

  • Initialization

62/23/rails 4/month ago

  • Form Helpers
  • Storage Backends + l18n
  • ordering
  • validations

Two ways. Symbols (:foo notation) or constants (FOO notation).

Symbols are appropriate when you want to enhance readability without littering code with literal strings.

postal_code[:minnesota] = "MN"
postal_code[:new_york] = "NY"

Constants are appropriate when you have an underlying value that is important. Just declare a module to hold your constants and then declare the constants within that.

module Foo
  BAR = 1
  BAZ = 2
  BIZ = 4
end

flags = Foo::BAR | Foo::BAZ # flags = 3

http://schneide.wordpress.com/2010/12/13/avoid-switch-use-enum/ http://programmers.stackexchange.com/questions/191063/should-classes-enums-and-other-entities-be-placed-in-separate-files http://simpleprogrammer.com/2012/02/21/refactoring-switches-to-classes/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment