Module: ActiveRecord::EnumTranslation
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/activerecord/enum_translation.rb,
lib/activerecord/enum_translation/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Method Summary collapse
-
#human_enum_name_for(enum_name, count: 1, default: nil) ⇒ String?
Translates enum identifiers into a more human format, such as `“Active”` instead of `:active`.
Instance Method Details
#human_enum_name_for(enum_name, count: 1, default: nil) ⇒ String?
Translates enum identifiers into a more human format, such as `“Active”` instead of `:active`.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/activerecord/enum_translation.rb', line 18 def human_enum_name_for(enum_name, count: 1, default: nil) = { count: count } enum_value = public_send(enum_name) scope = "#{self.class.i18n_scope}.attributes" defaults = [] if enum_value defaults += self.class.lookup_ancestors.map do |ancestor| :"#{scope}.#{ancestor.model_name.i18n_key}.#{enum_name}.#{enum_value}" end defaults << :"attributes.#{enum_name}.#{enum_value}" else defaults << nil end defaults << default if default defaults << enum_value.humanize if enum_value key = defaults.shift [:default] = defaults.empty? ? nil : defaults ::I18n.t(key, **) end |