From db6c86b242a40f15d4b9312f0a1ef6c5cc169b58 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 8 Nov 2018 10:19:52 +0100 Subject: [PATCH] DossierController: user can destroy pjs --- .../new_user/dossiers_controller.rb | 12 +++++- .../purge_champ_piece_justificative.js.erb | 2 + .../_piece_justificative.html.haml | 9 ++-- config/routes.rb | 1 + .../new_user/dossiers_controller_spec.rb | 41 +++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 app/views/new_user/dossiers/purge_champ_piece_justificative.js.erb diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index 9faa079a4..b9f1d3fdd 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -4,11 +4,11 @@ module NewUser SESSION_USER_RETURN_LOCATION = 'user_return_to' ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new] - ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire] + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire, :purge_champ_piece_justificative] before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE before_action :ensure_ownership_or_invitation!, only: ACTIONS_ALLOWED_TO_OWNER_OR_INVITE - before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_brouillon, :modifier, :update] + before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_brouillon, :modifier, :update, :purge_champ_piece_justificative] before_action :forbid_invite_submission!, only: [:update_brouillon] before_action :forbid_closed_submission!, only: [:update_brouillon] before_action :show_demarche_en_test_banner @@ -230,6 +230,14 @@ module NewUser redirect_to url_for dossiers_path end + def purge_champ_piece_justificative + @champ = dossier.champs.find(params[:champ_id]) + + @champ.piece_justificative_file.purge + + flash.notice = 'La pièce jointe a bien été supprimée.' + end + private def store_user_location! diff --git a/app/views/new_user/dossiers/purge_champ_piece_justificative.js.erb b/app/views/new_user/dossiers/purge_champ_piece_justificative.js.erb new file mode 100644 index 000000000..b564293b9 --- /dev/null +++ b/app/views/new_user/dossiers/purge_champ_piece_justificative.js.erb @@ -0,0 +1,2 @@ +<%= render_flash(timeout: 5000, sticky: true) %> +<%= remove_element("#piece_justificative_#{@champ.id}") %> diff --git a/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml b/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml index ce4b05fa3..1785b68d7 100644 --- a/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml +++ b/app/views/shared/dossiers/editable_champs/_piece_justificative.html.haml @@ -10,9 +10,12 @@ id: "champs_#{champ.id}", direct_upload: true - else - = render partial: "shared/champs/piece_justificative/pj_link", locals: { champ: champ, user_can_upload: true } - %br - Modifier : + %div{ id: "piece_justificative_#{champ.id}" } + = render partial: "shared/champs/piece_justificative/pj_link", locals: { champ: champ, user_can_upload: true } + %br + = link_to 'supprimer', purge_champ_piece_justificative_dossier_path(champ_id: champ.id), remote: true, method: :delete + %br + Modifier : = form.file_field :piece_justificative_file, id: "champs_#{champ.id}", direct_upload: true diff --git a/config/routes.rb b/config/routes.rb index 48532b4c2..5d8fbbc9b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -284,6 +284,7 @@ Rails.application.routes.draw do post 'commentaire' => 'dossiers#create_commentaire' post 'ask_deletion' get 'attestation' + delete 'purge_champ_piece_justificative' end collection do diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 27726b35f..d404218fc 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -906,4 +906,45 @@ describe NewUser::DossiersController, type: :controller do end end end + + describe '#purge_champ_piece_justificative' do + before { sign_in(user) } + + subject { delete :purge_champ_piece_justificative, params: { id: champ.dossier.id, champ_id: champ.id }, format: :js } + + context 'when dossier is owned by user' do + let(:dossier){ create(:dossier, user: user) } + let(:champ){ create(:champ_piece_justificative, dossier_id: dossier.id) } + + it { is_expected.to have_http_status(200) } + + it do + subject + expect(champ.reload.piece_justificative_file.attached?).to be(false) + end + + context 'but champ is not linked to this dossier' do + let(:champ){ create(:champ_piece_justificative, dossier: create(:dossier)) } + + it { is_expected.to redirect_to(root_path) } + + it do + subject + expect(champ.reload.piece_justificative_file.attached?).to be(true) + end + end + end + + context 'when dossier is not owned by user' do + let(:dossier){ create(:dossier, user: create(:user)) } + let(:champ){ create(:champ_piece_justificative, dossier_id: dossier.id) } + + it { is_expected.to redirect_to(root_path) } + + it do + subject + expect(champ.reload.piece_justificative_file.attached?).to be(true) + end + end + end end