Skip to content

Commit b1c36d2

Browse files
authored
Merge pull request #278 from MITLibraries/use-187-timdex-tabs
Add new search tabs and improve tab handling
2 parents 20ad4d3 + 2d23db5 commit b1c36d2

16 files changed

+213
-47
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ See `Optional Environment Variables` for more information.
9898
mode. Note that this is currently intended _only_ for the geodata app and
9999
may have unexpected consequences if applied to other TIMDEX UI apps.
100100
- `FEATURE_SIMULATE_SEARCH_LATENCY`: DO NOT SET IN PRODUCTION. Set to ensure a minimum of a one second delay in returning search results. Useful to see spinners/loaders. Only introduces delay for results that take less than one second to complete.
101+
- `FEATURE_TAB_TIMDEX_ALL`: Display a tab for displaying the combined TIMDEX data
102+
- `FEATURE_TAB_TIMDEX_ALMA`: Display a tab for displaying Alma data from TIMDEX
101103
- `FILTER_ACCESS_TO_FILES`: The name to use instead of "Access to files" for that filter / aggregation.
102104
- `FILTER_CONTENT_TYPE`: The name to use instead of "Content type" for that filter / aggregation.
103105
- `FILTER_CONTRIBUTOR`: The name to use instead of "Contributor" for that filter / aggregation.

app/controllers/application_controller.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base
88
def set_active_tab
99
# GeoData doesn't use the tab system.
1010
return if Feature.enabled?(:geodata)
11-
11+
1212
@active_tab = if params[:tab].present? && valid_tab?(params[:tab])
1313
# If params[:tab] is set and valid, use it and set session
1414
cookies[:last_tab] = params[:tab]
@@ -21,9 +21,21 @@ def set_active_tab
2121
end
2222
end
2323

24+
def primo_tabs
25+
%w[alma cdi primo]
26+
end
27+
28+
def timdex_tabs
29+
%w[aspace timdex timdex_alma website]
30+
end
31+
32+
def all_tabs
33+
['all', *primo_tabs, *timdex_tabs]
34+
end
35+
2436
private
2537

2638
def valid_tab?(tab)
27-
%w[primo timdex all].include?(tab)
39+
all_tabs.include?(tab)
2840
end
2941
end

app/controllers/search_controller.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ def results
2323
render 'results_geo'
2424
return
2525
end
26-
26+
2727
# Route to appropriate search based on active tab
2828
case @active_tab
29-
when 'primo'
30-
load_primo_results
31-
when 'timdex'
32-
load_timdex_results
3329
when 'all'
3430
load_all_results
31+
when *primo_tabs
32+
load_primo_results
33+
when *timdex_tabs
34+
load_timdex_results
3535
end
3636
end
3737

@@ -108,7 +108,7 @@ def load_all_results
108108

109109
# For now, just use primo pagination as a placeholder
110110
@pagination = primo_data[:pagination] || {}
111-
111+
112112
# Handle primo continuation for high page numbers
113113
@show_primo_continuation = primo_data[:show_continuation] || false
114114
end
@@ -119,9 +119,7 @@ def fetch_primo_data
119119
offset = (current_page - 1) * per_page
120120

121121
# Check if we're beyond Primo API limits before making the request.
122-
if offset >= Analyzer::PRIMO_MAX_OFFSET
123-
return { results: [], pagination: {}, errors: nil, show_continuation: true }
124-
end
122+
return { results: [], pagination: {}, errors: nil, show_continuation: true } if offset >= Analyzer::PRIMO_MAX_OFFSET
125123

126124
primo_response = query_primo
127125
results = NormalizePrimoResults.new(primo_response, @enhanced_query[:q]).normalize
@@ -133,7 +131,7 @@ def fetch_primo_data
133131
# exists in Primo UI, sending users there seems like the best we can do.
134132
show_continuation = false
135133
errors = nil
136-
134+
137135
if results.empty?
138136
docs = primo_response['docs'] if primo_response.is_a?(Hash)
139137
if docs.nil? || docs.empty?
@@ -153,7 +151,7 @@ def fetch_timdex_data
153151
query = QueryBuilder.new(@enhanced_query).query
154152
response = query_timdex(query)
155153
errors = extract_errors(response)
156-
154+
157155
if errors.nil?
158156
pagination = Analyzer.new(@enhanced_query, response, :timdex).pagination
159157
raw_results = extract_results(response)
@@ -169,14 +167,18 @@ def active_filters
169167
end
170168

