Sha256: 09d2eda2b4f6f708649be407f5eefe544e7b264a10a20eac4e99e84fde60332c

Contents?: true

Size: 1.11 KB

Versions: 35

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

35 entries across 35 versions & 4 rubygems

Version Path
actionview-5.0.7.2 lib/action_view/helpers/tags/translator.rb
actionview-5.0.7.1 lib/action_view/helpers/tags/translator.rb
actionview-5.0.7 lib/action_view/helpers/tags/translator.rb
actionview-5.0.6 lib/action_view/helpers/tags/translator.rb
actionview-5.0.6.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.0.5 lib/action_view/helpers/tags/translator.rb
actionview-5.0.5.rc2 lib/action_view/helpers/tags/translator.rb
actionview-5.0.5.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.0.4 lib/action_view/helpers/tags/translator.rb
actionview-5.0.4.rc1 lib/action_view/helpers/tags/translator.rb
actionview-5.0.3 lib/action_view/helpers/tags/translator.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionview-5.0.2/lib/action_view/helpers/tags/translator.rb
actionview-5.0.2 lib/action_view/helpers/tags/translator.rb
actionview-5.0.2.rc1 lib/action_view/helpers/tags/translator.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionview-5.0.1/lib/action_view/helpers/tags/translator.rb