From c1ea18bfcb4f948a9b10a25a18dbd5d40dd3b111 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Fri, 9 Dec 2022 13:33:35 +0000 Subject: [PATCH] Added edit option for snapshot title and description. --- app/controllers/snapshots_controller.rb | 21 +++++- app/views/snapshots/_buttons.html.erb | 2 + app/views/snapshots/_form.html.erb | 2 + app/views/snapshots/edit.html.erb | 7 ++ app/views/snapshots/new.html.erb | 2 - app/views/snapshots/show.html.erb | 2 +- config/routes.rb | 2 +- test/functional/snapshots_controller_test.rb | 77 ++++++++++++++++++++ 8 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 app/views/snapshots/edit.html.erb diff --git a/app/controllers/snapshots_controller.rb b/app/controllers/snapshots_controller.rb index aff98280b6..f23d1744b8 100644 --- a/app/controllers/snapshots_controller.rb +++ b/app/controllers/snapshots_controller.rb @@ -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] @@ -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, diff --git a/app/views/snapshots/_buttons.html.erb b/app/views/snapshots/_buttons.html.erb index 9a932bf48b..62aba7d695 100644 --- a/app/views/snapshots/_buttons.html.erb +++ b/app/views/snapshots/_buttons.html.erb @@ -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 %> diff --git a/app/views/snapshots/_form.html.erb b/app/views/snapshots/_form.html.erb index 78afbcb94f..e3fdc66fff 100644 --- a/app/views/snapshots/_form.html.erb +++ b/app/views/snapshots/_form.html.erb @@ -8,3 +8,5 @@ <%= f.label :description -%>
<%= f.text_area :description, rows: 3, :class=>"form-control"-%> + +<%= f.submit(:class => 'btn btn-primary') %> \ No newline at end of file diff --git a/app/views/snapshots/edit.html.erb b/app/views/snapshots/edit.html.erb new file mode 100644 index 0000000000..36d10e5239 --- /dev/null +++ b/app/views/snapshots/edit.html.erb @@ -0,0 +1,7 @@ +

Edit <%= t('snapshot') %>

+ +<%= form_for [@resource, @snapshot] do |f| %> + <%= render :partial => "form", :locals => { :f => f, :action=>:edit } -%> + + or <%= cancel_button polymorphic_path([@resource, @snapshot]) -%> +<% end -%> diff --git a/app/views/snapshots/new.html.erb b/app/views/snapshots/new.html.erb index 7e01af23bf..17674ad440 100644 --- a/app/views/snapshots/new.html.erb +++ b/app/views/snapshots/new.html.erb @@ -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) -%> diff --git a/app/views/snapshots/show.html.erb b/app/views/snapshots/show.html.erb index 901235f9ec..99e0d4235b 100644 --- a/app/views/snapshots/show.html.erb +++ b/app/views/snapshots/show.html.erb @@ -2,7 +2,7 @@ <%= @snapshot.title -%> (snapshot <%= @snapshot.snapshot_number -%>) <% 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'} %>
<%= item_description @snapshot.description -%> diff --git a/config/routes.rb b/config/routes.rb index 8d9cf068e8..25fbaa9caf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/functional/snapshots_controller_test.rb b/test/functional/snapshots_controller_test.rb index 292c99bfd1..11358a5d90 100644 --- a/test/functional/snapshots_controller_test.rb +++ b/test/functional/snapshots_controller_test.rb @@ -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