Skip to content

Commit

Permalink
Merge 3c65e3b into 08aa218
Browse files Browse the repository at this point in the history
  • Loading branch information
garethrees committed Feb 9, 2024
2 parents 08aa218 + 3c65e3b commit 4dcf8f7
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 10 deletions.
17 changes: 17 additions & 0 deletions app/helpers/admin/citations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Admin::CitationsHelper
ICONS = {
journalism: '🗞️',
campaigning: '📣',
academic: '🎓',
other: '🌐'
}.with_indifferent_access.freeze

def citation_icon(citation)
html_attrs = {
title: citation.type.humanize,
class: "citation-icon citation-icon--#{citation.type}"
}

tag.span(ICONS.fetch(citation.type), **html_attrs)
end
end
1 change: 1 addition & 0 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module AdminHelper
include Admin::BootstrapHelper
include Admin::CensorRulesHelper
include Admin::CitationsHelper
include Admin::LinkHelper
include Admin::ProminenceHelper

Expand Down
2 changes: 1 addition & 1 deletion app/models/citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Citation < ApplicationRecord
message: _('Source URL is too long') },
format: { with: /\Ahttps?:\/\/.*\z/,
message: _('Please enter a Source URL') }
validates :type, inclusion: { in: %w(news_story academic_paper other),
validates :type, inclusion: { in: %w(journalism academic campaigning other),
message: _('Please select a type') }

scope :newest, ->(limit = 1) do
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/citations/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<% citations.each do |citation| %>
<div class="row">
<span class="item-title span6">
<%= citation_icon(citation) %>
<tt><%= link_to citation.source_url, citation.source_url %></tt>
</span>

Expand Down
17 changes: 11 additions & 6 deletions app/views/citations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
<%= _('What type of article is it?') %>
</span>

<label for="citation_type_news_story" class="form_label">
<%= f.radio_button :type, 'news_story' %>
<%= _('News story') %>
<label for="citation_type_journalism" class="form_label">
<%= f.radio_button :type, 'journalism' %>
<%= _('Journalism') %>
</label>

<label for="citation_type_academic_paper" class="form_label">
<%= f.radio_button :type, 'academic_paper' %>
<%= _('Academic paper') %>
<label for="citation_type_campaigning" class="form_label">
<%= f.radio_button :type, 'campaigning' %>
<%= _('Campaigning') %>
</label>

<label for="citation_type_academic" class="form_label">
<%= f.radio_button :type, 'academic' %>
<%= _('Academic') %>
</label>

<label for="citation_type_other" class="form_label">
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20240209162933_change_citation_type_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class ChangeCitationTypeValues < ActiveRecord::Migration[7.0]
CHANGES = {
'news_story' => 'journalism',
'academic_paper' => 'academic'
}

def up
perform(CHANGES)
end

def down
perform(CHANGES.invert)
end

private

def perform(data)
data.each_pair do |before, after|
Citation.where(type: before).update_all(type: after)
end
end
end
2 changes: 1 addition & 1 deletion spec/controllers/citations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def action
end

let(:params) do
{ source_url: 'http://example.com/news', type: 'news_story' }
{ source_url: 'http://example.com/news', type: 'journalism' }
end
let(:apply_to_batch) { nil }

Expand Down
27 changes: 27 additions & 0 deletions spec/helpers/admin/citations_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

RSpec.describe Admin::CitationsHelper do
include Admin::CitationsHelper

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

context 'with a journalism link' do
let(:citation) { FactoryBot.build(:citation, type: 'journalism') }
it { is_expected.to include('🗞️') }
it { is_expected.to include('citation-icon--journalism') }
end

context 'with an academic link' do
let(:citation) { FactoryBot.build(:citation, type: 'academic') }
it { is_expected.to include('🎓') }
it { is_expected.to include('citation-icon--academic') }
end

context 'with a generic link' do
let(:citation) { FactoryBot.build(:citation, type: 'other') }
it { is_expected.to include('🌐') }
it { is_expected.to include('citation-icon--other') }
end
end
end
6 changes: 4 additions & 2 deletions spec/models/citation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ def setup_for_x_scope_data
it 'requires known type' do
citation.type = 'foobar'
is_expected.not_to be_valid
citation.type = 'news_story'
citation.type = 'journalism'
is_expected.to be_valid
citation.type = 'academic_paper'
citation.type = 'campaigning'
is_expected.to be_valid
citation.type = 'academic'
is_expected.to be_valid
citation.type = 'other'
is_expected.to be_valid
Expand Down

0 comments on commit 4dcf8f7

Please sign in to comment.