Module: SearchHelper

Defined in:
frontend/app/helpers/search_helper.rb

Defined Under Namespace

Classes: SearchColumn

Constant Summary collapse

IDENTIFIER_FOR_SEARCH_RESULT_LOOKUP =
{
  "accession"                => "identifier",
  "agent_corporate_entity"   => "authority_id",
  "agent_family"             => "authority_id",
  "agent_person"             => "authority_id",
  "agent_software"           => "authority_id",
  "archival_object"          => "component_id",
  "assessment"               => "assessment_id",
  "classification"           => "identifier",
  "classification_term"      => "identifier",
  "digital_object"           => "digital_object_id",
  "digital_object_component" => "component_id",
  "event"                    => "refid",
  "repository"               => "repo_code",
  "resource"                 => "identifier",
  "subject"                  => "authority_id",
}

Instance Method Summary collapse

Instance Method Details

#add_actions_columnObject



281
282
283
284
# File 'frontend/app/helpers/search_helper.rb', line 281

def add_actions_column
  add_column(sr_only(I18n.t('search_results.actions')), {:template => 'shared/actions',
    :class => 'actions table-record-actions'})
end

#add_column(label, opts = {}, block = nil) ⇒ Object



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
# File 'frontend/app/helpers/search_helper.rb', line 161

def add_column(label, opts = {}, block = nil)
  block ||= if opts[:template]
              proc do |record|
                render_aspace_partial :partial => opts[:template], :locals => {:record => record}
              end
            else
              proc do |record|
                v = Array(record[opts[:field]] || ASUtils.json_parse(record['json'])[opts[:field]])
                if v.length > 1
                  ('ul', :style => 'padding-left: 20px;') {
                    Array(v).collect { |i|
                      ('li',
                        process(i, opts))
                    }.join.html_safe
                  }
                elsif v.length == 1
                  process(v[0], opts)
                end
              end
            end

  @columns ||= []

  if opts[:sort_by].is_a? Array
    opts[:sort_by].each do |field|
      model = opts[:model] || 'multi'
      @search_data.add_sort_field(field, I18n.t("search.#{model}.#{field}"))
    end
  elsif opts[:sortable]
    opts[:sort_by] ||= opts[:field]
    @search_data.add_sort_field(opts[:sort_by], label)
  end

  col = SearchColumn.new(label, block, opts, @search_data)
  @columns.insert(opts[:index] || -1, col)
end

#add_columnsObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'frontend/app/helpers/search_helper.rb', line 301

def add_columns
  return if @columns
  type = @search_data.get_type
  type = 'agent' if type.include? 'agent'
  type = 'classification' if type == 'classification_term'

  add_multiselect_column if can_delete_search_results?(type) && !(request.path =~ /\/(advanced_)*search/)
  add_linker_column if params[:linker]==='true'

  if params[:include_components]
    case type
    when 'resource'
      add_pref_columns ['resource', 'archival_object']
    when 'digital_object'
      add_pref_columns ['digital_object', 'digital_object_component']
    end
  else
    add_pref_columns(type)
  end

  add_actions_column if !params[:linker] || params[:linker] === 'false'
end

#add_linker_columnObject



286
287
288
289
290
291
292
293
294
295
# File 'frontend/app/helpers/search_helper.rb', line 286

def add_linker_column
  add_column(sr_only('Linker'), {},
    proc { |record|
      if params[:multiplicity] === 'many'
        check_box_tag "linker-item", record["id"], false, :"data-object" => record.to_json
      else
        radio_button_tag "linker-item", record["id"], false, :"data-object" => record.to_json
      end
    })
end

#add_multiselect_columnObject



214
215
216
217
218
219
220
221
222
# File 'frontend/app/helpers/search_helper.rb', line 214

