Skip to content

Commit df76681

Browse files
authored
Merge pull request #60 from MITLibraries/rdi-200-search-multiple-sources-ui
Initial multiple source searching
2 parents fff30c6 + 1ef0a15 commit df76681

40 files changed

+530
-145
lines changed

app/assets/stylesheets/timdexui.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ body {
1414
.top-space {
1515
margin-top: 2.4rem;
1616
}
17+
18+
#advanced-search-panel legend {
19+
color: #000
20+
}

app/helpers/application_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
module ApplicationHelper
2+
def timdex_sources
3+
ENV.fetch('TIMDEX_SOURCES', timdex_source_defaults).split(',')
4+
end
5+
6+
def timdex_source_defaults
7+
['DSpace@MIT', 'Abdul Latif Jameel Poverty Action Lab Dataverse',
8+
'Woods Hole Open Access Server', 'Zenodo'].join(',')
9+
end
210
end

app/helpers/form_helper.rb

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

app/views/search/_form.html.erb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040

4141
<div class="field-wrap">
4242
<label for="advanced-fundingInformation" class="field-label">Funding information</label>
43-
<input type="text" class="field field-text wide" id="advanced-fundingInformation" name="Funding Information" placeholder="fundingInformation" value=<%= params[:fundingInformation] %>>
43+
<input type="text" class="field field-text wide" id="advanced-fundingInformation" name="Funding Information" placeholder="Funding information" value=<%= params[:fundingInformation] %>>
4444
</div>
4545

4646
<div class="field-wrap">
@@ -63,18 +63,15 @@ end
6363
<input type="text" class="field field-text wide" id="advanced-title" name="title" placeholder="Title" value=<%= params[:title] %>>
6464
</div>
6565

66-
<div class="field-wrap">
67-
<label for="advanced-source" class="field-label">Source</label>
68-
<select id="advanced-source" class="field field-select" name="source">
69-
<option></option>
70-
<option <%= "selected" if params[:source] == "Abdul Latif Jameel Poverty Action Lab Dataverse"%>>Abdul Latif Jameel Poverty Action Lab Dataverse</option>
71-
<option <%= "selected" if params[:source] == "DSpace@MIT"%>>DSpace@MIT</option>
72-
<option <%= "selected" if params[:source] == "Woods Hole Open Access Server"%>>Woods Hole Open Access Server</option>
73-
<option <%= "selected" if params[:source] == "Zenodo"%>>Zenodo</option>
74-
<option>ArchivesSpace@MIT (no data yet)</option>
75-
</select>
76-
NOTE: to allow selecting multiple sources we need to change the way we handle this in TIMDEX to treat source as an
77-
"OR" search not "AND"
66+
<div class="field-wrap list-checkboxes">
67+
<%# https://www.w3.org/WAI/tutorials/forms/grouping/ %>
68+
<fieldset>
69+
<legend>Limit search to checked sources.</legend>
70+
71+
<% timdex_sources.each do |source| %>
72+
<%= source_checkbox(source, params) %>
73+
<% end %>
74+
</fieldset>
7875
</div>
7976
</div>
8077
</details>

test/controllers/search_controller_test.rb

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
4747
assert_select 'input#advanced-locations', { count: 1 }
4848
assert_select 'input#advanced-subjects', { count: 1 }
4949
assert_select 'input#advanced-title', { count: 1 }
50-
assert_select 'select#advanced-source', { count: 1 }
50+
assert_select 'input.source', { minimum: 3 }
51+
end
52+
53+
test 'advanced search source checkboxes can be controlled by env' do
54+
get '/'
55+
assert_select 'input.source', { minimum: 3 }
56+
57+
ClimateControl.modify TIMDEX_SOURCES: 'HIGHLANDER' do
58+
get '/'
59+
assert_select 'input.source', { count: 1 }
60+
end
61+
62+
ClimateControl.modify TIMDEX_SOURCES: 'SITH,LORDS' do
63+
get '/'
64+
assert_select 'input.source', { count: 2 }
65+
end
5166
end
5267

