Skip to content

Commit 8438bc9

Browse files
Makes facets usable
** Why are these changes being introduced: * We need to make the facets in the left sidebar into something usable. * Because of inconsistencies in the imported records, certain facets need to be removed - leaving only those that we feel comfortable providing. ** Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/rdi-197 ** How does this address that need: A number of things are happening here: * A $contentType query variable has been added to the TimdexSearch model, which maps to the contentTypeFacet variable in the API. This requires changes to the enhancer and query_builder models to support this new value. This variable has been added to the search feedback in the results.html.erb view template. * We are removing a number of facets that are no longer desired in the UI. Removing them from the aggregations of the TimdexSearch model requires regenerating a large number of cassettes - I've kept those changes to a second commit in this PR for hopefully easier review. * The facet_labels method on the search controller is moving to a new facet helper, named nice_labels. This is not so much a method as a hash which can be used to look up human-readable names for facet categories. Note the invocation in the _facet partial on line 4 to see what I mean here. * Because the hash is moving to a helper, the extract_facets method on the controller gets simpler. Thanks to Jeremy for working through the best way to achieve this. * The facet helper also includes two methods for manipulating the query passed to results_path in the view partial. add_facet and remove_facet should be named pretty clearly, and take the expected parameters. * There is a new set of unit tests for all of the facet helpers. * Facets can be removed by clicking on a link below that facet. Active facets are called out in black, which hopefully will help identify them to the user. This should still be evaluated by UX when they join the project, obviously. ** Document any side effects to this change: * This changes the search feedback in the results.html.erb to work from the @enhanced_query instance variable rather than the raw params object. This should better reflect what the search actually was, giving space for the user to be informed of any manipulations by the application, rather than simply working from whatever the user had requested.
1 parent f24783c commit 8438bc9

File tree

9 files changed

+133
-61
lines changed

9 files changed

+133
-61
lines changed

app/assets/stylesheets/partials/_facets.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
margin-bottom: 0;
44
}
55

6+
.category {
7+
margin-bottom: 1em;
8+
}
9+
610
ul.category-terms {
711
border-collapse: separate;
812
border-spacing: 0px 4px;
913
display: table;
14+
margin-bottom: 0;
1015
table-layout: fixed;
1116
width: 100%;
1217

@@ -18,7 +23,8 @@
1823
text-decoration: none;
1924

2025
&:hover,
21-
&:focus {
26+
&:focus,
27+
&.applied {
2228
background: #000;
2329
color: #fff;
2430
}

app/controllers/search_controller.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,13 @@ def extract_errors(response)
3030
end
3131

3232
def extract_facets(response)
33-
facets = response&.data&.search&.to_h&.dig('aggregations')
34-
35-
facets&.map { |k, v| { facet_labels[k] => v } }
33+
response&.data&.search&.to_h&.dig('aggregations')
3634
end
3735

3836
def extract_results(response)
3937
response&.data&.search&.to_h&.dig('records')
4038
end
4139

42-
def facet_labels
43-
{
44-
'contentFormat' => 'Formats',
45-
'contentType' => 'Content types',
46-
'contributors' => 'Contributors',
47-
'languages' => 'Languages',
48-
'literaryForm' => 'Literary forms',
49-
'source' => 'Sources',
50-
'subjects' => 'Subjects'
51-
}
52-
end
53-
5440
def validate_q!
5541
return if params[:advanced]&.strip.present?
5642
return if params[:q]&.strip.present?

app/helpers/facet_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module FacetHelper
2+
def add_facet(query, facet, term)
3+
new_query = query.clone
4+
new_query[:page] = 1
5+
new_query[facet.to_sym] = term
6+
new_query
7+
end
8+
9+
def nice_labels
10+
{
11+
'contentType' => 'Content types',
12+
'source' => 'Sources'
13+
}
14+
end
15+
16+
def remove_facet(query, facet)
17+
new_query = query.clone
18+
new_query[:page] = 1
19+
new_query.delete facet.to_sym
20+
new_query
21+
end
22+
end

app/models/enhancer.rb

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

4-
QUERY_PARAMS = %i[q citation contributors fundingInformation identifiers locations subjects title].freeze
4+
QUERY_PARAMS = %i[q citation contentType contributors fundingInformation identifiers locations subjects title].freeze
55
FILTER_PARAMS = [:source].freeze
66
# accepts all params as each enhancer may require different data
77
def initialize(params)

app/models/query_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class QueryBuilder
22
attr_reader :query
33

44
RESULTS_PER_PAGE = 20
5-
QUERY_PARAMS = %w[q citation contributors fundingInformation identifiers locations subjects title].freeze
5+
QUERY_PARAMS = %w[q citation contentType contributors fundingInformation identifiers locations subjects title].freeze
66
FILTER_PARAMS = [:source].freeze
77

88
def initialize(enhanced_query)

app/models/timdex_search.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TimdexSearch < TimdexBase
1414
$title: String
1515
$sourceFacet: [String!]
1616
$from: String
17+
$contentType: String
1718
) {
1819
search(
1920
searchterm: $q
@@ -26,6 +27,7 @@ class TimdexSearch < TimdexBase
2627
title: $title
2728
sourceFacet: $sourceFacet
2829
from: $from
30+
contentTypeFacet: $contentType
2931
) {
3032
hits
3133
records {
@@ -51,34 +53,14 @@ class TimdexSearch < TimdexBase
5153
}
5254
}
5355
aggregations {
54-
contentFormat {
55-
key
56-
docCount
57-
}
5856
contentType {
5957
key
6058
docCount
6159
}
62-
contributors {
63-
key
64-
docCount
65-
}
66-
languages {
67-
key
68-
docCount
69-
}
70-
literaryForm {
71-
key
72-
docCount
73-
}
7460
source {
7561
key
7662
docCount
7763
}
78-
subjects {
79-
key
80-
docCount
81-
}
8264
}
8365
}
8466
}

