Sha256: 46b2b3c93ebb146289c8ba15a30ead34644641882a6bb56babcef40f9b83b1b0

Contents?: true

Size: 1.51 KB

Versions: 15

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # A class to model the configuration required to build the Document Upload form.
  # This configuration is also used in other places around the application (e.g. Metadata Field Config)
  # See Spotlight::Engine.config.upload_fields for where this is consumed
  # We should look into changing this to a standard blacklight field config in Blacklight 7
  class UploadFieldConfig
    attr_reader :blacklight_options, :field_name, :form_field_type
    def initialize(blacklight_options: {}, field_name:, form_field_type: :text_field, label: nil, solr_fields: nil)
      @blacklight_options = blacklight_options
      @field_name = field_name
      @form_field_type = form_field_type
      @solr_fields = solr_fields
      @label = label || field_name
    end

    # Allows a proc to be set as the label
    def label
      return @label.call if @label.is_a?(Proc)

      @label
    end

    # aliasing for backwards compatability and consistency with blacklight config
    alias solr_field field_name

    # providing backwards compatibility with the old way of configuring upload fields
    def solr_fields
      @solr_fields || Array(solr_field || field_name)
    end

    def data_to_solr(value)
      solr_fields.each_with_object({}) do |solr_field, solr_hash|
        if solr_field.is_a? Hash
          solr_field.each do |name, lambda|
            solr_hash[name] = lambda.call(value)
          end
        else
          solr_hash[solr_field] = value
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
blacklight-spotlight-3.0.0.alpha.10 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.9 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.8 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.7 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.6 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.5 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.4 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.3 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.2 lib/spotlight/upload_field_config.rb
blacklight-spotlight-3.0.0.alpha.1 lib/spotlight/upload_field_config.rb
blacklight-spotlight-2.13.0 lib/spotlight/upload_field_config.rb
blacklight-spotlight-2.12.1 lib/spotlight/upload_field_config.rb
blacklight-spotlight-2.12.0 lib/spotlight/upload_field_config.rb
blacklight-spotlight-2.11.0 lib/spotlight/upload_field_config.rb
blacklight-spotlight-2.10.0 lib/spotlight/upload_field_config.rb