Class: Blacklight::SearchState
- Inherits:
-
Object
- Object
- Blacklight::SearchState
show all
- Extended by:
- Deprecation
- 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
-
#blacklight_config ⇒ Object
readonly
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
-
#controller ⇒ Object
readonly
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary
collapse
Instance Method Summary
collapse
-
#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.
-
#add_facet_params_and_redirect(field, item) ⇒ Object
Used in catalog/facet action, facets.rb view, for a click on a facet value.
-
#clause_params ⇒ Object
-
#facet_page ⇒ Object
-
#facet_prefix ⇒ Object
-
#facet_sort ⇒ Object
-
#filter(field_key_or_field) ⇒ Object
-
#filter_params ⇒ Object
-
#filters ⇒ Object
-
#has_constraints? ⇒ Boolean
-
#has_facet?(config, value: nil) ⇒ Boolean
-
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
constructor
A new instance of SearchState.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
-
#page ⇒ Object
-
#params_for_search(params_to_merge = {}) {|params| ... } ⇒ ActionController::Parameters
Merge the source params with the params_to_merge hash.
-
#per_page ⇒ Object
-
#query_param ⇒ Object
-
#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..).
-
#remove_query_params ⇒ Object
-
#reset(params = nil) ⇒ Blacklight::SearchState
-
#reset_search(additional_params = {}) ⇒ Blacklight::SearchState
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#search_field ⇒ Object
-
#sort_field ⇒ Object
-
#to_hash ⇒ Object
(also: #to_h)
-
#to_unsafe_h ⇒ Object
-
#url_for_document(doc, options = {}) ⇒ Object
Extension point for downstream applications to provide more interesting routing to documents.
Constructor Details
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
Returns a new instance of SearchState.
23
24
25
26
27
|
# File 'lib/blacklight/search_state.rb', line 23
def initialize(params, blacklight_config, controller = nil)
@params = self.class.normalize_params(params)
@blacklight_config = blacklight_config
@controller = controller
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/blacklight/search_state.rb', line 61
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_config ⇒ Object
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
11
12
13
|
# File 'lib/blacklight/search_state.rb', line 11
def blacklight_config
@blacklight_config
end
|
#controller ⇒ Object
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers
16
17
18
|
# File 'lib/blacklight/search_state.rb', line 16
def controller
@controller
end
|
#params ⇒ Object
Returns the value of attribute params.
12
13
14
|
# File 'lib/blacklight/search_state.rb', line 12
def params
@params
end
|
Class Method Details
.facet_params_need_normalization(facet_params) ⇒ Object
251
252
253
|
# File 'lib/blacklight/search_state.rb', line 251
def self.facet_params_need_normalization(facet_params)
facet_params.is_a?(Hash) && facet_params.values.any? { |x| x.is_a?(Hash) }
end
|
.normalize_facet_params(facet_params) ⇒ Object
255
256
257
258
259
|
# File 'lib/blacklight/search_state.rb', line 255
def self.normalize_facet_params(facet_params)
facet_params.transform_values do |value|
value.is_a?(Hash) ? value.values : value
end
end
|
.normalize_params(untrusted_params = {}) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/blacklight/search_state.rb', line 29
def self.normalize_params(untrusted_params = {})
params = untrusted_params
if params.respond_to?(:to_unsafe_h)
params = params.to_unsafe_h
params = params.with_indifferent_access if params.instance_of? Hash
elsif params.is_a? Hash
params = params.dup.with_indifferent_access
else
params = params.dup.to_h.with_indifferent_access
end
params[:f] = normalize_facet_params(params[:f]) if facet_params_need_normalization(params[:f])
params[:f_inclusive] = normalize_facet_params(params[:f_inclusive]) if facet_params_need_normalization(params[:f_inclusive])
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
151
152
153
|
# File 'lib/blacklight/search_state.rb', line 151
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.
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/blacklight/search_state.rb', line 163
def add_facet_params_and_redirect(field, item)
new_params = Deprecation.silence(self.class) do
add_facet_params(field, item)
end
request_keys = blacklight_config.facet_paginator_class.request_keys
new_params.(*request_keys.values)
new_params
end
|
#clause_params ⇒ Object
90
91
92
|
# File 'lib/blacklight/search_state.rb', line 90
def clause_params
params[:clause] || {}
end
|
#facet_page ⇒ Object
239
240
241
|
# File 'lib/blacklight/search_state.rb', line 239
def facet_page
[params[facet_request_keys[:page]].to_i, 1].max
end
|
#facet_prefix ⇒ Object
247
248
249
|
# File 'lib/blacklight/search_state.rb', line 247
def facet_prefix
params[facet_request_keys[:prefix]]
end
|
#facet_sort ⇒ Object
243
244
245
|
# File 'lib/blacklight/search_state.rb', line 243
def facet_sort
params[facet_request_keys[:sort]]
end
|
#filter(field_key_or_field) ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/blacklight/search_state.rb', line 139
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_params ⇒ Object
94
95
96
|
# File 'lib/blacklight/search_state.rb', line 94
def filter_params
params[:f] || {}
end
|
#filters ⇒ Object
131
132
133
134
135
136
137
|
# File 'lib/blacklight/search_state.rb', line 131
def filters
@filters ||= blacklight_config.facet_fields.each_value.map do |value|
f = filter(value)
f if f.any?
end.compact
end
|
#has_constraints? ⇒ Boolean
80
81
82
83
84
|
# File 'lib/blacklight/search_state.rb', line 80
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
187
188
189
190
191
192
193
|
# File 'lib/blacklight/search_state.rb', line 187
def has_facet?(config, value: nil)
if value
filter(config).include?(value)
else
filter(config).any?
end
end
|
#page ⇒ Object
215
216
217
|
# File 'lib/blacklight/search_state.rb', line 215
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/blacklight/search_state.rb', line 200
def params_for_search(params_to_merge = {})
my_params = params.dup.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_page ⇒ Object
219
220
221
222
223
|
# File 'lib/blacklight/search_state.rb', line 219
def per_page
params[:rows].presence&.to_i ||
params[:per_page].presence&.to_i ||
blacklight_config.default_per_page
end
|
#query_param ⇒ Object
86
87
88
|
# File 'lib/blacklight/search_state.rb', line 86
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..)
182
183
184
|
# File 'lib/blacklight/search_state.rb', line 182
def remove_facet_params(field, item)
filter(field).remove(item).params
end
|
#remove_query_params ⇒ Object
125
126
127
128
129
|
# File 'lib/blacklight/search_state.rb', line 125
def remove_query_params
p = reset_search_params
p.delete(:q)
p
end
|
100
101
102
|
# File 'lib/blacklight/search_state.rb', line 100
def reset(params = nil)
self.class.new(params || ActionController::Parameters.new, blacklight_config, controller)
end
|
105
106
107
|
# File 'lib/blacklight/search_state.rb', line 105
def reset_search(additional_params = {})
reset(reset_search_params.merge(additional_params))
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
72
73
74
|
# File 'lib/blacklight/search_state.rb', line 72
def respond_to_missing?(method_name, include_private = false)
@params.respond_to?(method_name, include_private) || super
end
|
#search_field ⇒ Object
235
236
237
|
# File 'lib/blacklight/search_state.rb', line 235
def search_field
blacklight_config.search_fields[search_field_key]
end
|
#sort_field ⇒ Object
225
226
227
228
229
230
231
232
233
|
# File 'lib/blacklight/search_state.rb', line 225
def sort_field
if sort_field_key.blank?
blacklight_config.default_sort_field
else
blacklight_config.sort_fields[sort_field_key]
end
end
|
#to_hash ⇒ Object
Also known as:
to_h
51
52
53
|
# File 'lib/blacklight/search_state.rb', line 51
def to_hash
@params.deep_dup
end
|
#to_unsafe_h ⇒ Object
56
57
58
59
|
# File 'lib/blacklight/search_state.rb', line 56
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
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/blacklight/search_state.rb', line 113
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
|