Class: Spotlight::BlacklightConfiguration

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BlacklightConfigurationDefaults
Defined in:
app/models/spotlight/blacklight_configuration.rb

Overview

Exhibit-specific blacklight configuration model rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Instance Method Details

#blacklight_configObject

Serialize this configuration to a Blacklight::Configuration object appropriate to the current view. If a value isn’t set in this record, it will use the configuration set upstream (in default_blacklight_config) rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize

Parameters:

  • view (String)

    the configuration may be different depending on the index view selected



82
83
84
85
86
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/spotlight/blacklight_configuration.rb', line 82

def blacklight_config
  @blacklight_config ||= begin
    # Create a new config based on the defaults
    config = default_blacklight_config.inheritable_copy(self.class)

    config.current_exhibit = exhibit

    config.document_presenter_class = lambda do |context|
      if context.action_name == 'index'
        config.index.document_presenter_class
      else
        config.show.document_presenter_class
      end
    end

    config.show.merge! show if show.present?
    config.index.merge! index if index.present?
    config.index.respond_to[:iiif_json] = true

    config.index.thumbnail_field ||= Spotlight::Engine.config.thumbnail_field

    config.add_results_collection_tool 'curator_actions', if: :render_curator_actions?

    unless config.curator_actions
      config.curator_actions ||= Blacklight::NestedOpenStructWithHashAccess.new(Blacklight::Configuration::ToolConfig)
      config.curator_actions.save_search!
      config.curator_actions.bulk_actions!
    end

    unless config.bulk_actions
      config.bulk_actions ||= Blacklight::NestedOpenStructWithHashAccess.new(Blacklight::Configuration::ToolConfig)

      config.bulk_actions.change_visibility!
      config.bulk_actions.add_tags!
      config.bulk_actions.remove_tags!
    end

    config.default_solr_params = config.default_solr_params.merge(default_solr_params)

    config.default_per_page = default_per_page if default_per_page

    config.view.embed!
    # This is blacklight-gallery's openseadragon partial
    unless config.view.embed.document_component
      config.view.embed.partials ||= ['openseadragon']
      config.view.embed.document_component = Spotlight::SolrDocumentLegacyEmbedComponent
    end
    config.view.embed.if = false

    # blacklight-gallery requires tile_source_field
    config.view.embed.tile_source_field ||= config.show.tile_source_field
    config.view.embed.locals ||= { osd_container_class: '' }

    # Add any custom fields
    config.index_fields.merge! custom_index_fields(config)
    config.index_fields.reject! { |_k, v| v.if == false }

    # Update with customizations
    config.index_fields.each do |k, v|
      v.original = v.dup
      if index_fields[k]
        v.merge! index_fields[k].symbolize_keys
      elsif v.custom_field
        set_custom_field_defaults(v)
      else
        set_index_field_defaults(v)
      end

      v.immutable = Blacklight::OpenStructWithHashAccess.new(v.immutable)
      v.merge! v.immutable.to_h.symbolize_keys

      v.if = :field_enabled? unless v.if == false

      v.normalize! config
      v.validate!
    end

    config.show_fields.reject! { |_k, v| v.if == false }

    config.show_fields.reject { |k, _v| config.index_fields[k] }.each do |k, v|
      v.original = v.dup
      config.index_fields[k] = v

      if index_fields[k]
        v.merge! index_fields[k].symbolize_keys
      else
        set_show_field_defaults(v)
      end

      v.immutable = Blacklight::OpenStructWithHashAccess.new(v.immutable)
      v.merge! v.immutable.to_h.symbolize_keys

      v.if = :field_enabled? unless v.if == false

      v.normalize! config
      v.validate!
    end

    ##
    # Sort after the show fields have also been added
    config.index_fields = Hash[config.index_fields.sort_by { |k, _v| field_weight(index_fields, k) }]

    config.show_fields = config.index_fields

    config.search_fields.merge! custom_search_fields(config)
    if search_fields.present?
      config.search_fields = Hash[config.search_fields.sort_by { |k, _v| field_weight(search_fields, k) }]

      config.search_fields.each do |k, v|
        v.original = v.dup
        v.if = :field_enabled? unless v.if == false
        next if search_fields[k].blank?

        v.merge! search_fields[k].symbolize_keys
        v.normalize! config
        v.validate!
      end
    end

    if sort_fields.present?
      config.sort_fields = Hash[config.sort_fields.sort_by { |k, _v| field_weight(sort_fields, k) }]

      config.sort_fields.each do |k, v|
        v.original = v.dup
        v.if = :field_enabled? unless v.if == false
        next if sort_fields[k].blank?

        v.merge! sort_fields[k].symbolize_keys
        v.normalize! config
        v.validate!
      end
    end

    config.facet_fields.merge! custom_facet_fields
    if facet_fields.present?
      config.facet_fields = Hash[config.facet_fields.sort_by { |k, _v| field_weight(facet_fields, k) }]

      config.facet_fields.each do |k, v|
        v.original = v.dup unless v.custom_field
        next if facet_fields[k].blank?

        v.merge! facet_fields[k].symbolize_keys
        v.enabled = v.show
        v.if = :field_enabled? unless v.if == false
        v.normalize! config
        v.validate!
      end
    end

    config.per_page = (config.per_page & per_page) if per_page.present?

    if document_index_view_types.present?
      config.view.each do |k, v|
        v.original = v.dup
        v.key = k
        v.if = :enabled_in_spotlight_view_type_configuration? unless v.if == false
      end
    end

    if config.search_fields.blank?
      config.navbar.partials[:saved_searches].if = false if config.navbar.partials.key? :saved_searches
      config.navbar.partials[:search_history].if = false if config.navbar.partials.key? :search_history
    end

    config
  end