app/views/search/_facet.html.erb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
<% return if facet.values[0].empty? %>
1+
<% return if values.empty? %>
22

33
<div class="category">
4-
<h3><%= facet.keys[0] %></h3>
4+
<h3><%= nice_labels[category] || category %></h3>
55
<ul class="category-terms list-unbulleted">
6-
<% facet.values[0].each do |term| %>
6+
<% values.each do |term| %>
77
<li class="term">
8-
<a href="#">
8+
<a href="<%= results_path(add_facet(@enhanced_query, category, term['key'])) %>" class="<%= "applied" if @enhanced_query[category.to_sym] == term['key'] %>">
99
<span class="name"><%= term['key'] %></span>
1010
<span class="count"><%= term['docCount'] %> <span class="sr">records</span></span>
1111
</a>
1212
</li>
1313
<% end %>
1414
</ul>
15+
<% if @enhanced_query[category.to_sym].present? %>
16+
<div><%= link_to "Show all #{nice_labels[category]&.downcase || category}", results_path(remove_facet(@enhanced_query, category)) %>
17+
</div>
18+
<% end %>
1519
</div>

app/views/search/results.html.erb

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@
44

55
<p>Showing results for:
66
<ul>
7-
<% if params[:q].present? %>
8-
<li>Keyword anywhere: <%= params[:q] %></li>
7+
<% if @enhanced_query[:q].present? %>
8+
<li>Keyword anywhere: <%= @enhanced_query[:q] %></li>
99
<% end %>
10-
<% if params[:citation].present? %>
11-
<li>Citation: <%= params[:citation] %></li>
10+
<% if @enhanced_query[:citation].present? %>
11+
<li>Citation: <%= @enhanced_query[:citation] %></li>
1212
<% end %>
13-
<% if params[:contributors].present? %>
14-
<li>Contributors: <%= params[:contributors] %></li>
13+
<% if @enhanced_query[:contentType].present? %>
14+
<li>Content type: <%= @enhanced_query[:contentType] %></li>
1515
<% end %>
16-
<% if params[:fundingInformation].present? %>
17-
<li>Funders: <%= params[:fundingInformation] %></li>
16+
<% if @enhanced_query[:contributors].present? %>
17+
<li>Contributors: <%= @enhanced_query[:contributors] %></li>
1818
<% end %>
19-
<% if params[:identifiers].present? %>
20-
<li>Identifiers: <%= params[:identifiers] %></li>
19+
<% if @enhanced_query[:fundingInformation].present? %>
20+
<li>Funders: <%= @enhanced_query[:fundingInformation] %></li>
2121
<% end %>
22-
<% if params[:locations].present? %>
23-
<li>Locations: <%= params[:locations] %></li>
22+
<% if @enhanced_query[:identifiers].present? %>
23+
<li>Identifiers: <%= @enhanced_query[:identifiers] %></li>
2424
<% end %>
25-
<% if params[:subjects].present? %>
26-
<li>Subjects: <%= params[:subjects] %></li>
25+
<% if @enhanced_query[:locations].present? %>
26+
<li>Locations: <%= @enhanced_query[:locations] %></li>
2727
<% end %>
28-
<% if params[:title].present? %>
29-
<li>Title: <%= params[:title] %></li>
28+
<% if @enhanced_query[:subjects].present? %>
29+
<li>Subjects: <%= @enhanced_query[:subjects] %></li>
3030
<% end %>
31-
<% if params[:source].present? %>
32-
<li>Source: <%= params[:source] %></li>
31+
<% if @enhanced_query[:title].present? %>
32+
<li>Title: <%= @enhanced_query[:title] %></li>
33+
<% end %>
34+
<% if @enhanced_query[:source].present? %>
35+
<li>Source: <%= @enhanced_query[:source] %></li>
3336
<% end %>
3437
</ul>
3538
</p>
@@ -55,7 +58,9 @@
5558
<aside class="col1q facet-container">
5659
<div id="facets">
5760
<h2>Available filters</h2>
58-
<%= render(partial: 'search/facet', collection: @facets) || render('facet_empty') %>
61+
<% @facets&.each do |category, values| %>
62+
<%= render(partial: 'search/facet', locals: {category: category, values: values}) %>
63+
<% end %>
5964
</div>
6065
</aside>
6166
</div>

