Module: Blacklight::Configuration::Fields

Extended by:
ActiveSupport::Concern, Deprecation
Included in:
Blacklight::Configuration
Defined in:
lib/blacklight/configuration/fields.rb

Overview

This mixin provides Blacklight::Configuration with generic solr fields configuration

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_blacklight_field(config_key, field_key) ⇒ Object #add_blacklight_field(config_key) ⇒ Object #add_blacklight_field(config_key, options) ⇒ Object #add_blacklight_field(config_key, field) ⇒ Object #add_blacklight_field(config_key, fields) ⇒ Object

Add a solr field configuration to the given configuration key

The recommended and strongly encouraged format is a field name, configuration pair, e.g.:

add_blacklight_field :index_field, 'format', :label => 'Format'

Alternative formats include:

  • a field name and block format:

  • a plain block:

  • a configuration hash:

  • a Field instance:

  • an array of hashes:

Overloads:

  • #add_blacklight_field(config_key, field_key) ⇒ Object

    Parameters:

    • config_key (Symbol)
    • field_key (Symbol, String)

      add_blacklight_field :index_field, 'format' do |field|

      field.label = 'Format'
      

      end

  • #add_blacklight_field(config_key) ⇒ Object

    Parameters:

    • config_key (Symbol)

      add_blacklight_field :index_field do |field|

      field.field = 'format'
      field.label = 'Format'
      

      end

  • #add_blacklight_field(config_key, options) ⇒ Object

    Parameters:

    • config_key (Symbol)
    • options (Hash)

      add_blacklight_field :index_field, :field => 'format', :label => 'Format'

  • #add_blacklight_field(config_key, field) ⇒ Object

    Parameters:

  • #add_blacklight_field(config_key, fields) ⇒ Object

    Parameters:

    • config_key (Symbol)
    • fields (Array<Blacklight::Configuration::Field, Hash>)

      add_blacklight_field :index_field, [{ :field => 'format', :label => 'Format' }, IndexField.new(:field => 'date', :label => 'Date')]



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/blacklight/configuration/fields.rb', line 87

def add_blacklight_field config_key, *args, &block
  field_config = case args.first
    when String
      field_config_from_key_and_hash(config_key, *args)
    when Symbol
      args[0] = args[0].to_s
      field_config_from_key_and_hash(config_key, *args)
    when Array
      field_config_from_array(config_key, *args, &block)
      return # we've iterated over the array above.
    else
      field_config_from_field_or_hash(config_key, *args)
  end

  if (field_config.field || field_config.key).to_s =~ /\*/
    field_config.match = Regexp.new("^" + (field_config.field || field_config.key).to_s.gsub('*', '.+') + "$")
  end

  # look up any dynamic fields
  if field_config.match
    handle_matching_fields(config_key, field_config, &block)
    return
  end

  if block_given?
    yield field_config
  end

  field_config.normalize!(self)
  field_config.validate!

  raise "A #{config_key} with the key #{field_config.key} already exists." if self[config_key.pluralize][field_config.key].present?

  self[config_key.pluralize][ field_config.key ] = field_config
end

#field_config_from_array(config_key, array_of_fields_or_hashes, &block) ⇒ Object (protected)

Add multiple solr fields using a hash or Field instance



173
174
175
176
177
# File 'lib/blacklight/configuration/fields.rb', line 173

def field_config_from_array config_key, array_of_fields_or_hashes, &block
  array_of_fields_or_hashes.map do |field_or_hash|
    add_blacklight_field(config_key, field_or_hash, &block)
  end
end

#field_config_from_field_or_hash(config_key, field_or_hash = {}) ⇒ Object (protected)

Add a solr field using a hash or Field instance



180
181
182
# File 'lib/blacklight/configuration/fields.rb', line 180

def field_config_from_field_or_hash config_key, field_or_hash = {}
  hash_arg_to_config(field_or_hash, field_class_from_key(config_key))
end

#field_config_from_key_and_hash(config_key, field_name, field_or_hash = {}) ⇒ Object (protected)

Add a solr field by a solr field name and hash



166
167
168
169
170
# File 'lib/blacklight/configuration/fields.rb', line 166

def field_config_from_key_and_hash config_key, field_name, field_or_hash = {}
  field_config = field_config_from_field_or_hash(config_key, field_or_hash)
  field_config.key = field_name
  field_config
end

#handle_matching_fields(config_key, field_config, &block) ⇒ Object (protected)

Using reflection into the index, add any fields in the index that match the field_config



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/blacklight/configuration/fields.rb', line 127

def handle_matching_fields(config_key, field_config, &block)
  salient_fields = reflected_fields.select do |k, _v|
    k =~ field_config.match
  end

  salient_fields.each_key do |field|
    config = field_config.dup
    config.match = nil
    config.field = field
    config.key = field
    if self[config_key.pluralize][config.key]
      self[config_key.pluralize][config.key] = config.merge(self[config_key.pluralize][config.key])
    else
      add_blacklight_field(config_key, config, &block)
    end
  end
end

#hash_arg_to_config(hash_arg, klass) ⇒ Object (protected)

for our add_* methods, takes the optional hash param, and makes it into a specific config OpenStruct, like FacetField or SearchField. Or if the param already was one, that's cool. Or if the param is nil, make an empty one. Second argument is an actual class object.



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/blacklight/configuration/fields.rb', line 189

def hash_arg_to_config(hash_arg, klass)
  case hash_arg
  when Hash
    klass.new(hash_arg)
  when NilClass
    klass.new
  else
    # this assumes it already is an element of klass, or acts like one,
    # if not something bad will happen later, that's your problem.
    hash_arg
  end
end

#reflected_fieldsObject (protected) Also known as: luke_fields



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/blacklight/configuration/fields.rb', line 145

def reflected_fields
  if @table[:reflected_fields] == false
    return nil
  end

  @table[:reflected_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/reflected_fields", expires_in: 1.hour) do
    begin
      repository = repository_class.new(self)
      repository.reflect_fields
    rescue => e
      Blacklight.logger.warn "Error retrieving field metadata: #{e}"
      false
    end
  end

  @table[:reflected_fields] || {}
end