Module: HumanAttributeValues::ClassMethods

Defined in:
lib/human_attribute_values/human_attribute_value.rb

Instance Method Summary collapse

Instance Method Details

#human_attribute_value(attribute, value, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/human_attribute_values/human_attribute_value.rb', line 12

def human_attribute_value(attribute, value, options = {})
  return '' if value.nil?
  # do not translate values of associations
  return '' if reflect_on_all_associations.any? { |assoc| assoc.name == attribute.to_sym }
  options = { count: 1 }.merge!(options)
  parts = attribute.to_s.split(".")
  attribute = parts.pop
  namespace = parts.join("/") unless parts.empty?
  value_scope = "#{self.i18n_scope}.values"
  # dots would mean a new nesting level in YAML files
  key = value.is_a?(Numeric) ? value.to_s.gsub('.', '_') : value
  if namespace
    defaults = lookup_ancestors.map do |klass|
      :"#{value_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}.#{key}"
    end
    defaults << :"#{value_scope}.#{namespace}.#{attribute}.#{key}"
  else
    defaults = lookup_ancestors.map do |klass|
      :"#{value_scope}.#{klass.model_name.i18n_key}.#{attribute}.#{key}"
    end
  end
  defaults << :"values.#{attribute}.#{key}"
  defaults << options.delete(:default) if options[:default]
  defaults << value.to_s
  options[:default] = defaults
  I18n.translate(defaults.shift, options)
end