Skip to content

Commit

Permalink
Display search terms in summary panel
Browse files Browse the repository at this point in the history
Why these changes are being introduced:

We had been leaning on the search form to remind users of the
current search params, but this is not always reliable (e.g., if
a input is cleared without executing a new search).

Relevant ticket(s):

* [GDT-285](https://mitlibraries.atlassian.net/browse/GDT-285)

How this addresses that need:

This displays applied search terms in the summary panel where
we show applied filters.

Side effects of this change:

* Two skipped tests, which confirmed an earlier iteration of this
feature, have been reintroduced.
* The new param `booleanType` has been ignored in the page title
constructor as well as the summary panel.
  • Loading branch information
jazairi committed Apr 30, 2024
1 parent eac446a commit 208212a
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 54 deletions.
66 changes: 44 additions & 22 deletions app/assets/stylesheets/partials/_panels.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,56 @@
}
}

.filter-summary {
.search-summary {
color: $black;
background-color: $gray-l3;
margin-top: 0;
border: 0;
padding: 2rem;
padding-top: 1.5rem;
padding: 2rem 2rem 1rem 2rem;
.list-filter-summary,
.list-terms-summary {
@media (min-width: $bp-screen-md) {
display: flex;
}
}
.list-filter-summary {
display: flex;
.hd-filter-summary {
margin-right: 2rem;
padding-top: 0.3rem;
padding-top: 1rem;
border-top: 1px solid $black;
}
.hd-search-summary {
margin-right: 2rem;
padding-top: 0.2rem;
white-space: nowrap;
}
.list-unbulleted {
margin-bottom: 0;
}
.applied-filter,
.applied-term {
font-size: $fs-small;
text-decoration: none;
margin-right: 2rem;
}
.applied-term {
margin-top: .5rem;
&:first-child {
margin-top: 0;
}
}
.keyword {
margin-bottom: 1.2rem;
}
.applied-filter {
padding: .5rem;
&::before {
font-family: FontAwesome;
content: '\f00d';
padding-right: .25rem;
}
.applied-filter {
font-size: $fs-small;
text-decoration: none;
padding: .5rem;
&::before {
font-family: FontAwesome;
content: '\f00d';
padding-right: .25rem;
}
&:hover,
&:focus {
color: $white;
background-color: $black;
}
margin-right: 2rem;
&:hover,
&:focus {
color: $white;
background-color: $black;
}
}
.clear-filters {
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def index_page_title
def results_page_title(query, character_limit = 50)
return index_page_title unless query.present?

ignored_terms = %i[page advanced geobox geodistance]
ignored_terms = %i[page advanced geobox geodistance booleanType]
terms = query.reject { |term| ignored_terms.include? term }.values.join(' ')
terms = "#{terms.first(character_limit)}..." if terms.length > character_limit
"#{terms} | #{page_title_base}"
Expand Down
40 changes: 40 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,48 @@ def parse_geo_dates(dates)
handle_unparsable_date(relevant_date)
end

def applied_keyword(query)
relevant_terms = ['q']
render_relevant_terms(query, relevant_terms)
end

def applied_geobox_terms(query)
relevant_terms = %w[geoboxMinLatitude geoboxMaxLatitude geoboxMinLongitude geoboxMaxLongitude]
render_relevant_terms(query, relevant_terms)
end

def applied_geodistance_terms(query)
relevant_terms = %w[geodistanceLatitude geodistanceLongitude geodistanceDistance]
render_relevant_terms(query, relevant_terms)
end

def applied_advanced_terms(query)
relevant_terms = %w[title citation contributors fundingInformation identifiers locations subjects]
render_relevant_terms(query, relevant_terms)
end

private

# Query params need some treatment to look decent in the search summary panel.
def readable_param(param)
return 'Keyword anywhere' if param == 'q'

if param.starts_with?('geodistance')
param = param.gsub('geodistance', '')
elsif param.starts_with?('geobox')
param = param.gsub(/geobox(Max|Min)/, '\1 ')
end

param.titleize.humanize
end

def render_relevant_terms(query, relevant_terms)
applied_terms = query.select { |param, _value| relevant_terms.include?(param.to_s) }
return unless applied_terms.present?

applied_terms.filter_map { |param, value| "#{readable_param(param.to_s)}: #{value}" }
end

def handle_unparsable_date(date)
if date.include? '-'
extract_year(date, '-')
Expand Down
90 changes: 66 additions & 24 deletions app/views/search/_search_summary.html.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
<% return unless applied_filters(@enhanced_query).any? %>
<% return unless (applied_filters(@enhanced_query).present? ||
applied_keyword(@enhanced_query).present? ||
applied_geobox_terms(@enhanced_query).present? ||
applied_geodistance_terms(@enhanced_query).present? ||
applied_advanced_terms(@enhanced_query).present?) %>

<aside class="filter-summary">
<div class="list-filter-summary">
<h2 class="hd-filter-summary hd-5">Applied filters: </h2>
<ul class="list-inline">
<% applied_filters(@enhanced_query).each do |filter| %>
<li>
<a class="applied-filter"
href="<%= results_path(remove_filter(@enhanced_query, filter.keys[0], filter.values[0])) %>">
<%= "#{nice_labels[filter.keys[0]] || filter.keys[0]}:" %>
<% if Flipflop.enabled?(:gdt) %>
<%= "#{gdt_sources(filter.values[0], filter.keys[0])}" %>
<% else %>
<%= "#{filter.values[0]}" %>
<% end %>
<span class="sr">Remove applied filter?</span>
</a>
</li>
<% end %>
</ul>
<aside class="search-summary">
<div class="list-terms-summary">
<h2 class="hd-search-summary hd-5">Applied search terms: </h2>
<ul class="list-unbulleted">
<% if applied_keyword(@enhanced_query).present? %>
<li class="applied-term keyword"><%= applied_keyword(@enhanced_query).first %></li>
<% end %>
<% if applied_geobox_terms(@enhanced_query).present? %>
<li class="applied-term">
<ul class="list-inline">
<% applied_geobox_terms(@enhanced_query).each do |term| %>
<li class="applied-term"><%= term %></li>
<% end %>
</ul>
</li>
<% end %>
<% if applied_geodistance_terms(@enhanced_query).present? %>
<li class="applied-term">
<ul class="list-inline">
<% applied_geodistance_terms(@enhanced_query).each do |term| %>
<li class="applied-term"><%= term %></li>
<% end %>
</ul>
</li>
<% end %>
<% if applied_advanced_terms(@enhanced_query).present? %>
<li class="applied-term">
<ul class="list-inline">
<% applied_advanced_terms(@enhanced_query).each do |term| %>
<li class="applied-term"><%= term %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</div>
<% if applied_filters(@enhanced_query).length > 1 %>
<div class="clear-filters">
<a class="btn button-primary"
href="<%= results_path(remove_all_filters(@enhanced_query)) %>">Clear all filters</a>

<% if applied_filters(@enhanced_query).any? %>
<div class="list-filter-summary">
<h2 class="hd-search-summary hd-5">Applied filters: </h2>
<ul class="list-inline">
<% applied_filters(@enhanced_query).each do |filter| %>
<li>
<a class="applied-filter"
href="<%= results_path(remove_filter(@enhanced_query, filter.keys[0], filter.values[0])) %>">
<%= "#{nice_labels[filter.keys[0]] || filter.keys[0]}:" %>
<% if Flipflop.enabled?(:gdt) %>
<%= "#{gdt_sources(filter.values[0], filter.keys[0])}" %>
<% else %>
<%= "#{filter.values[0]}" %>
<% end %>
<span class="sr">Remove applied filter?</span>
</a>
</li>
<% end %>
</ul>
</div>
<% if applied_filters(@enhanced_query).length > 1 %>
<div class="clear-filters">
<a class="btn button-primary"
href="<%= results_path(remove_all_filters(@enhanced_query)) %>">Clear all filters</a>
</div>
<% end %>
<% end %>
</aside>
29 changes: 29 additions & 0 deletions test/controllers/search_controller_geo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ def setup
assert_select '#geodistance-search-panel', count: 0
end

test 'GDT lists applied geospatial search terms' do
VCR.use_cassette('geobox and geodistance',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
geobox: 'true',
geodistance: 'true',
geoboxMinLongitude: 40.5,
geoboxMinLatitude: 60.0,
geoboxMaxLongitude: 78.2,
geoboxMaxLatitude: 80.0,
geodistanceLatitude: 36.1,
geodistanceLongitude: 62.6,
geodistanceDistance: '50mi'
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]

assert_select 'li', 'Min longitude: 40.5'
assert_select 'li', 'Min latitude: 60.0'
assert_select 'li', 'Max longitude: 78.2'
assert_select 'li', 'Max latitude: 80.0'
assert_select 'li', 'Latitude: 36.1'
assert_select 'li', 'Longitude: 62.6'
assert_select 'li', 'Distance: 50mi'
end
end

test 'can query geobox' do
VCR.use_cassette('geobox',
allow_playback_repeats: true,
Expand Down
7 changes: 1 addition & 6 deletions test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ def setup

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

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

Expand Down
3 changes: 2 additions & 1 deletion test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ApplicationHelperTest < ActionView::TestCase
end

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

Expand Down
44 changes: 44 additions & 0 deletions test/helpers/search_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,48 @@ class SearchHelperTest < ActionView::TestCase
assert_equal 'Dates', format_highlight_label(date_range)
assert_equal 'Edition', format_highlight_label(edition)
end

test 'applied_keyword translates q param' do
query = {
q: 'usability'
}
assert_equal ['Keyword anywhere: usability'], applied_keyword(query)
end

test 'applied_geobox_terms includes and translates all geobox params' do
query = {
geobox: true,
geoboxMinLatitude: '41.2',
geoboxMaxLatitude: '42.9',
geoboxMinLongitude: '-73.5',
geoboxMaxLongitude: '-69.9'
}
assert_equal ['Min latitude: 41.2', 'Max latitude: 42.9', 'Min longitude: -73.5', 'Max longitude: -69.9'],
applied_geobox_terms(query)
end

test 'applied_geodistance_terms includes and translates all geodistance params' do
query = {
geodistance: true,
geodistanceLatitude: '42.3',
geodistanceLongitude: '-83.7',
geodistanceDistance: '50mi'
}
assert_equal ['Latitude: 42.3', 'Longitude: -83.7', 'Distance: 50mi'], applied_geodistance_terms(query)
end

test 'applied_advanced_terms includes all possible advanced search terms' do
query = {
title: 'sample book',
citation: 'person, sample. sample book. someplace, 2024',
contributors: 'person, sample',
fundingInformation: 'imls',
identifiers: '1234/5678',
locations: 'someplace',
subjects: 'unit testing'
}
assert_equal ['Title: sample book', 'Citation: person, sample. sample book. someplace, 2024',
'Contributors: person, sample', 'Funding information: imls', 'Identifiers: 1234/5678',
'Locations: someplace', 'Subjects: unit testing'], applied_advanced_terms(query)
end
end
Loading

0 comments on commit 208212a

Please sign in to comment.