Class: Blacklight::SearchState
- Inherits:
-
Object
- Object
- Blacklight::SearchState
show all
- Defined in:
- lib/blacklight/search_state.rb,
lib/blacklight/search_state/filter_field.rb,
lib/blacklight/search_state/pivot_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, PivotFilterField
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
This method is never accessed in this class, but may be used by subclasses that need to access the url_helpers.
Instance Method Summary
collapse
Constructor Details
#initialize(params, blacklight_config, controller = nil) ⇒ SearchState
Returns a new instance of SearchState.
21
22
23
24
25
|
# File 'lib/blacklight/search_state.rb', line 21
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
|
Instance Attribute Details
#blacklight_config ⇒ Object
Must be called blacklight_config, because Blacklight::Facet calls blacklight_config.
10
11
12
|
# File 'lib/blacklight/search_state.rb', line 10
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
14
15
16
|
# File 'lib/blacklight/search_state.rb', line 14
def controller
@controller
end
|
#params ⇒ Object
This method is never accessed in this class, but may be used by subclasses that need
to access the url_helpers
14
15
16
|
# File 'lib/blacklight/search_state.rb', line 14
def params
@params
end
|
Instance Method Details
#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.
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/blacklight/search_state.rb', line 111
def add_facet_params_and_redirect(field, item)
new_params = filter(field).add(item).to_h
request_keys = blacklight_config.facet_paginator_class.request_keys
new_params.(*request_keys.values)
new_params
end
|
#clause_params ⇒ Object
44
45
46
|
# File 'lib/blacklight/search_state.rb', line 44
def clause_params
params[:clause] || {}
end
|
#facet_page ⇒ Object
165
166
167
|
# File 'lib/blacklight/search_state.rb', line 165
def facet_page
[params[facet_request_keys[:page]].to_i, 1].max
end
|
#facet_prefix ⇒ Object
173
174
175
|
# File 'lib/blacklight/search_state.rb', line 173
def facet_prefix
params[facet_request_keys[:prefix]]
end
|
#facet_sort ⇒ Object
169
170
171
|
# File 'lib/blacklight/search_state.rb', line 169
def facet_sort
params[facet_request_keys[:sort]]
end
|
#filter(field_key_or_field) ⇒ FilterField
96
97
98
99
100
101
102
|
# File 'lib/blacklight/search_state.rb', line 96
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_fields ⇒ Object
85
86
87
|
# File 'lib/blacklight/search_state.rb', line 85
def filter_fields
blacklight_config.facet_fields.each_value.map { |value| filter(value) }
end
|
Return the list of filters (i.e. facets) that have been applied in the current search
91
92
93
|
# File 'lib/blacklight/search_state.rb', line 91
def filters
@filters ||= filter_fields.select(&:any?)
end
|
#has_constraints? ⇒ Boolean
32
33
34
|
# File 'lib/blacklight/search_state.rb', line 32
def has_constraints?
!(query_param.blank? && filters.blank? && clause_params.blank?)
end
|
#page ⇒ Object
141
142
143
|
# File 'lib/blacklight/search_state.rb', line 141
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/blacklight/search_state.rb', line 126
def params_for_search(params_to_merge = {})
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_page ⇒ Object
145
146
147
148
149
|
# File 'lib/blacklight/search_state.rb', line 145
def per_page
params[:rows].presence&.to_i ||
params[:per_page].presence&.to_i ||
blacklight_config.default_per_page
end
|
#query_boolean_operator ⇒ Object
40
41
42
|
# File 'lib/blacklight/search_state.rb', line 40
def query_boolean_operator
params[:op]
end
|
#query_param ⇒ Object
36
37
38
|
# File 'lib/blacklight/search_state.rb', line 36
def query_param
params[:q]
end
|
#remove_query_params ⇒ Object
79
80
81
82
83
|
# File 'lib/blacklight/search_state.rb', line 79
def remove_query_params
p = reset_search_params
p.delete(:q)
p
end
|
49
50
51
|
# File 'lib/blacklight/search_state.rb', line 49
def reset(params = nil)
self.class.new(params || {}, blacklight_config, controller)
end
|
54
55
56
|
# File 'lib/blacklight/search_state.rb', line 54
def reset_search(additional_params = {})
reset(reset_search_params.merge(additional_params))
end
|
#routable?(doc) ⇒ Boolean
To build a show route, we must have a blacklight_config that has
configured show views, and the doc must appropriate to the config
73
74
75
76
77
|
# File 'lib/blacklight/search_state.rb', line 73
def routable?(doc)
return false unless respond_to?(:blacklight_config) && blacklight_config.view_config(:show).route
doc.is_a? routable_model_for(blacklight_config)
end
|
#search_field ⇒ Object
161
162
163
|
# File 'lib/blacklight/search_state.rb', line 161
def search_field
blacklight_config.search_fields[search_field_key]
end
|
#sort_field ⇒ Object
151
152
153
154
155
156
157
158
159
|
# File 'lib/blacklight/search_state.rb', line 151
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
27
28
29
|
# File 'lib/blacklight/search_state.rb', line 27
def to_hash
params.deep_dup
end
|
#url_for_document(doc, options = {}) ⇒ Object
Extension point for downstream applications
to provide more interesting routing to
documents
62
63
64
65
66
67
68
|
# File 'lib/blacklight/search_state.rb', line 62
def url_for_document(doc, options = {})
return doc unless routable?(doc)
route = blacklight_config.view_config(:show).route.merge(action: :show, id: doc).merge(options)
route[:controller] = params[:controller] if route[:controller] == :current
route
end
|