5368
test 'results with no query redirects with info' do
@@ -305,4 +320,63 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
305320
assert_select 'li', 'Source: sauce'
306321
end
307322
end
323+
324+
def source_facet_count(controller)
325+
controller.view_context.assigns['facets']['source'].count
326+
end
327+
328+
test 'advanced search can limit to a single source' do
329+
VCR.use_cassette('advanced source limit to one source',
330+
allow_playback_repeats: true,
331+
match_requests_on: %i[method uri body]) do
332+
query = {
333+
q: 'data',
334+
advanced: 'true',
335+
source: ['dspace@mit']
336+
}.to_query
337+
get "/results?#{query}"
338+
assert_response :success
339+
assert_nil flash[:error]
340+
341+
assert(source_facet_count(@controller) == 1)
342+
end
343+
end
344+
345+
test 'advanced search defaults to all sources' do
346+
VCR.use_cassette('advanced source defaults to all',
347+
allow_playback_repeats: true,
348+
match_requests_on: %i[method uri body]) do
349+
query = {
350+
q: 'data',
351+
advanced: 'true'
352+
}.to_query
353+
get "/results?#{query}"
354+
assert_response :success
355+
assert_nil flash[:error]
356+
357+
# Assumption is we'll always have at least 3 default sources
358+
# DSpace, Aspace, Aleph (RDI sources are in now, but may not be default later)
359+
assert(source_facet_count(@controller) > 3)
360+
end
361+
end
362+
363+
test 'advanced search can limit to multiple sources' do
364+
VCR.use_cassette('advanced source limit to two sources',
365+
allow_playback_repeats: true,
366+
match_requests_on: %i[method uri body]) do
367+
# NOTE: when regenerating cassettes, if the source data does not have these
368+
# sources you may need to change the source array below to two sources that are
369+
# valid in the current available data.
370+
query = {
371+
q: 'data',
372+
advanced: 'true',
373+
source: ['dspace@mit', 'Woods Hole Open Access Server']
374+
}.to_query
375+
get "/results?#{query}"
376+
assert_response :success
377+
assert_nil flash[:error]
378+
379+
assert(source_facet_count(@controller) == 2)
380+
end
381+
end
308382
end

test/helpers/form_helper_test.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'test_helper'
2+
3+
class FormHelperTest < ActionView::TestCase
4+
include FormHelper
5+
6+
test 'checkbox is checked if source is only source in params' do
7+
source = 'hallo'
8+
params = { source: ['hallo'] }
9+
expected = "<div class='field-subitem'>
10+
<label class='field-checkbox'>
11+
<input type='checkbox' value='hallo' name='source[]' class='source' checked>
12+
hallo
13+
</label>
14+
</div>"
15+
actual = source_checkbox(source, params)
16+
17+
assert_equal(expected, actual)
18+
end
19+
20+
test 'checkbox is checked if source is one source in params' do
21+
source = 'hallo'
22+
params = { source: ['popcorn', 'hallo', 'dspace@mit'] }
23+
expected = "<div class='field-subitem'>
24+
<label class='field-checkbox'>
25+
<input type='checkbox' value='hallo' name='source[]' class='source' checked>
26+
hallo
27+
</label>
28+
</div>"
29+
actual = source_checkbox(source, params)
30+
31+
assert_equal(expected, actual)
32+
end
33+
34+
test 'checkbox is unchecked if no sources are in params' do
35+
source = 'hallo'
36+
params = {}
37+
expected = "<div class='field-subitem'>
38+
<label class='field-checkbox'>
39+
<input type='checkbox' value='hallo' name='source[]' class='source'>
40+
hallo
41+
</label>
42+
</div>"
43+
actual = source_checkbox(source, params)
44+
45+
assert_equal(expected, actual)
46+
end
47+
48+
test 'checkbox is unchecked if source is not one of sources in params' do
49+
source = 'hallo'
50+
params = { source: ['popcorn', 'nothallo', 'dspace@mit'] }
51+
expected = "<div class='field-subitem'>
52+
<label class='field-checkbox'>
53+
<input type='checkbox' value='hallo' name='source[]' class='source'>
54+
hallo
55+
</label>
56+
</div>"
57+
actual = source_checkbox(source, params)
58+
59+
assert_equal(expected, actual)
60+
end
61+
end

0 commit comments

Comments
 (0)