def add_multiselect_column
  @allow_multiselect = true
  header = ('<label for="select_all" class="sr-only">' +
    I18n.t("search_results.selected") + '</label>' +
    check_box_tag("select_all", 1, false, "autocomplete" => "off")).html_safe

  add_column(header, {:template => 'shared/multiselect',
    :class => 'multiselect-column'})
end

#add_pref_columns(models) ⇒ Object



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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'frontend/app/helpers/search_helper.rb', line 224

def add_pref_columns(models)
  models = Array(models) unless models.is_a? Array
  added = []
  skipped = []
  if models.length > 1
    add_column(I18n.t("search.multi.primary_type"), {:field => 'primary_type', :locale_key => '_singular',
      :sortable => true, :type => 'string'})
    added << 'primary_type'
  end
  for n in 1..AppConfig[:max_search_columns]
    models.each do |model|
      prop = browse_columns["#{model}_browse_column_#{n}"]
      # we do not want to display a column for no value or the relevancy score
      next if added.include?(prop) || !prop || prop == 'no_value' || prop == 'score'
      # we may not want to display context either, in cases like:
      if prop === 'context' && model === 'archival_object' && params["context_filter_term"]
        next if params["context_filter_term"].select {|ft| JSON.parse(ft).has_key?('resource')}
      end
      opts = column_opts[model][prop]
      opts[:locale_key] ||= locales(model)[prop] || "#{model}_#{prop}"
      opts[:model] = model
      # opts[:type] ||= 'string'
      opts[:class] ||= prop

      if opts.fetch(:condition, false)
        unless opts[:condition].call(self)
          skipped << prop
          next
        end
      end

      added << prop
      unless opts[:template]
        if lookup_context.template_exists?("#{prop}_cell", "#{model}s", true)
          opts[:template] = "#{model}s/#{prop}_cell"
        elsif lookup_context.template_exists?("#{prop}_cell", model, true)
          opts[:template] = "#{model}/#{prop}_cell"
        elsif lookup_context.template_exists?("#{prop}_cell", 'search', true)
          opts[:template] = "search/#{prop}_cell"
        end
      end
      add_column(I18n.t("search.#{model}.#{prop}"), opts)
    end
  end
  models.each do |model|
    prop = browse_columns["#{model}_sort_column"]
    next if added.include?(prop) || skipped.include?(prop) || !prop
    added << prop
    opts = prop == 'score' ? { sort_by: 'score' } : column_opts[model][prop]
    @search_data.add_sort_field(opts[:sort_by] ? opts[:sort_by]: prop, I18n.t("search.#{model}.#{prop}"))
  end
end

#allow_multiselect?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'frontend/app/helpers/search_helper.rb', line 88

def allow_multiselect?
  @allow_multiselect ||= false
end

#build_search_params(opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'frontend/app/helpers/search_helper.rb', line 34

def build_search_params(opts = {})
  removing_record_type_filter = false
  Array(opts["remove_filter_term"]).each do |filter_term|
    removing_record_type_filter = true if ASUtils.json_parse(filter_term).keys.include? 'primary_type'
  end

  search_params = {}

  search_params["filter_term"] = Array(opts["filter_term"] || params["filter_term"]).clone
  search_params["filter_term"].concat(Array(opts["add_filter_term"])) if opts["add_filter_term"]
  search_params["filter_term"] = search_params["filter_term"].reject {|f| Array(opts["remove_filter_term"]).include?(f)} if opts["remove_filter_term"]
  search_params["filter_term"] = search_params["filter_term"].select {|f| SearchResultData.BASE_FACETS.include?(ASUtils.json_parse(f).keys.first)} if removing_record_type_filter

  sort = (opts["sort"] || params["sort"] || (@search_data ? @search_data.sorted? : nil))

  if sort
    sort = sort.split(', ')
    sort[1] = opts["sort2"] if opts["sort2"]
    fields = sort.uniq
    fields = fields.select {|f| multi_columns.compact.include?(f.split.first)} if removing_record_type_filter
    search_params["sort"] = fields.join(', ')
  end

  if (opts["format"] || params["format"]).blank?
    search_params.delete("format")
  else
    search_params["format"] = opts["format"] || params["format"]
  end

  search_params["multiplicity"] = params["multiplicity"] if params["multiplicity"]
  search_params["linker"] = opts["linker"] || params["linker"] || false
  search_params["type"] = opts["type"] || params["type"]
  search_params["facets"] = opts["facets"] || params["facets"] unless removing_record_type_filter
  search_params["exclude"] = opts["exclude"] || params["exclude"]
  search_params["listing_only"] = true if params["listing_only"]
  search_params["include_components"] = opts.has_key?("include_components") ? opts["include_components"] : params["include_components"]

  search_params["q"] = opts["q"] || params["q"]

  # retain advanced search params
  if params["advanced"]
    search_params["advanced"] = params["advanced"]
    params.keys.each do |param_key|
      ["op", "f", "v", "dop", "t", "top"].each do |adv_search_prefix|
        if param_key =~ /^#{adv_search_prefix}\d+/
          search_params[param_key] = params[param_key]
        end
      end
    end
  end

  search_params.reject {|k, v| k.blank? or v.blank?}
