Skip to content

Commit

Permalink
WIP Allow batch requesters to create citations
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Jul 11, 2024
1 parent ab312de commit e9016ce
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
4 changes: 4 additions & 0 deletions app/controllers/citations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def create
case @citable
when InfoRequest
redirect_to show_request_path(citable.url_title), notice: notice
when InfoRequestBatch
redirect_to info_request_batch_path(citable), notice: notice
end
else
render :new
Expand All @@ -39,6 +41,8 @@ def resource
case params.fetch(:resource, 'InfoRequest')
when 'InfoRequest'
@resource ||= InfoRequest.find_by_url_title!(params[:url_title])
when 'InfoRequestBatch'
@resource ||= InfoRequestBatch.find_by_id!(params[:info_request_batch_id])
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ def initialize(user, project: nil, public_token: false)
end
end

can :create_citation, InfoRequest do |info_request|
user && (user.is_admin? || user.is_pro? || info_request.user == user)
can :create_citation, [InfoRequest, InfoRequestBatch] do |content|
user && (user.is_admin? || user.is_pro? || content.user == user)
end

can :share, InfoRequest do |info_request|
Expand Down
13 changes: 10 additions & 3 deletions app/views/citations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<h1><%= _('In the News') %></h1>

<p>
<%= _('Has this request been referenced in a news article or academic ' \
'paper? Let us know:') %>
<% if @resource.is_a?(InfoRequestBatch) %>
<%= _('Has this batch request been referenced in a news article or ' \
'academic paper? Let us know:') %>
<% else %>
<%= _('Has this request been referenced in a news article or academic ' \
'paper? Let us know:') %>
<% end %>
</p>

<%= form_for @citation, class: 'form form-horizontal' do |f| %>
<%= form_for @citation,
url: url_for(action: 'create', **params.permit(:resource, :url_title, :info_request_batch_id)),
class: 'form form-horizontal' do |f| %>
<%= foi_error_messages_for :citation %>

<p>
Expand Down
14 changes: 13 additions & 1 deletion app/views/info_request_batch/_citations.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<% if citations.any? %>
<% if can?(:create_citation, info_request_batch) || citations.any? %>
<div class="sidebar__section citations">
<h2><%= _('In the News') %></h2>

<% if citations.any? %>
<ul class="citations-list">
<%= render citations %>
</ul>
<% elsif can? :create_citation, info_request_batch %>
<p>
<%= _('Has this batch request been referenced in a news article or ' \
'academic paper?') %>
</p>
<% end %>
<% if can? :create_citation, info_request_batch %>
<%= link_to new_info_request_batch_citation_path(info_request_batch),
class: 'citations-new' do %>
<%= citations.any? ? _('New Citation') : _('Let us know') %>
<% end %>
<% end %>
</div>
<% end %>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ def matches?(request)
resources :widget_votes, :only => [:create]
end

resources :info_request_batch, :only => :show
resources :info_request_batch, :only => :show do
#### Citations controller
resources :citations, only: [:new, :create],
defaults: { resource: 'InfoRequestBatch' }

Check warning on line 243 in config/routes.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Align the keys of a hash literal if they span more than one line. Raw Output: config/routes.rb:243:7: C: Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.
####
end

#### OutgoingMessage controller
resources :outgoing_messages, :only => [] do
Expand Down
70 changes: 62 additions & 8 deletions spec/views/info_request_batch/_citations.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,31 @@ def render_view
{ citations: [], info_request_batch: info_request_batch }
end

before { render_view }
context 'the current_user cannot create a citation' do
before { ability.cannot :create_citation, info_request_batch }
before { render_view }

it 'renders nothing' do
expect(rendered).to be_blank
it 'renders nothing' do
expect(rendered).to be_blank
end
end

context 'the current_user can create a citation' do
before { ability.can :create_citation, info_request_batch }
before { render_view }

it 'renders the section' do
expect(rendered).to match(/In the News/)
end

it 'renders the blank slate text' do
expect(rendered).to match(/Has this batch request been referenced/)
end

it 'renders the link to add citations' do
expect(rendered).
to match(new_info_request_batch_citation_path(info_request_batch))
end
end
end

Expand All @@ -33,14 +54,47 @@ def render_view
info_request_batch: info_request_batch }
end

before { render_view }
context 'the current_user cannot create a citation' do
before { ability.cannot :create_citation, info_request_batch }
before { render_view }

it 'renders the section' do
expect(rendered).to match(/In the News/)
end

it 'renders the section' do
expect(rendered).to match(/In the News/)
it 'does not render the blank slate text' do
expect(rendered).not_to match(/Has this batch request been referenced/)
end

it 'renders the citations' do
expect(rendered).to match(/citations-list/)
end

it 'does not render the link to add a citation' do
expect(rendered).
not_to match(new_info_request_batch_citation_path(info_request_batch))
end
end

it 'renders the citations' do
expect(rendered).to match(/citations-list/)
context 'the current_user can create a citation' do
before { ability.can :create_citation, info_request_batch }
before { render_view }

it 'renders the section' do
expect(rendered).to match(/In the News/)
end

it 'does not render the blank slate text' do
expect(rendered).not_to match(/Has this request been referenced/)
end

it 'renders the citations' do
expect(rendered).to match(/citations-list/)
end

it 'renders the link to add citations' do
expect(rendered).to match('New Citation')
end
end
end
end

0 comments on commit e9016ce

Please sign in to comment.