Skip to content

Instantly share code, notes, and snippets.

@Martin91
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save Martin91/1eac5a835c41e07bc41e to your computer and use it in GitHub Desktop.

Select an option

Save Martin91/1eac5a835c41e07bc41e to your computer and use it in GitHub Desktop.
Rails active record human enum values
# #{Rails.root}/config/initializers/active_record/base_decorator.rb
ActiveRecord::Base.class_eval do
def self.human_enum_values(enum_name = nil)
enum_name ||= defined_enums.keys.first
raise "Could not find a enum named #{enum_name} in #{name}" unless enum_values = defined_enums[enum_name]
_human_enum_values = {}
enum_values.each do |key, value|
translated_key = I18n.t "activerecord.enums.#{name.underscore}.#{enum_name}.#{key}"
_human_enum_values[translated_key] = value
end
_human_enum_values
end
end
# #{Rails.root}/config/locales/models.zh-CN.yml
zh-CN:
activerecord:
models:
message: 消息
attributes:
message:
send_at: 发送时间
enums:
message:
message_type:
homework: 作业
notification: 通知
survey: 调查
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment