From f166077f5e168329f65ba75428a1825e01ba55d7 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Mon, 22 Jul 2019 15:33:58 +0200 Subject: [PATCH] ajout d'un bouton de suppression des admin dans le manager --- .../manager/administrateurs_controller.rb | 15 +++++++++++++++ app/models/administrateur.rb | 4 ++++ app/views/manager/administrateurs/show.html.erb | 3 +++ config/routes.rb | 1 + 4 files changed, 23 insertions(+) diff --git a/app/controllers/manager/administrateurs_controller.rb b/app/controllers/manager/administrateurs_controller.rb index 91866045c..bfb3cceea 100644 --- a/app/controllers/manager/administrateurs_controller.rb +++ b/app/controllers/manager/administrateurs_controller.rb @@ -33,6 +33,21 @@ module Manager head :ok end + def delete + administrateur = Administrateur.find(params[:id]) + + if !administrateur.can_be_deleted? + fail "Cannot delete this administrateur because it has dossiers or procedures" + end + administrateur.dossiers.each(&:delete_and_keep_track) + administrateur.destroy + + logger.info("L'administrateur #{administrateur.id} est supprimé par #{current_user.id}") + flash[:notice] = "L'administrateur #{administrateur.id} est supprimé" + + redirect_to manager_administrateurs_path + end + private def create_administrateur_params diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 6d1de0927..87c4ed221 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -123,4 +123,8 @@ class Administrateur < ApplicationRecord def gestionnaire Gestionnaire.find_by(email: email) end + + def can_be_deleted? + dossiers.state_instruction_commencee.none? && procedures.none? + end end diff --git a/app/views/manager/administrateurs/show.html.erb b/app/views/manager/administrateurs/show.html.erb index 8d152a733..04e85ce4f 100644 --- a/app/views/manager/administrateurs/show.html.erb +++ b/app/views/manager/administrateurs/show.html.erb @@ -33,6 +33,9 @@ as well as a link to its edit page.
+ <% if page.resource.can_be_deleted? %> + <%= link_to "supprimer", delete_manager_administrateur_path(page.resource), method: :delete, class: "button", data: { confirm: "Confirmez-vous la suppression de l'administrateur ?" } %> + <% end %> <% if page.resource.invitation_expired? %> <%= link_to "renvoyer l'invitation", reinvite_manager_administrateur_path(page.resource), method: :post, class: "button" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index e77e6eb5f..fc9169607 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Rails.application.routes.draw do resources :administrateurs, only: [:index, :show, :new, :create] do post 'reinvite', on: :member put 'enable_feature', on: :member + delete 'delete', on: :member end resources :users, only: [:index, :show] do