Module: Blacklight::SearchFields

Extended by:
ActiveSupport::Memoizable
Included in:
Blacklight
Defined in:
vendor/plugins/blacklight/lib/blacklight/search_fields.rb

Overview

Module to deal with accessing (and setting some defaults) in an array of hashes that describe Blacklight search fields. Requires the base class this module is added to implements a #config method that returns a hash, where config[:search_fields] will be an array of hashes describing search fields.

Search Field Configuration Hash =

:key

“title”, required, unique key used in search URLs to specify search_field

:display_label

“Title”, # user-displayable label, optional, if not supplied :key.titlecase will be used

:qt

“search”, # Solr qt param, request handler, usually can be left blank; defaults to Blacklight.config[:default_solr_params][:qt] if not specified.

:solr_parameters

=> “something” # optional hash of additional parameters to pass to solr for searches on this field.

:solr_local_parameters

=> “$something” # optional hash of additional parameters that will be passed using Solr LocalParams syntax, that can use dollar sign to reference other solr variables.

:include_in_simple_select

false. Defaults to true, but you can set to false to have a search field defined for deep-links or BL extensions, but not actually included in the HTML select for simple search choice.

Optionally you can supply a :key, which is what Blacklight will use to identify this search field in HTTP query params. If no :key is supplied, one will be computed from the :display_label. If that will result in a collision of keys, you should supply one explicitly.

Instance Method Summary (collapse)

Instance Method Details

- (Object) default_search_field

Returns default search field, used for simpler display in history, etc. if not set in config, defaults to first field listed in #search_field_list



62
63
64
# File 'vendor/plugins/blacklight/lib/blacklight/search_fields.rb', line 62

def default_search_field
  config[:default_search_field] || search_field_list[0]
end

- (Object) label_for_search_field(key)

Shortcut for commonly needed operation, look up display label for the key specified. Returns “Keyword” if a label can’t be found.



70
71
72
73
74
75
76
77
# File 'vendor/plugins/blacklight/lib/blacklight/search_fields.rb', line 70

def label_for_search_field(key)
  field_def = search_field_def_for_key(key)
  if field_def && field_def[:display_label]
     field_def[:display_label]
  else
     "Keyword"
  end            
end

- (Object) search_field_def_for_key(key)

Looks up a search field config hash from search_field_list having a certain supplied :key.



55
56
57
58
# File 'vendor/plugins/blacklight/lib/blacklight/search_fields.rb', line 55

def search_field_def_for_key(key)
  return nil if key.blank?
  search_field_list.find {|c| c[:key] == key}
end

- (Object) search_field_list

Looks up search field config list from config[:search_fields], and ‘normalizes’ all field config hashes using normalize_config method. Memoized for efficiency of normalization.



33
34
35
36
37
38
39
40
41
# File 'vendor/plugins/blacklight/lib/blacklight/search_fields.rb', line 33

def search_field_list
  normalized = config[:search_fields].collect {|obj| normalize_config(obj)}

  if (duplicates = normalized.collect{|h| h[:key]}.uniq!)
    raise "Duplicate keys found in search_field config: #{duplicates.inspect}"
  end
  
  normalized
end

- (Object) search_field_options_for_select

Returns suitable argument to options_for_select method, to create an html select based on #search_field_list. Skips search_fields marked :include_in_simple_select => false



47
48
49
50
51
# File 'vendor/plugins/blacklight/lib/blacklight/search_fields.rb', line 47

def search_field_options_for_select
  search_field_list.collect do |field_def|
    [field_def[:display_label],  field_def[:key]] unless field_def[:include_in_simple_select] == false
  end.compact
end