end

#can_delete_search_results?(record_type) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'frontend/app/helpers/search_helper.rb', line 113

def can_delete_search_results?(record_type)
  case record_type
  when 'accession', 'resource', 'digital_object'
    user_can? 'delete_archival_record'
  when 'assessment'
    user_can? 'delete_assessment_record'
  when 'subject'
    user_can? 'delete_subject_record'
  when 'agent'
    user_can? 'delete_agent_record'
  when 'location'
    user_can? 'update_location_record'
  when 'classification'
    user_can? 'delete_classification_record'
  when 'container_profile'
    user_can? 'update_container_profile_record'
  else
    false
  end
end

#can_edit_search_result?(record) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'frontend/app/helpers/search_helper.rb', line 92

def can_edit_search_result?(record)
  Plugins.edit_roles.each do |edit_role|
    return user_can?(edit_role.role, record['id']) if record['primary_type'] === edit_role.jsonmodel_type
  end

  return user_can?('update_container_record', record['id']) if record['primary_type'] === "top_container"
  return user_can?('update_container_profile_record') if record['primary_type'] === "container_profile"
  return user_can?('create_repository', record['id']) if record['primary_type'] === "repository"
  return user_can?('update_location_record') if record['primary_type'] === "location"
  return user_can?('update_subject_record') if record['primary_type'] === "subject"
  return user_can?('update_classification_record') if ["classification", "classification_term"].include?(record['primary_type'])
  return user_can?('update_agent_record') if Array(record['types']).include?("agent")

  return user_can?('update_accession_record') if record['primary_type'] === "accession"
  return user_can?('update_resource_record') if ["resource", "archival_object"].include?(record['primary_type'])
  return user_can?('update_digital_object_record') if ["digital_object", "digital_object_component"].include?(record['primary_type'])
  return user_can?('update_assessment_record') if record['primary_type'] === "assessment"
  return user_can?('update_event_record') if record['primary_type'] === "event"
  return user_can?('update_location_profile_record') if record['primary_type'] === "location_profile"
end

#column_optsObject



370
371
372
# File 'frontend/app/helpers/search_helper.rb', line 370

def column_opts
  @column_opts ||= SearchAndBrowseColumnConfig.columns
end

#context_ancestor(result) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'frontend/app/helpers/search_helper.rb', line 353

def context_ancestor(result)
  case
  when result['ancestors']
    ancestors = result['ancestors']
  when result['linked_instance_uris']
    ancestors = result['linked_instance_uris']
  when result['linked_record_uris']
    ancestors = result['linked_record_uris']
  when result['primary_type'] == 'top_container'
    ancestors = Array(result['collection_uri_u_sstr'])
  when result['primary_type'] == 'digital_object_component'
    ancestors = result['digital_object'].split
  else
    ancestors = ['']
  end