test/helpers/facet_helper_test.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'test_helper'
2+
3+
class FacetHelperTest < ActionView::TestCase
4+
include FacetHelper
5+
6+
test 'add_facet will support adding a facet parameter to a search URL' do
7+
original_query = {
8+
page: 1,
9+
q: 'data'
10+
}
11+
expected_query = {
12+
page: 1,
13+
q: 'data',
14+
contentType: 'dataset'
15+
}
16+
assert_equal expected_query, add_facet(original_query, 'contentType', 'dataset')
17+
end
18+
19+
test 'add_facet will reset a page count when called' do
20+
original_query = {
21+
page: 3,
22+
q: 'data'
23+
}
24+
expected_query = {
25+
page: 1,
26+
q: 'data',
27+
contentType: 'dataset'
28+
}
29+
assert_equal expected_query, add_facet(original_query, 'contentType', 'dataset')
30+
end
31+
32+
test 'nice_labels allows translation of machine categories to human readable headings' do
33+
needle = 'contentType'
34+
assert_equal 'Content types', nice_labels[needle]
35+
end
36+
37+
test 'nice_labels returns nil if a category is not mapped yet' do
38+
needle = 'foo'
39+
assert_nil nice_labels[needle]
40+
end
41+
42+
test 'remove_facet will remove a specific facet parameter from a search URL' do
43+
original_query = {
44+
page: 1,
45+
q: 'data',
46+
contentType: 'dataset'
47+
}
48+
expected_query = {
49+
page: 1,
50+
q: 'data'
51+
}
52+
assert_equal expected_query, remove_facet(original_query, 'contentType')
53+
end
54+
55+
test 'remove_facet will reset a page count when called' do
56+
original_query = {
57+
page: 3,
58+
q: 'data',
59+
contentType: 'dataset'
60+
}
61+
expected_query = {
62+
page: 1,
63+
q: 'data'
64+
}
65+
assert_equal expected_query, remove_facet(original_query, 'contentType')
66+
end
67+
end

0 commit comments

Comments
 (0)