Skip to content

Commit

Permalink
Added edit option for snapshot title and description.
Browse files Browse the repository at this point in the history
  • Loading branch information
fherreazcue authored and fbacall committed Sep 26, 2024
1 parent 4158a78 commit c1ea18b
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 6 deletions.
21 changes: 19 additions & 2 deletions app/controllers/snapshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class SnapshotsController < ApplicationController
before_action :find_resource
before_action :auth_resource, only: [:mint_doi_confirm, :mint_doi, :new, :create, :export_preview, :export_submit, :destroy]
before_action :auth_resource, only: [:mint_doi_confirm, :mint_doi, :new, :create, :edit, :update, :export_preview, :export_submit, :destroy]
before_action :check_resource_permitted_for_ro, only: [:new, :create]
before_action :find_snapshot, only: [:show, :mint_doi_confirm, :mint_doi, :download, :export_preview, :export_submit, :destroy]
before_action :find_snapshot, only: [:show, :edit, :update, :mint_doi_confirm, :mint_doi, :download, :export_preview, :export_submit, :destroy]
before_action :doi_minting_enabled?, only: [:mint_doi_confirm, :mint_doi]
before_action :zenodo_oauth_client
before_action :zenodo_oauth_session, only: [:export_submit]
Expand Down Expand Up @@ -40,6 +40,23 @@ def show
def new
end

def edit
if @snapshot.has_doi?
flash[:error] = "You cannot modify a snapshot that has a DOI."
redirect_to polymorphic_path([@resource, @snapshot])
end
end

def update
if @snapshot.has_doi?
flash[:error] = "You cannot modify a snapshot that has a DOI."
else
@snapshot.update(snapshot_params)
flash[:notice] = "Snapshot updated"
end
redirect_to polymorphic_path([@resource, @snapshot])
end

def download
@content_blob = @snapshot.content_blob
send_file @content_blob.filepath,
Expand Down
2 changes: 2 additions & 0 deletions app/views/snapshots/_buttons.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<% if @snapshot.can_mint_doi? %>
<%= button_link_to("Generate a DOI", 'doi', polymorphic_path([@resource, @snapshot], action: 'mint_doi_confirm')) %>
<% end %>
<%= button_link_to("Edit",'edit', polymorphic_path([@resource, @snapshot], action: 'edit'),
'data-tooltip' => 'Modify the title and/or description of your snapshot') %>
<%= button_link_to("Delete", 'destroy', polymorphic_path([@resource, @snapshot]),
{ confirm: "Are you sure you wish to delete this snapshot?", method: :delete }) %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/snapshots/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
<%= f.label :description -%><br/>
<%= f.text_area :description, rows: 3, :class=>"form-control"-%>
</div>

<%= f.submit(:class => 'btn btn-primary') %>
7 changes: 7 additions & 0 deletions app/views/snapshots/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Edit <%= t('snapshot') %></h1>

<%= form_for [@resource, @snapshot] do |f| %>
<%= render :partial => "form", :locals => { :f => f, :action=>:edit } -%>

or <%= cancel_button polymorphic_path([@resource, @snapshot]) -%>
<% end -%>
2 changes: 0 additions & 2 deletions app/views/snapshots/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
<%= form_for [@resource, @resource.snapshots.build] do |f| -%>
<%= render :partial => "form", :locals => { :f => f, :action=>:new } -%>

<%= f.submit(:class => 'btn btn-primary') %>

<%= image_tag_for_key('publish', polymorphic_path(@resource, :action => :check_related_items), nil, {:method=>:post,:class => 'btn btn-default'}, "Publish full #{t(@resource.class.name.downcase)}") if excluded_items.any? %>

or <%= cancel_button polymorphic_path(@resource) -%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/snapshots/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= @snapshot.title -%>
<small>(snapshot <%= @snapshot.snapshot_number -%>)</small>
<% end %>
<%= render :partial => "general/item_title", :locals => {:item=>@snapshot, :title=>snapshot_display_name(@snapshot), :buttons_partial => 'snapshots/buttons'} %>
<%= render :partial => "general/item_title", :locals => {:item=>@snapshot, :buttons_partial => 'snapshots/buttons'} %>
<div class="row">
<div class="col-md-9 col-sm-8">
<%= item_description @snapshot.description -%>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
end

concern :has_snapshots do
resources :snapshots, only: [:show, :new, :create, :destroy], concerns: [:has_doi] do
resources :snapshots, concerns: [:has_doi] do
member do
get :download
get :export, action: :export_preview
Expand Down
77 changes: 77 additions & 0 deletions test/functional/snapshots_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,84 @@ class SnapshotsControllerTest < ActionController::TestCase
assert flash[:error].include?('exist')
end

test 'edit button is shown for authorized users' do
create_investigation_snapshot
login_as(@user)
get :show, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_select 'a', text: 'Edit'
end

test 'edit button not shown for unauthorized users' do
create_investigation_snapshot
login_as(Factory(:user))
get :show, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_select 'a', text: 'Edit', count: 0
end

test 'edit button not shown for snapshots with DOI' do
create_investigation_snapshot
login_as(@user)
@snapshot.doi = '10.5072/123'
@snapshot.save
get :show, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_select 'a', text: 'Edit', count: 0
end

test 'authorized users can edit snapshot title and description' do
create_investigation_snapshot
login_as(@user)
get :edit, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_response :success
end

test "unauthorized user can't edit snapshot" do
create_investigation_snapshot
login_as(Factory(:user))
get :edit, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_redirected_to investigation_path(@investigation)
assert flash[:error]
end

test "can't edit snapshot with DOI" do
create_investigation_snapshot
login_as(@user)
@snapshot.doi = '10.5072/123'
@snapshot.save
get :edit, params: { investigation_id: @investigation, id: @snapshot.snapshot_number }
assert_redirected_to investigation_snapshot_path(@investigation, @snapshot)
assert flash[:error].include?('DOI')
end

test 'authorized users can update snapshot' do
create_investigation_snapshot
login_as(@user)
put :update, params: { investigation_id: @investigation, id: @snapshot.snapshot_number,
snapshot: { title: 'My mod snapshot', description: 'Snapshot mod info' } }
assert_redirected_to investigation_snapshot_path(@investigation, @snapshot)
@snapshot.reload
assert_equal 'My mod snapshot', @snapshot.title
assert_equal 'Snapshot mod info', @snapshot.description
end

test "unauthorized users can't update snapshot" do
create_investigation_snapshot
login_as(Factory(:user))
put :update, params: { investigation_id: @investigation, id: @snapshot.snapshot_number,
snapshot: { title: 'My mod snapshot', description: 'Snapshot mod info' } }
assert_redirected_to investigation_path(@investigation)
assert flash[:error]
end

test "can't update snapshot with doi" do
create_investigation_snapshot
login_as(@user)
@snapshot.doi = '10.5072/123'
@snapshot.save
put :update, params: { investigation_id: @investigation, id: @snapshot.snapshot_number,
snapshot: { title: 'My mod snapshot', description: 'Snapshot mod info' } }
assert_redirected_to investigation_snapshot_path(@investigation, @snapshot)
assert flash[:error].include?('DOI')
end

test 'can get confirmation when minting DOI for snapshot' do
datacite_mock
Expand Down

0 comments on commit c1ea18b

Please sign in to comment.