Skip to content

Commit 6839bf9

Browse files
committed
Refactor full record URL evaluation to helper method
1 parent 4ddb717 commit 6839bf9

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

app/helpers/results_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def article?(format)
8181
format.match?(/\barticle\b/i)
8282
end
8383

84+
# Extracts the full record URL from a result's links
85+
#
86+
# @param result [Hash] A normalized result hash with :links
87+
# @return [String, nil] The URL of the 'full record' link, or nil if not found
88+
def full_record_url(result)
89+
return unless result.is_a?(Hash)
90+
91+
result[:links]&.find { |link| link['kind'] == 'full record' }&.dig('url')
92+
end
93+
8494
# Determines if a result has any fulfillment links to render
8595
#
8696
# @param result [Hash] A normalized Primo result hash

app/views/search/_result_primo.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<div class="title-lockup">
44
<%# NOTE: the order of the two items in this lockup are being swapped visually with CSS. The markup is ordered to make the screen reading experience more logical %>
55
<h3 class="record-title">
6-
<% if result[:links]&.find { |link| link['kind'] == 'full record' } %>
7-
<%= link_to(result[:title], result[:links].find { |link| link['kind'] == 'full record' }['url'], data: { content_piece: 'Result Title' }) %>
6+
<% if full_record_url(result) %>
7+
<%= link_to(result[:title], full_record_url(result), data: { content_piece: 'Result Title' }) %>
88
<% else %>
99
<%= result[:title] %>
1010
<% end %>
@@ -122,8 +122,7 @@
122122
<%# Trigger BrowZine lookup (render inside result-get so injected HTML
123123
is part of the flex `.result-get` area and receives expected styles) %>
124124
<% if ThirdIron.enabled? && result[:format].downcase == 'journal' && result[:issn].present? %>
125-
<% full_record_url = result[:links]&.find { |link| link['kind'] == 'full record' }&.dig('url') %>
126-
<%= render(partial: 'trigger_browzine', locals: { issn: result[:issn], full_record_url: full_record_url }) %>
125+
<%= render(partial: 'trigger_browzine', locals: { issn: result[:issn], full_record_url: full_record_url(result) }) %>
127126
<% end %>
128127
</div>
129128
<% end %>

test/helpers/results_helper_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,38 @@ class ResultsHelperTest < ActionView::TestCase
191191
})
192192
end
193193
end
194+
195+
test 'full_record_url returns the URL when result has a full record link' do
196+
result = {
197+
links: [
198+
{ 'kind' => 'PDF', 'url' => 'https://pdf.example.com' },
199+
{ 'kind' => 'full record', 'url' => 'https://primo.example.com/record' }
200+
]
201+
}
202+
assert_equal 'https://primo.example.com/record', full_record_url(result)
203+
end
204+
205+
test 'full_record_url returns nil when result has no links' do
206+
result = {}
207+
assert_nil full_record_url(result)
208+
end
209+
210+
test 'full_record_url returns nil when result has links but no full record link' do
211+
result = {
212+
links: [
213+
{ 'kind' => 'PDF', 'url' => 'https://pdf.example.com' },
214+
{ 'kind' => 'HTML', 'url' => 'https://html.example.com' }
215+
]
216+
}
217+
assert_nil full_record_url(result)
218+
end
219+
220+
test 'full_record_url returns nil when result is nil' do
221+
assert_nil full_record_url(nil)
222+
end
223+
224+
test 'full_record_url returns nil when result[:links] is nil' do
225+
result = { links: nil }
226+
assert_nil full_record_url(result)
227+
end
194228
end

0 commit comments

Comments
 (0)