171169
def query_timdex(query)
170+
query[:sourceFilter] = ['MIT Libraries Website', 'LibGuides'] if @active_tab == 'website'
171+
query[:sourceFilter] = ['MIT ArchivesSpace'] if @active_tab == 'aspace'
172+
query[:sourceFilter] = ['MIT Alma'] if @active_tab == 'timdex_alma'
173+
172174
# We generate unique cache keys to avoid naming collisions.
173175
cache_key = generate_cache_key(query)
174176

175177
# Builder hands off to wrapper which returns raw results here.
176178
Rails.cache.fetch("#{cache_key}/#{@active_tab}", expires_in: 12.hours) do
177179
raw = if Feature.enabled?(:geodata)
178180
execute_geospatial_query(query)
179-
elsif @active_tab == 'timdex' || @active_tab == 'all'
181+
else
180182
TimdexBase::Client.query(TimdexSearch::BaseQuery, variables: query)
181183
end
182184

app/helpers/search_helper.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ def link_to_result(result)
2222
end
2323
end
2424

25-
def link_to_tab(target)
26-
if @active_tab == target.downcase
27-
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: target.downcase)),
28-
aria: { current: "page" },
29-
class: "active tab-link",
30-
data: { turbo_frame: "search-results", turbo_action: "advance" }
25+
# Generates a link for a search results tab, marking it as active if it matches the current active tab.
26+
# @param target [String] The name of the tab to link to
27+
# @param label [String] The display label for the tab (optional)
28+
# @return [String] HTML link element for the tab
29+
def link_to_tab(target, label = nil)
30+
clean_target = target.downcase.gsub(' ', '_').downcase
31+
tab_label = label || target
32+
if @active_tab == clean_target
33+
link_to tab_label, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
34+
aria: { current: 'page' },
35+
class: 'active tab-link',
36+
data: { turbo_frame: 'search-results', turbo_action: 'advance' }
3137
else
32-
link_to target, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: target.downcase)),
33-
class: "tab-link",
34-
data: { turbo_frame: "search-results", turbo_action: "advance" }
38+
link_to tab_label, results_path(params.permit(:q, :per_page, :booleanType, :tab).merge(tab: clean_target)),
39+
class: 'tab-link',
40+
data: { turbo_frame: 'search-results', turbo_action: 'advance' }
3541
end
3642
end
3743

app/models/feature.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#
3434
class Feature
3535
# List of all valid features in the application
36-
VALID_FEATURES = %i[geodata boolean_picker simulate_search_latency].freeze
36+
VALID_FEATURES = %i[geodata boolean_picker simulate_search_latency tab_timdex_all tab_timdex_alma].freeze
3737

3838
# Check if a feature is enabled by name
3939
#

