Skip to content

Commit 02db6ad

Browse files
committed
Add search summary panel and full filter support
Why these changes are being introduced: It would be helpful if users could see what filters they've applied to a search directly below the search bar, which would also provide another path to remove filters. Relevant ticket(s): https://mitlibraries.atlassian.net/browse/GDT-142 https://mitlibraries.atlassian.net/browse/GDT-196 How this addresses that need: This provides the requested panel. We had been calling this a "filter removal" panel, but I ended up naming the partial `search_summary` to avoid any confusion with the filter sidebar. However, the currently displayed data has the `filter-removal` class, because it's possible we will want to show advanced search terms here, too. While working on this, it occurred to me that the existing TIMDEX UI filter implementation is incomplete. It pulls in the aggregation names and uses those as the internal filter names. This works fine for the existing `contentType` and `source` filters, but it will cause a name collision for filters that also exist as regular search inputs (e.g. `contributors` vs. `contributorsFilter`). Since we will be adding more aggregations and filters soon, it makes sense to fix this problem now. Because this only lists filters at present, we've changed the label from 'You searched for' to 'Applied filters'. This is subject to change. Side effects of these changes: * The "You searched for" list has been removed, as this feature replaces it. As mentioned above, we will likely want to provide that information in some manner in the search summary, but that will require some discussion with Darcy, who is out-of-office at the moment. * Filter keys are now cast as symbols throughout the application. Previously, they would be strings in some places and symbols in others. I found that confusing, but if this change is even more confusing, I'm happy to revert it. * A few tests have been skipped due to the removal of the "You searched for list". I chose not to delete them in case we reintroduce that feature in the near future. * Most of the cassettes have been regenerated, due to changes to the `TimdexSearch` model. Address code review feedback
1 parent fe1fa4b commit 02db6ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+641
-466
lines changed

app/assets/stylesheets/partials/_panels.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,36 @@
9999
}
100100
}
101101
}
102+
103+
.filter-summary {
104+
color: $black;
105+
background-color: $gray-l3;
106+
margin-top: 0;
107+
border: 0;
108+
padding: 2rem;
109+
padding-top: 1.5rem;
110+
display: flex;
111+
.hd-filter-summary {
112+
margin-right: 2rem;
113+
padding-top: 0.3rem;
114+
}
115+
a {
116+
font-size: $fs-small;
117+
text-decoration: none;
118+
padding: .5rem;
119+
&::before {
120+
font-family: FontAwesome;
121+
content: '\f00d';
122+
padding-right: .25rem;
123+
}
124+
&:hover,
125+
&:focus {
126+
color: $white;
127+
background-color: $black;
128+
}
129+
margin-right: 2rem;
130+
}
131+
@media (max-width: $bp-screen-md) {
132+
display: block;
133+
}
134+
}

