Sha256: b9276f3f43afa5826f27e7ef2f39e3034e82283223e94d3932574683ac2c7840

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module BootstrapForm
  module Components
    module Hints
      extend ActiveSupport::Concern

      private

      def generate_help(name, help_text)
        return if help_text == false || inline_error?(name)

        help_klass ||= "form-text text-muted"
        help_text ||= get_help_text_by_i18n_key(name)
        help_tag ||= :small

        content_tag(help_tag, help_text, class: help_klass) if help_text.present?
      end

      def get_help_text_by_i18n_key(name)
        return unless object

        partial_scope = if object.class.respond_to?(:model_name)
                          object.class.model_name.name
                        else
                          object.class.name
                        end

        # First check for a subkey :html, as it is also accepted by i18n, and the
        # simple check for name would return an hash instead of a string (both
        # with .presence returning true!)
        help_text = nil
        ["#{name}.html", name, "#{name}_html"].each do |scope|
          break if help_text

          help_text = scoped_help_text(scope, partial_scope)
        end
        help_text
      end

      def scoped_help_text(name, partial_scope)
        underscored_scope = "activerecord.help.#{partial_scope.underscore}"
        downcased_scope = "activerecord.help.#{partial_scope.downcase}"

        help_text = translated_help_text(name, underscored_scope).presence

        help_text ||= if (text = translated_help_text(name, downcased_scope).presence)
                        warn "I18n key '#{downcased_scope}.#{name}' is deprecated, use '#{underscored_scope}.#{name}' instead"
                        text
                      end

        help_text
      end

      def translated_help_text(name, scope)
        ActiveSupport::SafeBuffer.new I18n.t(name, scope: scope, default: "")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/bootstrap_form-4.5.0/lib/bootstrap_form/components/hints.rb
bootstrap_form-5.0.0 lib/bootstrap_form/components/hints.rb
bootstrap_form-4.5.0 lib/bootstrap_form/components/hints.rb
bootstrap_form-4.4.0 lib/bootstrap_form/components/hints.rb
bootstrap_form-4.3.0 lib/bootstrap_form/components/hints.rb
bootstrap_form-4.2.0 lib/bootstrap_form/components/hints.rb