Class: Blacklight::SearchState

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Includes:
Deprecations::SearchStateNormalization
Defined in:
lib/blacklight/search_state.rb,
lib/blacklight/search_state/filter_field.rb

Overview

This class encapsulates the search state as represented by the query parameters namely: :f, :q, :page, :per_page and, :sort

Defined Under Namespace

Classes: FilterField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, blacklight_config, controller = nil) ⇒ SearchState

Returns a new instance of SearchState.

Parameters:

  • params (ActionController::Parameters)
  • blacklight_config (Blacklight::Config)
  • controller (ApplicationController) (defaults to: nil)

    used for the routing helpers



25
26
27
28
29
# File 'lib/blacklight/search_state.rb', line 25

def initialize(params, blacklight_config, controller = nil)
  @blacklight_config = blacklight_config
  @controller = controller
  @params = Blacklight::Parameters.new(params, self).permit_search_params.to_h.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/blacklight/search_state.rb', line 41

def method_missing(method_name, *arguments, &block)
  if @params.respond_to?(method_name)
    Deprecation.warn(self.class, "Calling `#{method_name}` on Blacklight::SearchState " \
      'is deprecated and will be removed in Blacklight 8. Call #to_h first if you ' \
      ' need to use hash methods (or, preferably, use your own SearchState implementation)')
    @params.public_send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#blacklight_configObject (readonly)

Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.



13
14
15
# File 'lib/blacklight/search_state.rb', line 13

def blacklight_config
  @blacklight_config
end

#controllerObject (readonly)

This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers



18
19
20
# File 'lib/blacklight/search_state.rb', line 18

def controller
  @controller
end

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/blacklight/search_state.rb', line 14

def params
  @params
end

Instance Method Details

#add_facet_params(field, item) ⇒ Object

adds the value and/or field to params Does NOT remove request keys and otherwise ensure that the hash is suitable for a redirect. See add_facet_params_and_redirect



131
132
133
# File 'lib/blacklight/search_state.rb', line 131

def add_facet_params(field, item)
  filter(field).add(item).params
end

#add_facet_params_and_redirect(field, item) ⇒ Object

Used in catalog/facet action, facets.rb view, for a click on a facet value. Add on the facet params to existing search constraints. Remove any paginator-specific request params, or other request params that should be removed for a 'fresh' display. Change the action to 'index' to send them back to catalog/index with their new facet choice.



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/blacklight/search_state.rb', line 143

def add_facet_params_and_redirect(field, item)
  new_params = Deprecation.silence(self.class) do
    add_facet_params(field, item).to_h.with_indifferent_access
  end

  # Delete any request params from facet-specific action, needed
  # to redir to index action properly.
  request_keys = blacklight_config.facet_paginator_class.request_keys
  new_params.extract!(*request_keys.values)

  new_params
end

#clause_paramsObject



70
71
72
# File 'lib/blacklight/search_state.rb', line 70

def clause_params
  params[:clause] || {}
end

#facet_pageObject



219
220
221
# File 'lib/blacklight/search_state.rb', line 219

def facet_page
  [params[facet_request_keys[:page]].to_i, 1].max
end

#facet_prefixObject



227
228
229
# File 'lib/blacklight/search_state.rb', line 227

def facet_prefix
  params[facet_request_keys[:prefix]]
end

#facet_sortObject



223
224
225
# File 'lib/blacklight/search_state.rb', line 223

def facet_sort
  params[facet_request_keys[:sort]]
end

#filter(field_key_or_field) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/blacklight/search_state.rb', line 119

def filter(field_key_or_field)
  field = field_key_or_field if field_key_or_field.is_a? Blacklight::Configuration::Field
  field ||= blacklight_config.facet_fields[field_key_or_field]
  field ||= Blacklight::Configuration::NullField.new(key: field_key_or_field)

  (field.filter_class || FilterField).new(field, self)
end

#filter_fieldsObject



111
112
113
# File 'lib/blacklight/search_state.rb', line 111

def filter_fields
  blacklight_config.facet_fields.each_value.map { |value| filter(value) }
end

#filter_paramsObject



74
75
76
# File 'lib/blacklight/search_state.rb', line 74

def filter_params
  params[:f] || {}
end

#filtersObject



115
116
117
# File 'lib/blacklight/search_state.rb', line 115

def filters
  @filters ||= filter_fields.select(&:any?)
end

#has_constraints?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/blacklight/search_state.rb', line 60

