Sha256: 7dae755f46ded4873164919a0d6ed4b14fa7e590e4743e2bd54bc8d012623106

Contents?: true

Size: 1.11 KB

Versions: 44

Compression:

Stored size: 1.11 KB

Contents

module ActionView
  module Helpers
    module Tags # :nodoc:
      class Translator # :nodoc:
        def initialize(object, object_name, method_and_value, scope)
          @object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1')
          @method_and_value = method_and_value
          @scope = scope
          @model = object.respond_to?(:to_model) ? object.to_model : nil
        end

        def translate
          translated_attribute = I18n.t("#{object_name}.#{method_and_value}", default: i18n_default, scope: scope).presence
          translated_attribute || human_attribute_name
        end

        protected

        attr_reader :object_name, :method_and_value, :scope, :model

        private

        def i18n_default
          if model
            key = model.model_name.i18n_key
            ["#{key}.#{method_and_value}".to_sym, ""]
          else
            ""
          end
        end

        def human_attribute_name
          if model && model.class.respond_to?(:human_attribute_name)
            model.class.human_attribute_name(method_and_value)
          end
        end
      end
    end
  end
end

Version data entries

44 entries across 43 versions & 8 rubygems

Version Path
actionview-4.2.11.3 lib/action_view/helpers/tags/translator.rb
actionview-4.2.11.2 lib/action_view/helpers/tags/translator.rb
actionview-4.2.11.1 lib/action_view/helpers/tags/translator.rb
actionview-4.2.11 lib/action_view/helpers/tags/translator.rb
actionview-4.2.10 lib/action_view/helpers/tags/translator.rb
actionview-4.2.10.rc1 lib/action_view/helpers/tags/translator.rb
actionview-4.2.9 lib/action_view/helpers/tags/translator.rb
actionview-4.2.9.rc2 lib/action_view/helpers/tags/translator.rb
actionview-4.2.9.rc1 lib/action_view/helpers/tags/translator.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionview-4.2.8/lib/action_view/helpers/tags/translator.rb
actionview-4.2.8 lib/action_view/helpers/tags/translator.rb
actionview-4.2.8.rc1 lib/action_view/helpers/tags/translator.rb
actionview-4.2.7.1 lib/action_view/helpers/tags/translator.rb
actionview-4.2.7 lib/action_view/helpers/tags/translator.rb
actionview-4.2.7.rc1 lib/action_view/helpers/tags/translator.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-4.2.6/lib/action_view/helpers/tags/translator.rb
actionview-4.2.6 lib/action_view/helpers/tags/translator.rb
actionview-4.2.6.rc1 lib/action_view/helpers/tags/translator.rb
actionview-4.2.5.2 lib/action_view/helpers/tags/translator.rb
activejob-lock-0.0.2 rails/actionview/lib/action_view/helpers/tags/translator.rb