Sha256: e748c87503c2f925412810d5ad9ba512eb743767cf8bf27bc5f7dad733c53d63

Contents?: true

Size: 1.3 KB

Versions: 90

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

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

        # TODO Change this to private once we've dropped Ruby 2.2 support.
        # Workaround for Ruby 2.2 "private attribute?" warning.
        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

90 entries across 90 versions & 5 rubygems

Version Path
actionview-5.2.8.1 lib/action_view/helpers/tags/translator.rb
actionview-5.2.8 lib/action_view/helpers/tags/translator.rb
actionview-5.2.7.1 lib/action_view/helpers/tags/translator.rb
actionview-5.2.7 lib/action_view/helpers/tags/translator.rb
actionview-5.2.6.3 lib/action_view/helpers/tags/translator.rb
actionview-5.2.6.2 lib/action_view/helpers/tags/translator.rb
actionview-5.2.6.1 lib/action_view/helpers/tags/translator.rb
actionview-5.2.6 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.6 lib/action_view/helpers/tags/translator.rb
actionview-5.2.5 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.5 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.4 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.3 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.2 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.1 lib/action_view/helpers/tags/translator.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/translator.rb
actionview-5.2.4 lib/action_view/helpers/tags/translator.rb
actionview-5.2.4.rc1 lib/action_view/helpers/tags/translator.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/translator.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/translator.rb