def has_constraints?
  Deprecation.silence(Blacklight::SearchState) do
    !(query_param.blank? && filter_params.blank? && filters.blank? && clause_params.blank?)
  end
end

#has_facet?(config, value: nil) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
171
172
173
# File 'lib/blacklight/search_state.rb', line 167

def has_facet?(config, value: nil)
  if value
    filter(config).include?(value)
  else
    filter(config).any?
  end
end

#pageObject



195
196
197
# File 'lib/blacklight/search_state.rb', line 195

def page
  [params[:page].to_i, 1].max
end

#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters

Merge the source params with the params_to_merge hash

Parameters:

  • params_to_merge (Hash) (defaults to: {})

    to merge into above

Yields:

  • (params)

    The merged parameters hash before being sanitized

Returns:

  • (ActionController::Parameters)

    the current search parameters after being sanitized by Blacklight::Parameters.sanitize



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/blacklight/search_state.rb', line 180

def params_for_search(params_to_merge = {})
  # params hash we'll return
  my_params = to_h.merge(self.class.new(params_to_merge, blacklight_config, controller))

  if block_given?
    yield my_params
  end

  if my_params[:page] && (my_params[:per_page] != params[:per_page] || my_params[:sort] != params[:sort])
    my_params[:page] = 1
  end

  Parameters.sanitize(my_params)
end

#per_pageObject



199
200
201
202
203
# File 'lib/blacklight/search_state.rb', line 199

def per_page
  params[:rows].presence&.to_i ||
    params[:per_page].presence&.to_i ||
    blacklight_config.default_per_page
end

#query_paramObject



66
67
68
# File 'lib/blacklight/search_state.rb', line 66

def query_param
  params[:q]
end

#remove_facet_params(field, item) ⇒ Object

copies the current params (or whatever is passed in as the 3rd arg) removes the field value from params removes the field if there are no more values in params[field] removes additional params (page, id, etc..)

Parameters:

  • field (String)
  • item (String)


162
163
164
# File 'lib/blacklight/search_state.rb', line 162

def remove_facet_params(field, item)
  filter(field).remove(item).params
end

#remove_query_paramsObject



105
106
107
108
109
# File 'lib/blacklight/search_state.rb', line 105

def remove_query_params
  p = reset_search_params
  p.delete(:q)
  p
end

#reset(params = nil) ⇒ Blacklight::SearchState



80
81
82
# File 'lib/blacklight/search_state.rb', line 80

def reset(params = nil)
  self.class.new(params || {}, blacklight_config, controller)
end

#reset_search(additional_params = {}) ⇒ Blacklight::SearchState



85
86
87
# File 'lib/blacklight/search_state.rb', line 85

def reset_search(additional_params = {})
  reset(reset_search_params.merge(additional_params))
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/blacklight/search_state.rb', line 52

def respond_to_missing?(method_name, include_private = false)
  @params.respond_to?(method_name, include_private) || super
end

#search_fieldObject



215
216
217
# File 'lib/blacklight/search_state.rb', line 215

def search_field
  blacklight_config.search_fields[search_field_key]
end

#sort_fieldObject



205
206
207
208
209
210
211
212
213
# File 'lib/blacklight/search_state.rb', line 205

def sort_field
  if sort_field_key.blank?
    # no sort param provided, use default
    blacklight_config.default_sort_field
  else
    # check for sort field key
    blacklight_config.sort_fields[sort_field_key]
  end
end

#to_hashObject Also known as: to_h



31
32
33
# File 'lib/blacklight/search_state.rb', line 31

def to_hash
  params.deep_dup
end

#to_unsafe_hObject



36
37
38
39
# File 'lib/blacklight/search_state.rb', line 36

def to_unsafe_h
  Deprecation.warn(self.class, 'Use SearchState#to_h instead of SearchState#to_unsafe_h')
  to_hash
end

#url_for_document(doc, options = {}) ⇒ Object

Extension point for downstream applications to provide more interesting routing to documents



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/blacklight/search_state.rb', line 93

def url_for_document(doc, options = {})
  if respond_to?(:blacklight_config) &&
      blacklight_config.view_config(:show).route &&
      (!doc.respond_to?(:to_model) || doc.to_model.is_a?(SolrDocument))
    route = blacklight_config.view_config(:show).route.merge(action: :show, id: doc).merge(options)
    route[:controller] = params[:controller] if route[:controller] == :current
    route
  else
    doc
  end
end