Sha256: afd19a8db7986e1626bc7b06b9d1b858e41047cd5be92f77cb86d9569c549f52

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module EnumAttributeLocalizable
  extend ActiveSupport::Concern

  module ClassMethods
    def human_enum_value(attribute, value, options = {})
      parts     = attribute.to_s.split(".")
      attribute = parts.pop.pluralize
      attributes_scope = "#{i18n_scope}.attributes"

      if parts.any?
        namespace = parts.join("/")
        defaults = lookup_ancestors.map do |klass|
          :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}.#{value}"
        end
        defaults << :"#{attributes_scope}.#{namespace}.#{attribute}.#{value}"
      else
        defaults = lookup_ancestors.map do |klass|
          :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}.#{value}"
        end
      end

      defaults << :"attributes.#{attribute}.#{value}"
      defaults << options.delete(:default) if options[:default]
      defaults << value.to_s.humanize

      options[:default] = defaults
      I18n.translate(defaults.shift, options)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
script_core-0.2.1 spec/dummy/app/models/concerns/enum_attribute_localizable.rb
script_core-0.2.0 spec/dummy/app/models/concerns/enum_attribute_localizable.rb
script_core-0.1.1 spec/dummy/app/models/concerns/enum_attribute_localizable.rb
script_core-0.1.0 spec/dummy/app/models/concerns/enum_attribute_localizable.rb
script_core-0.0.6 spec/dummy/app/models/concerns/enum_attribute_localizable.rb
script_core-0.0.5 spec/dummy/app/models/concerns/enum_attribute_localizable.rb