Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/helpers/results_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,33 @@ def article?(format)

format.match?(/\barticle\b/i)
end

# Determines if a result has any fulfillment links to render
#
# @param result [Hash] A normalized Primo result hash
# @return [Boolean] True if the result has links, availability, or ThirdIron/OpenAlex triggers
def result_get?(result)
renderable_links?(result) ||
result[:availability].present? ||
(Feature.enabled?(:oa_always) && article?(result[:format])) ||
thirdiron_content?(result)
end

private

def renderable_links?(result)
return false unless result[:links].present?
return true if Feature.enabled?(:record_link)

# If record_link is disabled, exclude results that have ONLY "full record" links
result[:links].any? { |link| link['kind'].downcase != 'full record' }
end

def thirdiron_content?(result)
ThirdIron.enabled? && (
result[:doi].present? ||
result[:pmid].present? ||
(result[:format]&.downcase == 'journal' && result[:issn].present?)
)
end
end
30 changes: 20 additions & 10 deletions app/javascript/controllers/content_loader_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ export default class extends Controller {
fetch(this.urlValue)
.then(response => response.text())
.then(html => {
const parentElement = this.element.parentElement;
// Replace the entire element with the fetched HTML
this.element.outerHTML = html;
// Hide primo links if libkey link is present
if (parentElement.querySelector('.libkey-link')) {
const resultGet = parentElement.closest('.result-get');
if (resultGet) {
const primoLinks = resultGet.querySelectorAll('.primo-link');
// removing instead of hiding to avoid layout issues when selecting which link to highlight
primoLinks.forEach(link => link.remove());
const parentElement = this.element.parentElement
// Replace the entire element with the fetched HTML, or remove if empty
if (html.trim()) {
this.element.outerHTML = html
// Hide primo links if libkey link is present
if (parentElement.querySelector('.libkey-link')) {
const resultGet = parentElement.closest('.result-get')
if (resultGet) {
const primoLinks = resultGet.querySelectorAll('.primo-link')
// removing instead of hiding to avoid layout issues when selecting which link to highlight
primoLinks.forEach(link => link.remove())
}
}
} else {
// Remove empty loader
this.element.remove()

// Remove parent empty container, confirming first that it's empty
if (!parentElement.textContent.trim()) {
parentElement.remove()
}
}
Comment thread
jazairi marked this conversation as resolved.
Comment thread
jazairi marked this conversation as resolved.
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with high complexity (count = 7): load [qlty:function-complexity]

Expand Down
3 changes: 3 additions & 0 deletions app/views/search/_result_primo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<% end %>
</div>


<% if result_get?(result) %>
<div class="result-get">
<% if result[:links].present? %>
<% result[:links].each do |link| %>
Expand Down Expand Up @@ -120,6 +122,7 @@
<%= render(partial: 'trigger_browzine', locals: { issn: result[:issn] }) %>
<% end %>
</div>
<% end %>
<% end %>
</div>
</li>
3 changes: 2 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
# config.i18n.raise_on_missing_translations = true

# Annotate rendered view with file names.
config.action_view.annotate_rendered_view_with_filenames = true
# Disabled because annotation comments pollute dynamically-loaded HTML fragments (e.g. ThirdIron responses)
# config.action_view.annotate_rendered_view_with_filenames = true

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
Expand Down
80 changes: 80 additions & 0 deletions test/helpers/results_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,84 @@ class ResultsHelperTest < ActionView::TestCase
assert_not article?(nil)
assert_not article?('')
end

test 'result_get? returns true when result has links' do
assert result_get?({ links: [{ 'kind' => 'PDF', 'url' => 'https://example.com' }] })
end

test 'result_get? returns true when result has availability' do
assert result_get?({ availability: 'Available' })
end

test 'result_get? returns true when ThirdIron enabled and result has doi' do
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
assert result_get?({ doi: '10.1234/test' })
end
end

test 'result_get? returns true when ThirdIron enabled and result has pmid' do
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
assert result_get?({ pmid: '12345678' })
end
end

test 'result_get? returns true when ThirdIron enabled and result is a journal with issn' do
ClimateControl.modify(THIRDIRON_ID: '123', THIRDIRON_KEY: 'abc') do
assert result_get?({ format: 'Journal', issn: '1234-5678' })
end
end

test 'result_get? returns true when oa_always enabled and result is an article' do
ClimateControl.modify(FEATURE_OA_ALWAYS: 'true') do
assert result_get?({ format: 'Article' })
end
end

test 'result_get? returns false when result has no fulfillment content' do
ClimateControl.modify(THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
assert_not result_get?({})
assert_not result_get?({ format: 'Book' })
end
end

test 'result_get? returns false when ThirdIron disabled even with doi' do
ClimateControl.modify(THIRDIRON_ID: nil, THIRDIRON_KEY: nil) do
assert_not result_get?({ doi: '10.1234/test' })
end
end

test 'result_get? returns false when oa_always disabled and result has no links' do
ClimateControl.modify(FEATURE_OA_ALWAYS: nil, THIRDIRON_ID: nil, THIRDIRON_KEY: nil) do
assert_not result_get?({ format: 'Article' })
end
end

test 'result_get? returns false when result has only full record link and feature is disabled' do
ClimateControl.modify(FEATURE_RECORD_LINK: nil, THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
assert_not result_get?({ links: [{ 'kind' => 'Full Record', 'url' => 'https://example.com' }] })
end
end

test 'result_get? returns true when result has only full record link and feature is enabled' do
ClimateControl.modify(FEATURE_RECORD_LINK: 'true', THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
assert result_get?({ links: [{ 'kind' => 'Full Record', 'url' => 'https://example.com' }] })
end
end

test 'result_get? returns true when result has PDF link even if record_link feature is disabled' do
ClimateControl.modify(FEATURE_RECORD_LINK: nil, THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
assert result_get?({ links: [{ 'kind' => 'PDF', 'url' => 'https://example.com' }] })
end
end

test 'result_get? returns true when result has both full record and PDF links even if record_link feature is disabled' do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [123/120] [rubocop:Layout/LineLength]

ClimateControl.modify(FEATURE_RECORD_LINK: nil, THIRDIRON_ID: nil, THIRDIRON_KEY: nil, FEATURE_OA_ALWAYS: nil) do
assert result_get?({
links: [
{ 'kind' => 'Full Record', 'url' => 'https://example.com' },
{ 'kind' => 'PDF', 'url' => 'https://pdf.example.com' }
]
})
end
end
end