Skip to content

Commit 0265e9d

Browse files
committed
Add helper method to handle cases where stimulus controller JS doesn't fire
1 parent 57a4d80 commit 0265e9d

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

app/helpers/results_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,20 @@ def article?(format)
8080

8181
format.match?(/\barticle\b/i)
8282
end
83+
84+
# Determines if a result has any fulfillment links to render
85+
#
86+
# @param result [Hash] A normalized Primo result hash
87+
# @return [Boolean] True if the result has links, availability, or ThirdIron/OpenAlex triggers
88+
def result_get?(result)
89+
return true if result[:links].present? || result[:availability].present?
90+
return true if Feature.enabled?(:oa_always) && article?(result[:format])
91+
92+
if ThirdIron.enabled?
93+
return true if result[:doi].present? || result[:pmid].present?
94+
return true if result[:format]&.downcase == 'journal' && result[:issn].present?
95+
end
96+
97+
false
98+
end
8399
end

app/javascript/controllers/content_loader_controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export default class extends Controller {
2525
}
2626
}
2727
} else {
28+
// Remove empty loader
2829
this.element.remove()
30+
31+
// Remove parent empty container, confirming first that it's empty
2932
if (!parentElement.textContent.trim()) {
3033
parentElement.remove()
3134
}

app/views/search/_result_primo.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<% end %>
7676
</div>
7777

78+
<% if result_get?(result) %>
7879
<div class="result-get">
7980
<% if result[:links].present? %>
8081
<% result[:links].each do |link| %>
@@ -120,6 +121,7 @@
120121
<%= render(partial: 'trigger_browzine', locals: { issn: result[:issn] }) %>
121122
<% end %>
122123
</div>
124+
<% end %>
123125
<% end %>
124126
</div>
125127
</li>

test/helpers/results_helper_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,55 @@ class ResultsHelperTest < ActionView::TestCase
111111
assert_not article?(nil)
112112
assert_not article?('')
113113
end
114+
115+
test 'result_get? returns true when result has links' do
116+
assert result_get?({ links: [{ 'kind' => 'PDF', 'url' => 'https://example.com' }] })
117+
end
118+
119+
test 'result_get? returns true when result has availability' do
120+
assert result_get?({ availability: 'Available' })
121+
end
122+
123+
test 'result_get? returns true when ThirdIron enabled and result has doi' do
124+
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
125+
assert result_get?({ doi: '10.1234/test' })
126+
end
127+
end
128+
129+
test 'result_get? returns true when ThirdIron enabled and result has pmid' do
130+
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
131+
assert result_get?({ pmid: '12345678' })
132+
end
133+
end
134+
135+
test 'result_get? returns true when ThirdIron enabled and result is a journal with issn' do
136+
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
137+
assert result_get?({ format: 'Journal', issn: '1234-5678' })
138+
end
139+
end
140+
141+
test 'result_get? returns true when oa_always enabled and result is an article' do
142+
ClimateControl.modify(FEATURE_OA_ALWAYS: 'true') do
143+
assert result_get?({ format: 'Article' })
144+
end
145+
end
146+
147+
test 'result_get? returns false when result has no fulfillment content' do
148+
ClimateControl.modify(THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
149+
assert_not result_get?({})
150+
assert_not result_get?({ format: 'Book' })
151+
end
152+
end
153+
154+
test 'result_get? returns false when ThirdIron disabled even with doi' do
155+
ClimateControl.modify(THIRDIRON_ID: nil, THIRDIRON_KEY: nil) do
156+
assert_not result_get?({ doi: '10.1234/test' })
157+
end
158+
end
159+
160+
test 'result_get? returns false when oa_always disabled and result has no links' do
161+
ClimateControl.modify(FEATURE_OA_ALWAYS: nil, THIRDIRON_ID: nil, THIRDIRON_KEY: nil) do
162+
assert_not result_get?({ format: 'Article' })
163+
end
164+
end
114165
end

0 commit comments

Comments
 (0)