Sha256: 6a5bedc89f31719385f2fb5af4c256590468aeb1f23edb6c1eb0625c57b5733f

Contents?: true

Size: 1.27 KB

Versions: 20

Compression:

Stored size: 1.27 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

        # 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

20 entries across 20 versions & 1 rubygems

Version Path
actionview-5.1.7 lib/action_view/helpers/tags/translator.rb
actionview-5.1.7.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.6.2 lib/action_view/helpers/tags/translator.rb
actionview-5.1.6.1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.6 lib/action_view/helpers/tags/translator.rb
actionview-5.1.5 lib/action_view/helpers/tags/translator.rb
actionview-5.1.5.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.4 lib/action_view/helpers/tags/translator.rb
actionview-5.1.4.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.3 lib/action_view/helpers/tags/translator.rb
actionview-5.1.3.rc3 lib/action_view/helpers/tags/translator.rb
actionview-5.1.3.rc2 lib/action_view/helpers/tags/translator.rb
actionview-5.1.3.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.2 lib/action_view/helpers/tags/translator.rb
actionview-5.1.2.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.0 lib/action_view/helpers/tags/translator.rb
actionview-5.1.0.rc2 lib/action_view/helpers/tags/translator.rb
actionview-5.1.0.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.1.0.beta1 lib/action_view/helpers/tags/translator.rb