app/models/normalize_primo_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(record, query)
88
def normalize
99
{
1010
# Core fields
11+
api: 'primo',
1112
title:,
1213
creators:,
1314
source:,

app/models/normalize_timdex_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(record, query)
88
def normalize
99
{
1010
# Core fields
11+
api: 'timdex',
1112
title:,
1213
creators:,
1314
source:,

app/views/search/_source_tabs.html.erb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
<nav id="tabs" class="tab-navigation" aria-label="Result type navigation">
33
<ul>
44
<li><%= link_to_tab("All") %></li>
5-
<li><%= link_to_tab("Primo") %></li>
6-
<li><%= link_to_tab("TIMDEX") %></li>
5+
<li><%= link_to_tab("Primo", "Articles and Catalog") %></li>
6+
<% if Feature.enabled?(:tab_timdex_all) %>
7+
<li><%= link_to_tab("TIMDEX") %></li>
8+
<% end %>
9+
<% if Feature.enabled?(:tab_timdex_alma) %>
10+
<li><%= link_to_tab("timdex_alma", "Alma (TIMDEX)") %></li>
11+
<% end %>
12+
<li><%= link_to_tab("Website") %></li>
13+
<li><%= link_to_tab("aspace", "Archival materials") %></li>
714
</ul>
815
</nav>
916

app/views/search/results.html.erb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@
2222
<p class="results-context-description">From all MIT Libraries sources</p>
2323
<div id="results-layout-wrapper">
2424
<main id="results">
25-
<ol class="results-list" start="<%= @pagination[:start] %>">
26-
<% case @active_tab %>
27-
<% when 'primo' %>
28-
<%= render(partial: 'search/result_primo', collection: @results, as: :result) %>
29-
<% when 'timdex' %>
30-
<%= render(partial: 'search/result', collection: @results, as: :result) %>
31-
<% when 'all' %>
32-
<% @results.each do |result| %>
33-
<% if result[:source] == 'Primo' %>
25+
<ol class="results-list" start="<%= @pagination[:start] %>">
26+
<% @results.each do |result| %>
27+
<% if result[:api] == 'primo' %>
3428
<%= render(partial: 'search/result_primo', locals: { result: result }) %>
35-
<% else %>
29+
<% elsif result[:api] == 'timdex' %>
3630
<%= render(partial: 'search/result', locals: { result: result }) %>
3731
<% end %>
3832
<% end %>
39-
<% end %>
4033
</ol>
4134
<%= render partial: "results_callouts" %>
4235
</main>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'test_helper'
2+
3+
class ApplicationControllerUnitTest < ActionController::TestCase
4+
setup do
5+
@controller = ApplicationController.new
6+
end
7+
8+
test '#primo_tabs returns expected tabs' do
9+
assert_equal %w[alma cdi primo], @controller.primo_tabs
10+
end
11+
12+
test '#timdex_tabs returns expected tabs' do
13+
assert_equal %w[aspace timdex timdex_alma website], @controller.timdex_tabs
14+
end
15+
16+
test '#all_tabs includes both primo and timdex tabs as well as all tab' do
17+
expected = %w[all alma cdi primo aspace timdex timdex_alma website]
18+
assert_equal expected, @controller.all_tabs
19+
end
20+
21+
test '#valid_tab? returns true for valid tabs' do
22+
assert @controller.send(:valid_tab?, 'all')
23+
assert @controller.send(:valid_tab?, 'alma')
24+
assert @controller.send(:valid_tab?, 'timdex')
25+
end
26+
27+
test '#valid_tab? returns false for invalid tabs' do
28+
refute @controller.send(:valid_tab?, 'invalid')
29+
refute @controller.send(:valid_tab?, '')
30+
end
31+
32+
test 'set_active_tab defaults @active_tab to all when no params or cookies are set' do
33+
@controller.stubs(:params).returns({})
34+
@controller.stubs(:cookies).returns({})
35+
@controller.set_active_tab
36+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
37+
end
38+
39+
test 'set_active_tab sets @active_tab to tab when tab params is valid' do
40+
@controller.stubs(:params).returns({ tab: 'primo' })
41+
@controller.stubs(:cookies).returns({})
42+
@controller.set_active_tab
43+
assert_equal 'primo', @controller.instance_variable_get(:@active_tab)
44+
end
45+
46+
test 'set_active_tab sets @active_tab to all when tab params is invalid and no cookie is set' do
47+
@controller.stubs(:params).returns({ tab: 'supertab' })
48+
@controller.stubs(:cookies).returns({})
49+
@controller.set_active_tab
50+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
51+
end
52+
53+
test 'set_active_tab sets @active_tab to cookie value when tab params is invalid and valid cookie is set' do
54+
@controller.stubs(:params).returns({ tab: 'supertab' })
55+
@controller.stubs(:cookies).returns({ last_tab: 'timdex' })
56+
@controller.set_active_tab
57+
assert_equal 'timdex', @controller.instance_variable_get(:@active_tab)
58+
end
59+
60+
test 'set_active_tab sets @active_tab to all value when tab params is invalid and cookie is invalid' do
61+
@controller.stubs(:params).returns({ tab: 'supertab' })
62+
@controller.stubs(:cookies).returns({ last_tab: 'woohoo' })
63+
@controller.set_active_tab
64+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
65+
end
66+
end

0 commit comments

Comments
 (0)