Sha256: 500c99297765117e1f14acb3a20603074fb6eb4f06c087084e6a9fc9b2932b37

Contents?: true

Size: 1.15 KB

Versions: 160

Compression:

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

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

          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

160 entries across 153 versions & 14 rubygems

Version Path
omg-actionview-8.0.0.alpha1 lib/action_view/helpers/tags/translator.rb
actionview-7.1.4 lib/action_view/helpers/tags/translator.rb
actionview-7.2.1 lib/action_view/helpers/tags/translator.rb
actionview-7.2.0 lib/action_view/helpers/tags/translator.rb
actionview-7.2.0.rc1 lib/action_view/helpers/tags/translator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionview-7.0.8.4/lib/action_view/helpers/tags/translator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionview-7.1.3.4/lib/action_view/helpers/tags/translator.rb
actionview-7.2.0.beta3 lib/action_view/helpers/tags/translator.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/helpers/tags/translator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/helpers/tags/translator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/helpers/tags/translator.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/actionview-7.1.3.4/lib/action_view/helpers/tags/translator.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionview-7.1.3.4/lib/action_view/helpers/tags/translator.rb
actionview-7.2.0.beta2 lib/action_view/helpers/tags/translator.rb
actionview-7.1.3.4 lib/action_view/helpers/tags/translator.rb
actionview-7.0.8.4 lib/action_view/helpers/tags/translator.rb
actionview-6.1.7.8 lib/action_view/helpers/tags/translator.rb
actionview-7.2.0.beta1 lib/action_view/helpers/tags/translator.rb
actionview-7.1.3.2 lib/action_view/helpers/tags/translator.rb
actionview-7.1.3.1 lib/action_view/helpers/tags/translator.rb