Method: EnumTranslate::ClassMethods#human_attribute_options

Defined in:
lib/enum_translate/enum_translate_concern.rb

#human_attribute_options(attribute_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/enum_translate/enum_translate_concern.rb', line 19

def human_attribute_options(attribute_name)
  attribute_values = public_send(attribute_name.to_s.pluralize)

  # `attribute_values` is a return value of calling ActiveRecord::Enum attribute class API.
  #  It should be a ActiveSupport::HashWithIndifferentAccess value, and raise an error if it is not inherited a Hash class.
  raise EnumTranslateArgumentError, "#{name}.#{attribute_name} should be Hash-type value." unless attribute_values.is_a?(Hash)

  option_values_hash = I18n.t "activerecord.attributes.#{name.underscore}/#{attribute_name}", default: attribute_values

  attribute_values.keys.map { |attribute_value| [option_values_hash[attribute_value.intern], attribute_value] }
end