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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'app/models/spotlight/blacklight_configuration.rb', line 68

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

    config.show.merge! show unless show.blank?
    config.index.merge! index unless index.blank?

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

    config.add_results_collection_tool 'save_search', if: :render_save_this_search?

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

    config.view.embed.partials ||= ['openseadragon']
    config.view.embed.if = false
    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

    unless search_fields.blank?
      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

    unless sort_fields.blank?
      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
    unless facet_fields.blank?
      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) unless per_page.blank?

    if default_per_page
      config.per_page.delete(default_per_page)
      config.per_page.unshift(default_per_page)
    end

    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 unless document_index_view_types.blank?

    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



216
217
218
219
220
221
222
223
224
225
226
# File 'app/models/spotlight/blacklight_configuration.rb', line 216

def custom_facet_fields
  Hash[exhibit.custom_fields.vocab.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



204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/spotlight/blacklight_configuration.rb', line 204

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

#default_blacklight_configObject

Get the “upstream” blacklight configuration to use



230
231
232
233
234
235
236
# File 'app/models/spotlight/blacklight_configuration.rb', line 230

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"]


244
245
246
247
248
249
250
# File 'app/models/spotlight/blacklight_configuration.rb', line 244

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



257
258
259
260
261
262
263
264
265
# File 'app/models/spotlight/blacklight_configuration.rb', line 257

def document_index_view_types_selected_hash
  selected_view_types = document_index_view_types
  avail_view_types = default_blacklight_config.view.select { |_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