app/assets/stylesheets/partials/_search.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* basic search bar */
66
.basic-search {
77
background-color: #989898;
8-
margin-bottom: 1rem;
8+
margin-bottom: 0rem;
99
padding: 1.6rem 2rem;
1010

1111
details {

app/controllers/search_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def extract_filters(response)
3333
aggs = response&.data&.search&.to_h&.dig('aggregations')
3434
return if aggs.blank?
3535

36-
aggs.select { |_, agg_values| agg_values.present? }
36+
# We use aggregations to determine which terms can be filtered. However, agg names do not include 'filter', whereas
37+
# our filter fields do (e.g., 'source' vs 'sourceFilter'). Because of this mismatch, we need to modify the
38+
# aggregation key names before collecting them as filters, so that when a filter is applied, it searches the
39+
# correct field name.
40+
aggs.select { |_, agg_values| agg_values.present? }.transform_keys { |key| (key.dup << 'Filter').to_sym }
3741
end
3842

3943
def extract_results(response)

app/helpers/filter_helper.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def add_filter(query, filter, term)
99
# seems like the best solution as each record only has a single source
1010
# in the data so there will never be a case to apply multiple in an AND
1111
# which is all we support in filter application.
12-
if new_query[filter].present? && filter != 'source'
12+
if new_query[filter].present? && filter != :sourceFilter
1313
new_query[filter] << term
1414
new_query[filter].uniq!
1515
else
@@ -21,8 +21,8 @@ def add_filter(query, filter, term)
2121

2222
def nice_labels
2323
{
24-
'contentType' => 'Content types',
25-
'source' => 'Sources'
24+
contentTypeFilter: 'Content type',
25+
sourceFilter: 'Source'
2626
}
2727
end
2828

@@ -44,4 +44,14 @@ def filter_applied?(terms, term)
4444

4545
terms.include?(term)
4646
end
47+
48+
def applied_filters
49+
filters = []
50+
@enhanced_query.map do |param, values|
51+
next unless param.to_s.include? 'Filter'
52+
53+
values.each { |value| filters << { param => value } }
54+
end
55+
filters
56+
end
4757
end

app/helpers/form_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ module FormHelper
22
def source_checkbox(source, params)
33
"<div class='field-subitem'>
44
<label class='field-checkbox'>
5-
<input type='checkbox' value='#{source.downcase}' name='source[]' class='source'#{if params[:source]&.include?(source.downcase)
6-
' checked'
7-
end}>
5+
<input type='checkbox' value='#{source.downcase}' name='sourceFilter[]'
6+
class='source'#{' checked' if params[:sourceFilter]&.include?(source.downcase) }>
87
#{source}
98
</label>
109
</div>".html_safe

app/models/enhancer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Enhancer
22
attr_accessor :enhanced_query
33

44
QUERY_PARAMS = %i[q citation contentType contributors fundingInformation identifiers locations subjects title].freeze
5-
FILTER_PARAMS = [:source].freeze
5+
FILTER_PARAMS = %i[sourceFilter contentTypeFilter].freeze
66
# accepts all params as each enhancer may require different data
77
def initialize(params)
88
@enhanced_query = {}

app/models/query_builder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class QueryBuilder
33

44
RESULTS_PER_PAGE = 20
55
QUERY_PARAMS = %w[q citation contributors fundingInformation identifiers locations subjects title].freeze
6-
FILTER_PARAMS = %i[source contentType].freeze
6+
FILTER_PARAMS = %i[sourceFilter contentTypeFilter].freeze
77

88
def initialize(enhanced_query)
99
@query = {}
@@ -29,8 +29,8 @@ def extract_query(enhanced_query)
2929
end
3030

3131
def extract_filters(enhanced_query)
32-
# NOTE: ui and backend naming are not aligned so we can't loop here. we should fix in UI
33-
@query['sourceFilter'] = enhanced_query[:source]
34-
@query['contentType'] = enhanced_query[:contentType]
32+
FILTER_PARAMS.each do |qp|
33+
@query[qp] = enhanced_query[qp]
34+
end
3535
end
3636
end

app/models/timdex_search.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TimdexSearch < TimdexBase
1515
$sourceFilter: [String!]
1616
$index: String
1717
$from: String
18-
$contentType: [String!]
18+
$contentTypeFilter: [String!]
1919
) {
2020
search(
2121
searchterm: $q
@@ -29,7 +29,7 @@ class TimdexSearch < TimdexBase
2929
sourceFilter: $sourceFilter
3030
index: $index
3131
from: $from
32-
contentTypeFilter: $contentType
32+
contentTypeFilter: $contentTypeFilter
3333
) {
3434
hits
3535
records {

app/views/search/_filter.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% return if values.empty? %>
1+
<% return if values.blank? %>
22

33
<div class="category">
44
<button class="filter-label <%= 'expanded' if @enhanced_query[category.to_sym].present? || first == true %>"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<% return unless applied_filters.any? %>
2+
3+
<div class="filter-summary">
4+
<h2 class="hd-filter-summary hd-5">Applied filters: </h2>
5+
<ul class="list-filter-summary list-inline">
6+
<% applied_filters.each do |filter| %>
7+
<li>
8+
<a href="<%= results_path(remove_filter(@enhanced_query, filter.keys[0], filter.values[0])) %>">
9+
<%= "#{nice_labels[filter.keys[0]] || filter.keys[0]}: #{filter.values[0]}" %>
10+
<span class="sr">Remove applied filter?</span>
11+
</a>
12+
</li>
13+
<% end %>
14+
</ul>
15+
</div>

0 commit comments

Comments
 (0)