From 3f55fddcef2f390d16f15350d8f3eab7df092d8d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 31 Mar 2023 12:46:28 +0200 Subject: [PATCH] feat(routing): ui side: prevent tdc deletion if used for routing --- .../champ_component/champ_component.html.haml | 17 ++++++++++++----- app/models/procedure.rb | 4 ++++ app/models/procedure_revision_type_de_champ.rb | 4 ++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml index 07b26cca4..be712bf14 100644 --- a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml +++ b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml @@ -1,10 +1,17 @@ %li.type-de-champ.flex.column.justify-start{ html_options } - .flex.justify-start.section.head.hr + .flex.justify-between.section.head.hr .handle.small.icon-only.icon.move-handle{ title: "Déplacer le champ vers le haut ou vers le bas" } - .flex.justify-start.delete - = button_to type_de_champ_path, class: 'button small icon-only danger', method: :delete, form: { data: { turbo_confirm: 'Êtes vous sûr de vouloir supprimer ce champ ?' } } do - .icon.delete - %span.sr-only Supprimer + + - if coordinate.used_by_routing_rules? + .flex.align-center + %span + utilisé pour + = link_to('le routage', admin_procedure_groupe_instructeurs_path(revision.procedure_id, anchor: 'routing-rules')) + - else + .flex.justify-start.delete + = button_to type_de_champ_path, class: 'button small icon-only danger', method: :delete, form: { data: { turbo_confirm: 'Êtes vous sûr de vouloir supprimer ce champ ?' } } do + .icon.delete + %span.sr-only Supprimer - if @errors.present? .types-de-champ-errors diff --git a/app/models/procedure.rb b/app/models/procedure.rb index e0048b19b..23fa64bf0 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -919,4 +919,8 @@ class Procedure < ApplicationRecord groupe_instructeurs.create(label: GroupeInstructeur::DEFAUT_LABEL) end end + + def stable_ids_used_by_routing_rules + @stable_ids_used_by_routing_rules ||= groupe_instructeurs.flat_map { _1.routing_rule&.sources }.compact + end end diff --git a/app/models/procedure_revision_type_de_champ.rb b/app/models/procedure_revision_type_de_champ.rb index 2fd7521b0..90fb598e8 100644 --- a/app/models/procedure_revision_type_de_champ.rb +++ b/app/models/procedure_revision_type_de_champ.rb @@ -69,4 +69,8 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord revision end end + + def used_by_routing_rules? + stable_id.in?(procedure.stable_ids_used_by_routing_rules) + end end