Last active
August 29, 2015 14:21
-
-
Save Martin91/1eac5a835c41e07bc41e to your computer and use it in GitHub Desktop.
Rails active record human enum values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # #{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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # #{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