From f166077f5e168329f65ba75428a1825e01ba55d7 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Mon, 22 Jul 2019 15:33:58 +0200 Subject: [PATCH 1/2] 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 From af436500fd0bf3249883cfabb3323c59767c28e6 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Tue, 23 Jul 2019 15:24:43 +0200 Subject: [PATCH 2/2] better button --- app/controllers/manager/administrateurs_controller.rb | 2 +- app/views/manager/administrateurs/show.html.erb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/manager/administrateurs_controller.rb b/app/controllers/manager/administrateurs_controller.rb index bfb3cceea..83894c0ab 100644 --- a/app/controllers/manager/administrateurs_controller.rb +++ b/app/controllers/manager/administrateurs_controller.rb @@ -37,7 +37,7 @@ module Manager administrateur = Administrateur.find(params[:id]) if !administrateur.can_be_deleted? - fail "Cannot delete this administrateur because it has dossiers or procedures" + fail "Impossible de supprimer cet administrateur car il a des dossiers ou des procédures" end administrateur.dossiers.each(&:delete_and_keep_track) administrateur.destroy diff --git a/app/views/manager/administrateurs/show.html.erb b/app/views/manager/administrateurs/show.html.erb index 04e85ce4f..34b4b9857 100644 --- a/app/views/manager/administrateurs/show.html.erb +++ b/app/views/manager/administrateurs/show.html.erb @@ -33,12 +33,10 @@ 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 %> + <%= button_to "supprimer", delete_manager_administrateur_path(page.resource), method: :delete, disabled: !page.resource.can_be_deleted?, class: "button", data: { confirm: "Confirmez-vous la suppression de l'administrateur ?" }, title: page.resource.can_be_deleted? ? "Supprimer" : "Cet administrateur a des dossiers ou des procédures et ne peut être supprimé" %>