Sha256: e5de6ba1a73f22ae1ccc2d424f637548b8455dffd24e8788e87cd4e61d686155

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true
module Blacklight
  class Configuration::Field < OpenStructWithHashAccess
    def normalize! _blacklight_config = nil
      self.field ||= key
      self.key ||= self.field

      self.label ||= default_label

      self.if = true if self.if.nil?
      self.unless = false if self.unless.nil?

      self.field &&= self.field.to_s

      self
    end

    def validate!
      raise ArgumentError, "Must supply a field name" if self.field.nil?
    end

    def display_label(context = nil)
      field_label(
        (:"blacklight.search.fields.#{context}.#{key}" if context),
        :"blacklight.search.fields.#{key}",
        label,
        default_label
      )
    end

    def default_label
      if self.key.respond_to?(:titleize)
        self.key.try(:titleize)
      else
        self.key.to_s.titleize
      end
    end

    private

    ##
    # Look up the label for a solr field.
    #
    # @overload label
    #   @param [Symbol] an i18n key
    #
    # @overload label, i18n_key, another_i18n_key, and_another_i18n_key
    #   @param [String] default label to display if the i18n look up fails
    #   @param [Symbol] i18n keys to attempt to look up
    #     before falling  back to the label
    #   @param [Symbol] any number of additional keys
    #   @param [Symbol] ...
    def field_label *i18n_keys
      first, *rest = i18n_keys.compact

      I18n.t(first, default: rest)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
blacklight-7.5.1 lib/blacklight/configuration/field.rb
blacklight-7.5.0 lib/blacklight/configuration/field.rb
blacklight-7.4.2 lib/blacklight/configuration/field.rb
blacklight-7.4.1 lib/blacklight/configuration/field.rb
blacklight-7.4.0 lib/blacklight/configuration/field.rb
blacklight-7.3.0 lib/blacklight/configuration/field.rb
blacklight-7.2.0 lib/blacklight/configuration/field.rb
blacklight-7.1.0 lib/blacklight/configuration/field.rb
blacklight-7.1.0.alpha lib/blacklight/configuration/field.rb
blacklight-7.0.1 lib/blacklight/configuration/field.rb
blacklight-7.0.0 lib/blacklight/configuration/field.rb
blacklight-7.0.0.rc2 lib/blacklight/configuration/field.rb
blacklight-7.0.0.rc1 lib/blacklight/configuration/field.rb