diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 335aec27..ca04667c 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -14,6 +14,14 @@ def format_highlight_label(field_name) field_name.underscore.humanize end + def link_to_result(result) + if result['source_link'].present? + link_to(result['title'], result['source_link']) + else + result['title'] + end + end + def view_online(result) return unless result['source_link'].present? diff --git a/app/views/search/_result.html.erb b/app/views/search/_result.html.erb index fad44d90..42f20d0d 100644 --- a/app/views/search/_result.html.erb +++ b/app/views/search/_result.html.erb @@ -1,7 +1,8 @@
diff --git a/test/helpers/search_helper_test.rb b/test/helpers/search_helper_test.rb index 0ab09dd9..9e79b2a5 100644 --- a/test/helpers/search_helper_test.rb +++ b/test/helpers/search_helper_test.rb @@ -171,4 +171,36 @@ def setup } assert_equal ['Authors: person, sample'], applied_advanced_terms(query) end + + test 'link_to_result returns link when source_link is present' do + result = { + 'title' => 'Sample Document Title', + 'source_link' => 'https://example.com/document' + } + expected_link = 'Sample Document Title' + assert_equal expected_link, link_to_result(result) + end + + test 'link_to_result returns plain title when source_link is nil' do + result = { + 'title' => 'Sample Document Title', + 'source_link' => nil + } + assert_equal 'Sample Document Title', link_to_result(result) + end + + test 'link_to_result returns plain title when source_link is empty string' do + result = { + 'title' => 'Sample Document Title', + 'source_link' => '' + } + assert_equal 'Sample Document Title', link_to_result(result) + end + + test 'link_to_result returns plain title when source_link key is absent' do + result = { + 'title' => 'Sample Document Title' + } + assert_equal 'Sample Document Title', link_to_result(result) + end end