Sha256: 8a031b2c54005db6dd59ed635d74032a3c52c8481863fc0ca19126b67606e02a

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

module HumanAttributeValues
  extend ActiveSupport::Concern

  included {}

  def human_attribute_value(attribute, options = {})
    value = self.send attribute
    self.class.human_attribute_value(attribute, value, options)
  end

  module ClassMethods
    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
  end
end


ActiveRecord::Base.send :include, HumanAttributeValues

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
human_attribute_values-1.0.1 lib/human_attribute_values/human_attribute_value.rb
human_attribute_values-1.0.0 lib/human_attribute_values/human_attribute_value.rb
human_attribute_values-0.0.2 lib/human_attribute_values/human_attribute_value.rb
human_attribute_values-0.0.1 lib/human_attribute_values/human_attribute_value.rb