Skip to content

Commit a29bbad

Browse files
authored
Merge pull request #180 from MITLibraries/gdt-285-search-terms-in-summary
Display search terms in summary panel
2 parents cae06e0 + 1f1ba47 commit a29bbad

File tree

10 files changed

+421
-54
lines changed

10 files changed

+421
-54
lines changed

app/assets/stylesheets/partials/_panels.scss

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,34 +98,56 @@
9898
}
9999
}
100100

101-
.filter-summary {
101+
.search-summary {
102102
color: $black;
103103
background-color: $gray-l3;
104104
margin-top: 0;
105105
border: 0;
106-
padding: 2rem;
107-
padding-top: 1.5rem;
106+
padding: 2rem 2rem 1rem 2rem;
107+
.list-filter-summary,
108+
.list-terms-summary {
109+
@media (min-width: $bp-screen-md) {
110+
display: flex;
111+
}
112+
}
108113
.list-filter-summary {
109-
display: flex;
110-
.hd-filter-summary {
111-
margin-right: 2rem;
112-
padding-top: 0.3rem;
114+
padding-top: 1rem;
115+
border-top: 1px solid $black;
116+
}
117+
.hd-search-summary {
118+
margin-right: 2rem;
119+
padding-top: 0.2rem;
120+
white-space: nowrap;
121+
}
122+
.list-unbulleted {
123+
margin-bottom: 0;
124+
}
125+
.applied-filter,
126+
.applied-term {
127+
font-size: $fs-small;
128+
text-decoration: none;
129+
margin-right: 2rem;
130+
}
131+
.applied-term {
132+
margin-top: .5rem;
133+
&:first-child {
134+
margin-top: 0;
135+
}
136+
}
137+
.keyword {
138+
margin-bottom: 1.2rem;
139+
}
140+
.applied-filter {
141+
padding: .5rem;
142+
&::before {
143+
font-family: FontAwesome;
144+
content: '\f00d';
145+
padding-right: .25rem;
113146
}
114-
.applied-filter {
115-
font-size: $fs-small;
116-
text-decoration: none;
117-
padding: .5rem;
118-
&::before {
119-
font-family: FontAwesome;
120-
content: '\f00d';
121-
padding-right: .25rem;
122-
}
123-
&:hover,
124-
&:focus {
125-
color: $white;
126-
background-color: $black;
127-
}
128-
margin-right: 2rem;
147+
&:hover,
148+
&:focus {
149+
color: $white;
150+
background-color: $black;
129151
}
130152
}
131153
.clear-filters {

app/helpers/application_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def index_page_title
1515
def results_page_title(query, character_limit = 50)
1616
return index_page_title unless query.present?
1717

18-
ignored_terms = %i[page advanced geobox geodistance]
18+
ignored_terms = %i[page advanced geobox geodistance booleanType]
1919
terms = query.reject { |term| ignored_terms.include? term }.values.join(' ')
2020
terms = "#{terms.first(character_limit)}..." if terms.length > character_limit
2121
"#{terms} | #{page_title_base}"

app/helpers/search_helper.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,49 @@ def parse_geo_dates(dates)
4444
handle_unparsable_date(relevant_date)
4545
end
4646

47+
def applied_keyword(query)
48+
relevant_terms = ['q']
49+
render_relevant_terms(query, relevant_terms)
50+
end
51+
52+
def applied_geobox_terms(query)
53+
relevant_terms = %w[geoboxMinLatitude geoboxMaxLatitude geoboxMinLongitude geoboxMaxLongitude]
54+
render_relevant_terms(query, relevant_terms)
55+
end
56+
57+
def applied_geodistance_terms(query)
58+
relevant_terms = %w[geodistanceLatitude geodistanceLongitude geodistanceDistance]
59+
render_relevant_terms(query, relevant_terms)
60+
end
61+
62+
def applied_advanced_terms(query)
63+
relevant_terms = %w[title citation contributors fundingInformation identifiers locations subjects]
64+
render_relevant_terms(query, relevant_terms)
65+
end
66+
4767
private
4868

69+
# Query params need some treatment to look decent in the search summary panel.
70+
def readable_param(param)
71+
return 'Keyword anywhere' if param == 'q'
72+
return 'Authors' if param == 'contributors' && Flipflop.enabled?(:gdt)
73+
74+
if param.starts_with?('geodistance')
75+
param = param.gsub('geodistance', '')
76+
elsif param.starts_with?('geobox')
77+
param = param.gsub(/geobox(Max|Min)/, '\1 ')
78+
end
79+
80+
param.titleize.humanize
81+
end
82+
83+
def render_relevant_terms(query, relevant_terms)
84+
applied_terms = query.select { |param, _value| relevant_terms.include?(param.to_s) }
85+
return unless applied_terms.present?
86+
87+
applied_terms.filter_map { |param, value| "#{readable_param(param.to_s)}: #{value}" }
88+
end
89+
4990
def handle_unparsable_date(date)
5091
if date.include? '-'
5192
extract_year(date, '-')
Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,71 @@
1-
<% return unless applied_filters(@enhanced_query).any? %>
1+
<% return unless (applied_filters(@enhanced_query).present? ||
2+
applied_keyword(@enhanced_query).present? ||
3+
applied_geobox_terms(@enhanced_query).present? ||
4+
applied_geodistance_terms(@enhanced_query).present? ||
5+
applied_advanced_terms(@enhanced_query).present?) %>
26

3-
<aside class="filter-summary">
4-
<div class="list-filter-summary">
5-
<h2 class="hd-filter-summary hd-5">Applied filters: </h2>
6-
<ul class="list-inline">
7-
<% applied_filters(@enhanced_query).each do |filter| %>
8-
<li>
9-
<a class="applied-filter"
10-
href="<%= results_path(remove_filter(@enhanced_query, filter.keys[0], filter.values[0])) %>">
11-
<%= "#{nice_labels[filter.keys[0]] || filter.keys[0]}:" %>
12-
<% if Flipflop.enabled?(:gdt) %>
13-
<%= "#{gdt_sources(filter.values[0], filter.keys[0])}" %>
14-
<% else %>
15-
<%= "#{filter.values[0]}" %>
16-
<% end %>
17-
<span class="sr">Remove applied filter?</span>
18-
</a>
19-
</li>
20-
<% end %>
21-
</ul>
7+
<aside class="search-summary">
8+
<div class="list-terms-summary">
9+
<h2 class="hd-search-summary hd-5">Applied search terms: </h2>
10+
<ul class="list-unbulleted">
11+
<% if applied_keyword(@enhanced_query).present? %>
12+
<li class="applied-term keyword"><%= applied_keyword(@enhanced_query).first %></li>
13+
<% end %>
14+
<% if applied_geobox_terms(@enhanced_query).present? %>
15+
<li class="applied-term">
16+
<ul class="list-inline">
17+
<% applied_geobox_terms(@enhanced_query).each do |term| %>
18+
<li class="applied-term"><%= term %></li>
19+
<% end %>
20+
</ul>
21+
</li>
22+
<% end %>
23+
<% if applied_geodistance_terms(@enhanced_query).present? %>
24+
<li class="applied-term">
25+
<ul class="list-inline">
26+
<% applied_geodistance_terms(@enhanced_query).each do |term| %>
27+
<li class="applied-term"><%= term %></li>
28+
<% end %>
29+
</ul>
30+
</li>
31+
<% end %>
32+
<% if applied_advanced_terms(@enhanced_query).present? %>
33+
<li class="applied-term">
34+
<ul class="list-inline">
35+
<% applied_advanced_terms(@enhanced_query).each do |term| %>
36+
<li class="applied-term"><%= term %></li>
37+
<% end %>
38+
</ul>
39+
</li>
40+
<% end %>
41+
</ul>
2242
</div>
23-
<% if applied_filters(@enhanced_query).length > 1 %>
24-
<div class="clear-filters">
25-
<a class="btn button-primary"
26-
href="<%= results_path(remove_all_filters(@enhanced_query)) %>">Clear all filters</a>
43+
44+
<% if applied_filters(@enhanced_query).any? %>
45+
<div class="list-filter-summary">
46+
<h2 class="hd-search-summary hd-5">Applied filters: </h2>
47+
<ul class="list-inline">
48+
<% applied_filters(@enhanced_query).each do |filter| %>
49+
<li>
50+
<a class="applied-filter"
51+
href="<%= results_path(remove_filter(@enhanced_query, filter.keys[0], filter.values[0])) %>">
52+
<%= "#{nice_labels[filter.keys[0]] || filter.keys[0]}:" %>
53+
<% if Flipflop.enabled?(:gdt) %>
54+
<%= "#{gdt_sources(filter.values[0], filter.keys[0])}" %>
55+
<% else %>
56+
<%= "#{filter.values[0]}" %>
57+
<% end %>
58+
<span class="sr">Remove applied filter?</span>
59+
</a>
60+
</li>
61+
<% end %>
62+
</ul>
2763
</div>
64+
<% if applied_filters(@enhanced_query).length > 1 %>
65+
<div class="clear-filters">
66+
<a class="btn button-primary"
67+
href="<%= results_path(remove_all_filters(@enhanced_query)) %>">Clear all filters</a>
68+
</div>
69+
<% end %>
2870
<% end %>
2971
</aside>

test/controllers/search_controller_geo_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ def setup
8282
assert_select '#geodistance-search-panel', count: 0
8383
end
8484

85+
test 'GDT lists applied geospatial search terms' do
86+
VCR.use_cassette('geobox and geodistance',
87+
allow_playback_repeats: true,
88+
match_requests_on: %i[method uri body]) do
89+
query = {
90+
geobox: 'true',
91+
geodistance: 'true',
92+
geoboxMinLongitude: 40.5,
93+
geoboxMinLatitude: 60.0,
94+
geoboxMaxLongitude: 78.2,
95+
geoboxMaxLatitude: 80.0,
96+
geodistanceLatitude: 36.1,
97+
geodistanceLongitude: 62.6,
98+
geodistanceDistance: '50mi'
99+
}.to_query
100+
get "/results?#{query}"
101+
assert_response :success
102+
assert_nil flash[:error]
103+
104+
assert_select 'li', 'Min longitude: 40.5'
105+
assert_select 'li', 'Min latitude: 60.0'
106+
assert_select 'li', 'Max longitude: 78.2'
107+
assert_select 'li', 'Max latitude: 80.0'
108+
assert_select 'li', 'Latitude: 36.1'
109+
assert_select 'li', 'Longitude: 62.6'
110+
assert_select 'li', 'Distance: 50mi'
111+
end
112+
end
113+
85114
test 'can query geobox' do
86115
VCR.use_cassette('geobox',
87116
allow_playback_repeats: true,

test/controllers/search_controller_test.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@ def setup
288288

289289
# Advanced search behavior
290290
test 'advanced search by keyword' do
291-
skip("We no longer display a list of search terms in the UI; leaving this in in case we decide to reintroduce" \
292-
"that feature soon.")
293291
VCR.use_cassette('advanced keyword asdf',
294292
allow_playback_repeats: true,
295293
match_requests_on: %i[method uri body]) do
@@ -313,8 +311,6 @@ def setup
313311
end
314312

315313
test 'advanced search can accept values from all fields' do
316-
skip("We no longer display a list of search terms in the UI; leaving this in in case we decide to reintroduce" \
317-
"that feature soon.")
318314
VCR.use_cassette('advanced all',
319315
allow_playback_repeats: true,
320316
match_requests_on: %i[method uri body]) do
@@ -337,12 +333,11 @@ def setup
337333
assert_select 'li', 'Keyword anywhere: data'
338334
assert_select 'li', 'Citation: citation'
339335
assert_select 'li', 'Contributors: contribs'
340-
assert_select 'li', 'Funders: fund'
336+
assert_select 'li', 'Funding information: fund'
341337
assert_select 'li', 'Identifiers: ids'
342338
assert_select 'li', 'Locations: locs'
343339
assert_select 'li', 'Subjects: subs'
344340
assert_select 'li', 'Title: title'
345-
assert_select 'li', 'Source: sauce'
346341
end
347342
end
348343

test/helpers/application_helper_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class ApplicationHelperTest < ActionView::TestCase
1313
end
1414

1515
test 'results_page_title excludes irrelevant query params' do
16-
query = { q: 'National Parks Service', page: 1, geobox: 'true', geodistance: 'true', advanced: 'true' }
16+
query = { q: 'National Parks Service', page: 1, geobox: 'true', geodistance: 'true', advanced: 'true',
17+
booleanType: 'AND' }
1718
assert_equal 'National Parks Service | MIT Libraries', results_page_title(query)
1819
end
1920

test/helpers/search_helper_test.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
class SearchHelperTest < ActionView::TestCase
44
include SearchHelper
55

6+
def setup
7+
@test_strategy = Flipflop::FeatureSet.current.test!
8+
@test_strategy.switch!(:gdt, false)
9+
end
10+
611
test 'removes displayed fields from highlights' do
712
result = { 'highlight' => [{ 'matchedField' => 'title', 'matchedPhrases' => 'Very important data' },
813
{ 'matchedField' => 'title.exact_value', 'matchedPhrases' => 'Very important data' },
@@ -113,4 +118,57 @@ class SearchHelperTest < ActionView::TestCase
113118
assert_equal 'Dates', format_highlight_label(date_range)
114119
assert_equal 'Edition', format_highlight_label(edition)
115120
end
121+
122+
test 'applied_keyword translates q param' do
123+
query = {
124+
q: 'usability'
125+
}
126+
assert_equal ['Keyword anywhere: usability'], applied_keyword(query)
127+
end
128+
129+
test 'applied_geobox_terms includes and translates all geobox params' do
130+
query = {
131+
geobox: true,
132+
geoboxMinLatitude: '41.2',
133+
geoboxMaxLatitude: '42.9',
134+
geoboxMinLongitude: '-73.5',
135+
geoboxMaxLongitude: '-69.9'
136+
}
137+
assert_equal ['Min latitude: 41.2', 'Max latitude: 42.9', 'Min longitude: -73.5', 'Max longitude: -69.9'],
138+
applied_geobox_terms(query)
139+
end
140+
141+
test 'applied_geodistance_terms includes and translates all geodistance params' do
142+
query = {
143+
geodistance: true,
144+
geodistanceLatitude: '42.3',
145+
geodistanceLongitude: '-83.7',
146+
geodistanceDistance: '50mi'
147+
}
148+
assert_equal ['Latitude: 42.3', 'Longitude: -83.7', 'Distance: 50mi'], applied_geodistance_terms(query)
149+
end
150+
151+
test 'applied_advanced_terms includes all possible advanced search terms' do
152+
query = {
153+
title: 'sample book',
154+
citation: 'person, sample. sample book. someplace, 2024',
155+
contributors: 'person, sample',
156+
fundingInformation: 'imls',
157+
identifiers: '1234/5678',
158+
locations: 'someplace',
159+
subjects: 'unit testing'
160+
}
161+
assert_equal ['Title: sample book', 'Citation: person, sample. sample book. someplace, 2024',
162+
'Contributors: person, sample', 'Funding information: imls', 'Identifiers: 1234/5678',
163+
'Locations: someplace', 'Subjects: unit testing'], applied_advanced_terms(query)
164+
end
165+
166+
test 'applied_advanced_terms translates contributors in GDT' do
167+
@test_strategy.switch!(:gdt, true)
168+
169+
query = {
170+
contributors: 'person, sample',
171+
}
172+
assert_equal ['Authors: person, sample'], applied_advanced_terms(query)
173+
end
116174
end

0 commit comments

Comments
 (0)