Skip to content

Commit

Permalink
Add citation_title helper
Browse files Browse the repository at this point in the history
Use `title` over the `source_url` but fallback if not present.
  • Loading branch information
gbp committed Oct 7, 2024
1 parent 4b3ab56 commit 27ba748
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/helpers/admin/citations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Admin::CitationsHelper
other: '🌐'
}.with_indifferent_access.freeze

def citation_title(citation)
citation.title.presence || citation.source_url
end

def citation_icon(citation)
citation_icon_for_type(citation.type)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/citations/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<span class="item-title span6">
<%= citation_icon(citation) %>
<tt><%= link_to citation_source_url, edit_admin_citation_path(citation) %></tt>
<tt><%= link_to citation_title(citation), edit_admin_citation_path(citation) %></tt>
</span>

<span class="item-metadata span6">
Expand Down
6 changes: 1 addition & 5 deletions app/views/citations/_citation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
</div>

<div class="citation__title">
<strong>
<%= MySociety::Format.make_clickable(
citation.source_url, contract: true, nofollow: true
).html_safe %>
</strong>
<strong><%= link_to citation_title(citation), citation.source_url, rel: 'nofollow' %></strong>
<br>
<% case citation.citable %>
<% when InfoRequest %>
Expand Down
6 changes: 1 addition & 5 deletions app/views/citations/_compact.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<li class="citations-list__citation--compact">
<%=
MySociety::Format.make_clickable(
citation.source_url, contract: true, nofollow: true
).html_safe
%>
<%= link_to citation_title(citation), citation.source_url, rel: 'nofollow' %>
</li>
17 changes: 17 additions & 0 deletions spec/helpers/admin/citations_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
RSpec.describe Admin::CitationsHelper do
include Admin::CitationsHelper

describe '#citation_title' do
subject { citation_title(citation) }

context 'with a citation that has a title' do
let(:citation) { FactoryBot.build(:citation, title: 'Example Title') }
it { is_expected.to eq('Example Title') }
end

context 'with a citation that has a blank title' do
let(:citation) do
FactoryBot.build(:citation, title: '', source_url: 'http://example.com')
end

it { is_expected.to eq('http://example.com') }
end
end

describe '#citation_icon' do
subject { citation_icon(citation) }

Expand Down

0 comments on commit 27ba748

Please sign in to comment.