From 8111babdd63474259d837f620e4f1be1254755eb Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 14 Jul 2020 18:25:31 +0200 Subject: [PATCH] =?UTF-8?q?user:=20revoque=20un=20invit=C3=A9=20sur=20un?= =?UTF-8?q?=20dossier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/stylesheets/new_design/common.scss | 4 +++ app/controllers/invites_controller.rb | 16 ++++++++++++ app/views/invites/_form.html.haml | 4 ++- app/views/invites/_redraw.js.erb | 2 ++ app/views/invites/create.js.erb | 3 +-- app/views/invites/destroy.js.erb | 1 + config/routes.rb | 2 +- spec/controllers/invites_controller_spec.rb | 26 +++++++++++++++++++ 8 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 app/views/invites/_redraw.js.erb create mode 100644 app/views/invites/destroy.js.erb diff --git a/app/assets/stylesheets/new_design/common.scss b/app/assets/stylesheets/new_design/common.scss index 171076082..4b6e2f772 100644 --- a/app/assets/stylesheets/new_design/common.scss +++ b/app/assets/stylesheets/new_design/common.scss @@ -58,6 +58,10 @@ sup { font-size: 0.83em; } +small { + font-size: small; +} + .container { @include horizontal-padding($default-padding); max-width: $page-width + 2 * $default-padding; diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 29412c102..a09f0db97 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -54,6 +54,22 @@ class InvitesController < ApplicationController redirect_to dossiers_path end + def destroy + invite = Invite.find(params[:id]) + dossier = invite.dossier + if dossier.user == current_user + invite.destroy! + flash.notice = "L'autorisation de #{invite.email} vient d'être révoquée." + else + flash.alert = "Vous ne pouvez pas révoquer cette autorisation" + end + + respond_to do |format| + format.html { redirect_back(fallback_location: helpers.url_for_dossier(dossier)) } + format.js { @dossier = dossier } + end + end + private def store_user_location! diff --git a/app/views/invites/_form.html.haml b/app/views/invites/_form.html.haml index 5ebaeca70..9c21f300a 100644 --- a/app/views/invites/_form.html.haml +++ b/app/views/invites/_form.html.haml @@ -3,7 +3,9 @@ %h4 Personnes invitées à participer à ce dossier %ul - dossier.invites.each do |invite| - %li= invite.email + %li + = invite.email + %small= link_to("Révoquer l'autorisation", invite_path(invite), data: { confirm: "Souhaitez-vous supprimer l'autorisation ?" }, method: :delete, remote: true) %p Ces personnes peuvent modifier ce dossier. - if dossier.brouillon? %p Une fois le dossier complet, vous devez le déposer vous-même. diff --git a/app/views/invites/_redraw.js.erb b/app/views/invites/_redraw.js.erb new file mode 100644 index 000000000..c73db9f67 --- /dev/null +++ b/app/views/invites/_redraw.js.erb @@ -0,0 +1,2 @@ +<%= render_to_element('#invites-form', partial: 'invites/form', locals: { dossier: @dossier }, outer: true) %> +<%= render_flash %> diff --git a/app/views/invites/create.js.erb b/app/views/invites/create.js.erb index c73db9f67..4d2fd3879 100644 --- a/app/views/invites/create.js.erb +++ b/app/views/invites/create.js.erb @@ -1,2 +1 @@ -<%= render_to_element('#invites-form', partial: 'invites/form', locals: { dossier: @dossier }, outer: true) %> -<%= render_flash %> +<%= render 'redraw' %> diff --git a/app/views/invites/destroy.js.erb b/app/views/invites/destroy.js.erb new file mode 100644 index 000000000..4d2fd3879 --- /dev/null +++ b/app/views/invites/destroy.js.erb @@ -0,0 +1 @@ +<%= render 'redraw' %> diff --git a/config/routes.rb b/config/routes.rb index 71827ae76..4394a6b3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -215,7 +215,7 @@ Rails.application.routes.draw do resources :instructeurs, only: [:index, :create, :destroy] end - resources :invites, only: [:show] do + resources :invites, only: [:show, :destroy] do collection do post 'dossier/:dossier_id', to: 'invites#create', as: :dossier end diff --git a/spec/controllers/invites_controller_spec.rb b/spec/controllers/invites_controller_spec.rb index c26030a8b..ef58fc8c7 100644 --- a/spec/controllers/invites_controller_spec.rb +++ b/spec/controllers/invites_controller_spec.rb @@ -249,4 +249,30 @@ describe InvitesController, type: :controller do end end end + + describe '#DELETE destroy' do + let!(:invite) { create :invite, email: email, dossier: dossier } + let(:signed_in_profile) { dossier.user } + + before do + sign_in signed_in_profile + end + + subject { delete :destroy, params: { id: invite.id } } + + context 'when user is signed in' do + it "destroy invites" do + expect { subject }.to change { Invite.count }.from(1).to(0) + end + end + + context 'when dossier does not belong to user' do + let(:another_user) { create(:user) } + + it 'does not destroy invite' do + sign_in another_user + expect { subject }.not_to change { Invite.count } + end + end + end end