Skip to content

Commit

Permalink
Add edit/update actions for project dataset
Browse files Browse the repository at this point in the history
Allow adding description and making the dataset public.
  • Loading branch information
gbp committed Jul 26, 2024
1 parent 579bcc6 commit 8fe4ae0
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 6 deletions.
23 changes: 21 additions & 2 deletions app/controllers/projects/dataset_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
##
# Controller which manages Project data downloads.
# Controller which manages viewing and downloading Project datasets.
#
class Projects::DatasetController < Projects::BaseController
skip_before_action :html_response

before_action :load_dataset_key_set
before_action :load_dataset_key_set, only: :show

def show
authorize! :view, @dataset_key_set
Expand All @@ -18,9 +18,28 @@ def show
end
end

def edit
authorize! :edit, @project
end

def update
authorize! :edit, @project

if @project.update(project_dataset_params)
redirect_to project_dataset_path(@project),
notice: _('Dataset was successfully updated.')
else
render :edit
end
end

private

def load_dataset_key_set
@dataset_key_set = @project.key_set
end

def project_dataset_params
params.require(:project).permit(:dataset_description, :dataset_public)
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Project < ApplicationRecord
validates :title, :owner, presence: true

has_rich_text :briefing
has_rich_text :dataset_description

def original_briefing
attributes['briefing']
Expand Down
46 changes: 46 additions & 0 deletions app/views/projects/dataset/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<% content_for :javascript_head do %>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/trix.umd.min.js"></script>
<% end %>

<div id="project-form" class="inner-canvas">
<div class="inner-canvas-header">
<div class="row">
<% @title = _("Edit your dataset") %>
<h1><%= @title %></h1>
</div>
</div>

<div class="inner-canvas-body">
<div class="row">
<%= foi_error_messages_for :project, full_message: true %>
</div>

<div class="row">
<%= form_for @project, url: project_dataset_path(@project) do |f| %>
<div class="form_input">
<%= f.label :dataset_description, _('Description'), class: 'form_label' %>
<%= f.rich_text_area :dataset_description %>
</div>

<div class="form_input">
<%= f.label :dataset_public, _('Visibility'), class: 'form_label' %>

<div class="radio_buttons">
<%= f.radio_button :dataset_public, true, disabled: @project.dataset_public? %>
<%= f.label :dataset_public, _('Public'), value: true %>
<%= f.radio_button :dataset_public, false, disabled: @project.dataset_public? %>
<%= f.label :dataset_public, _('Private'), value: false %>
</div>

<div class="form_note">
<%= _('When a dataset is made public, this action cannot be reverted to private.') %>
</div>
</div>

<div class="form_button">
<%= f.submit _('Update dataset') %>
</div>
<% end %>
</div> <!-- .row -->
</div> <!-- .inner-canvas-body -->
</div> <!-- #project-form.inner-canvas -->
21 changes: 18 additions & 3 deletions app/views/projects/dataset/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
</div>

<div class="inner-canvas-body">
<div class="row">
<%= @project.dataset_description %>
<% if can?(:edit, @project) %>
<% button_text = if @project.dataset_description.present?
@project.dataset_public? ? _('Edit description') : _('Edit description and make dataset public')
else
@project.dataset_public? ? _('Add description') : _('Add description and make dataset public')
end %>
<%= link_to button_text, edit_project_dataset_path(@project), class: 'button' %>
<% end %>
</div>

<div class="row">
<table class="dataset">
<% @export.data_for_web.each_with_index do |data, index| %>
Expand All @@ -24,9 +37,11 @@
<% end %>
</table>
<p>
<%= link_to _('Back to project'),
project_path(@project),
class: 'button-secondary' %>
<% if can? :read, @project %>
<%= link_to _('Back to project'),
project_path(@project),
class: 'button-secondary' %>
<% end %>
<%= link_to _('Download'),
project_dataset_path(@project, format: :csv),
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def matches?(request)

resource :contributors, only: [:destroy]

resource :dataset, controller: :dataset, only: [:show]
resource :dataset, controller: :dataset, only: [:show, :edit, :update]
resource :leaderboard, only: [:show], format: true
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/controllers/projects/dataset_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,52 @@ def show(format: 'csv')
end
end
end

describe 'PATCH #update' do
let(:project) { FactoryBot.create(:project, owner: user) }
let(:user) { FactoryBot.create(:user) }

before { sign_in(user) }

context 'when the user is authorized' do
it 'updates the project' do
patch :update, params: {
project_id: project.id,
project: { dataset_description: 'Updated description' }
}
project.reload
expect(project.dataset_description.to_plain_text).
to eq('Updated description')
end

it 'redirects to the project dataset show page' do
patch :update, params: {
project_id: project.id,
project: { dataset_description: 'Updated description' }
}
expect(response).to redirect_to(project_dataset_path(project))
end

it 'sets a success flash notice' do
patch :update, params: {
project_id: project.id,
project: { dataset_description: 'Updated description' }
}
expect(flash[:notice]).to eq('Dataset was successfully updated.')
end
end

context 'when the user is not authorized' do
before do
allow(controller).to receive(:authorize!).with(:edit, project).
and_raise(CanCan::AccessDenied)
end

it 'raises an authorization error' do
expect {
patch :update, params: { project_id: project.id, project: {} }
}.to raise_error(CanCan::AccessDenied)
end
end
end
end

0 comments on commit 8fe4ae0

Please sign in to comment.