end

#context_separator(result) ⇒ Object



345
346
347
348
349
350
351
# File 'frontend/app/helpers/search_helper.rb', line 345

def context_separator(result)
  if result['ancestors'] || result['linked_instance_uris']
    @separator = '>'
  else
    @separator = '<br />'.html_safe
  end
end

#deleted(record) ⇒ Object



324
325
326
# File 'frontend/app/helpers/search_helper.rb', line 324

def deleted(record)
  params.has_key?("deleted_uri") and Array(params["deleted_uri"]).include?(record["id"])
end

#fieldsObject



374
375
376
377
# File 'frontend/app/helpers/search_helper.rb', line 374

def fields
  add_columns unless @columns
  @columns.collect { |col| col.field }.compact
end

#get_ancestor_title(field) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'frontend/app/helpers/search_helper.rb', line 328

def get_ancestor_title(field)
  begin
    field_json = JSONModel::HTTP.get_json(field)
  # one record might be "found in" another that is suppressed
  # so we will just ignore the error.
  rescue RecordNotFound
    return nil
  end
  unless field_json.nil?
    if field.include?('resources') || field.include?('digital_objects')
      clean_mixed_content(field_json['title'])
    else
      clean_mixed_content(field_json['display_string'])
    end
  end
end

#identifier_for_search_result(result) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'frontend/app/helpers/search_helper.rb', line 21

def identifier_for_search_result(result)
  identifier = IDENTIFIER_FOR_SEARCH_RESULT_LOOKUP.fetch(result["primary_type"], "")
  unless identifier.empty?
    if result.has_key? identifier
      identifier = result[identifier]
    else
      json       = ASUtils.json_parse(result["json"])
      identifier = json.fetch(identifier, "")
    end
  end
  identifier.to_s.html_safe
end

#locales(model) ⇒ Object



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
# File 'frontend/app/helpers/search_helper.rb', line 134

def locales(model)
  case model
  when 'resource', 'archival_object'
    {'level' => 'archival_record_level',
      'processing_priority' => 'collection_management_processing_priority'}
  when 'accession'
    {'processing_priority' => 'collection_management_processing_priority'}
  when 'subject'
    {'source' => 'subject_source', 'first_term_type' => 'subject_term_type'}
  when 'agent'
    {'source' => 'name_source', 'rules' => 'name_rule', 'primary_type' => 'agent.agent_type'}
  when 'container_profile'
    {'container_profile_dimension_units_u_sstr' => 'dimension_units'}
  when 'location_profile'
    {'location_profile_dimension_units_u_sstr' => 'dimension_units'}
  when 'top_container'
    {'type' => 'container_type'}
  when 'assessment'
    {'assessment_record_types' => '_singular'}
  when 'collection_management'
    {'parent_type' => '_singular'}
  else
    {'primary_type' => '_singular'}
  end
end

#multi_columnsObject



277
278
279
# File 'frontend/app/helpers/search_helper.rb', line 277

def multi_columns
  column_opts['multi'].collect { |col, opts| opts[:sort_by] } + ['create_time', 'user_mtime']
end

#process(data, opts) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'frontend/app/helpers/search_helper.rb', line 198

def process(data, opts)
  case opts[:type]
  when 'boolean'
    I18n.t("boolean.#{data}", :default => data.to_s)
  when 'date'
    Date.parse(data)
  else
    if opts[:locale_key] == '_singular'
      I18n.t("#{data}._singular", :default => data.to_s)
    else
      opts[:locale_key] = "enumerations.#{opts[:locale_key]}" unless opts[:locale_key].include?('.')
      I18n.t("#{opts[:locale_key]}.#{data}", :default => data.to_s)
    end
  end
end

#sr_only(text) ⇒ Object



297
298
299
# File 'frontend/app/helpers/search_helper.rb', line 297

def sr_only(text)
  ('<span class="sr-only">' + text + '</span>').html_safe
end