Skip to content

Instantly share code, notes, and snippets.

@johnnaegle
Created February 10, 2015 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnaegle/67c2b7b22e22b21d4577 to your computer and use it in GitHub Desktop.
Save johnnaegle/67c2b7b22e22b21d4577 to your computer and use it in GitHub Desktop.
module EnumHelper
def enum_to_translated_option(klass, enum, enum_value, default = enum_value.to_s.titleize)
key = "activerecord.attributes.#{klass.to_s.underscore.gsub('/', '_')}.#{enum}.#{enum_value}"
translated = I18n.t(key, :default => default)
end
def enums_to_translated_options_array(klass, enum)
enum_values = klass.send(enum)
enum_values.map do |enum_value, db_value|
translated = enum_to_translated_option(klass, enum, enum_value)
[translated, enum_value]
end.reject {|translated, enum_value| translated.blank?}
end
def options_for_enum(klass, enum, object)
options = enums_to_translated_options_array(klass, enum.to_s.pluralize)
options_for_select(options, object.send(enum))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment