module EnumTranslate::ClassMethods

Public Instance Methods

human_attribute_options(attribute_name) click to toggle source
# 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