end

#custom_facet_fieldsObject



263
264
265
266
267
268
269
270
271
272
273
# File 'app/models/spotlight/blacklight_configuration.rb', line 263

def custom_facet_fields
  Hash[exhibit.custom_fields.facetable.reject(&:new_record?).map do |x|
    field = Blacklight::Configuration::FacetField.new x.configuration.merge(
      key: x.field, field: x.solr_field, show: false, custom_field: true
    )
    field.if = :field_enabled?
    field.enabled = false
    field.limit = true
    [x.field, field]
  end]
end

#custom_index_fields(blacklight_config) ⇒ Object

rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize



251
252
253
254
255
256
257
258
259
260
261
# File 'app/models/spotlight/blacklight_configuration.rb', line 251

def custom_index_fields(blacklight_config)
  Hash[exhibit.custom_fields.reject(&:new_record?).map do |custom_field|
    original_config = blacklight_config.index_fields[custom_field.field] || {}
    field = Blacklight::Configuration::IndexField.new original_config.merge(
      custom_field.configuration.merge(
        key: custom_field.field, field: custom_field.solr_field, custom_field: true
      )
    )
    [custom_field.field, field]
  end]
end

#custom_search_fields(blacklight_config) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
# File 'app/models/spotlight/blacklight_configuration.rb', line 275

def custom_search_fields(blacklight_config)
  Hash[exhibit.custom_search_fields.reject(&:new_record?).map do |custom_field|
    original_config = blacklight_config.search_fields[custom_field.field] || {}
    field = Blacklight::Configuration::SearchField.new original_config.merge(
      custom_field.configuration.merge(
        key: custom_field.slug, solr_parameters: { qf: custom_field.field }, custom_field: true
      )
    )
    [custom_field.slug, field]
  end]
end

#default_blacklight_configObject

Get the “upstream” blacklight configuration to use



289
290
291
292
293
294
295
# File 'app/models/spotlight/blacklight_configuration.rb', line 289

def default_blacklight_config
  @default_blacklight_config ||= begin
    config = Spotlight::Engine.blacklight_config.deep_copy
    add_exhibit_specific_fields(config)
    config
  end
end

#document_index_view_types=(hash_or_array) ⇒ Object

Parse params checkbox arrays into simple arrays. A group of checkboxes on a form returns values like this:

{"list"=>"1", "gallery"=>"1", "map"=>"0"}

where, “list” and “gallery” are selected and “map” is not. This function digests that hash into a list of selected values. e.g.:

["list", "gallery"]


303
304
305
306
307
308
309
# File 'app/models/spotlight/blacklight_configuration.rb', line 303

def document_index_view_types=(hash_or_array)
  if hash_or_array.is_a? Hash
    super(hash_or_array.select { |_, checked| checked == '1' }.keys)
  else
    super(hash_or_array)
  end
end

#document_index_view_types_selected_hashOpenStructWithHashAccess

A group of checkboxes on a form needs values like this:

{"list"=>"1", "gallery"=>"1", "map"=>"0"}

where, “list” and “gallery” are selected and “map” is not. This function takes [“list”, “gallery”] and turns it into the above.

Returns:

  • (OpenStructWithHashAccess)

    keys are view types; value is 1 if enabled



316
317
318
319
320
321
322
323
324
# File 'app/models/spotlight/blacklight_configuration.rb', line 316

def document_index_view_types_selected_hash
  selected_view_types = document_index_view_types
  avail_view_types = default_blacklight_config.view.to_h.reject { |_k, v| v.if == false }.keys
  Blacklight::OpenStructWithHashAccess.new.tap do |s|
    avail_view_types.each do |k|
      s[k] = selected_view_types.include?(k.to_s)
    end
  end
end