From 0e7c390fa95ff1b3cc749ee63679474450bfcf61 Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 17 May 2024 15:54:45 +0200 Subject: [PATCH] feat(TypeDeChampEditor): prevent to destroy a type de champ used by eligibilite rules --- .../champ_component/champ_component.html.haml | 6 +++++- app/models/procedure_revision_type_de_champ.rb | 4 ++++ .../types_de_champ_editor/champ_component_spec.rb | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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 c7ce0590031..0c509c68d2e 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 @@ -10,6 +10,10 @@ %span utilisé pour = link_to('le routage', admin_procedure_groupe_instructeurs_path(revision.procedure_id, anchor: 'routing-rules')) + - elsif coordinate.used_by_eligibilite_rules? + %span + utilisé pour + = link_to('l’eligibilité des dossiers', edit_admin_procedure_eligibilite_rules_path(revision.procedure_id)) - else = button_to type_de_champ_path, class: 'fr-btn fr-btn--tertiary-no-outline fr-icon-delete-line', title: "Supprimer le champ", method: :delete, form: { data: { turbo_confirm: 'Êtes vous sûr de vouloir supprimer ce champ ?' } } do %span.sr-only Supprimer @@ -24,7 +28,7 @@ .flex.justify-start.width-33 .cell.flex.justify-start.column.flex-grow = form.label :type_champ, "Type de champ", for: dom_id(type_de_champ, :type_champ) - = form.select :type_champ, grouped_options_for_select(types_of_type_de_champ, type_de_champ.type_champ), {}, class: 'fr-select small-margin small inline width-100', id: dom_id(type_de_champ, :type_champ), disabled: coordinate.used_by_routing_rules? + = form.select :type_champ, grouped_options_for_select(types_of_type_de_champ, type_de_champ.type_champ), {}, class: 'fr-select small-margin small inline width-100', id: dom_id(type_de_champ, :type_champ), disabled: coordinate.used_by_routing_rules? || coordinate.used_by_eligibilite_rules? .flex.column.justify-start.flex-grow .cell diff --git a/app/models/procedure_revision_type_de_champ.rb b/app/models/procedure_revision_type_de_champ.rb index c4842da20d3..952d68f227a 100644 --- a/app/models/procedure_revision_type_de_champ.rb +++ b/app/models/procedure_revision_type_de_champ.rb @@ -75,4 +75,8 @@ def block def used_by_routing_rules? stable_id.in?(procedure.stable_ids_used_by_routing_rules) end + + def used_by_eligibilite_rules? + revision.eligibilite_enabled? && stable_id.in?(revision.eligibilite_rules&.sources || []) + end end diff --git a/spec/components/types_de_champ_editor/champ_component_spec.rb b/spec/components/types_de_champ_editor/champ_component_spec.rb index 1e368f1ba72..df72d0e82c2 100644 --- a/spec/components/types_de_champ_editor/champ_component_spec.rb +++ b/spec/components/types_de_champ_editor/champ_component_spec.rb @@ -2,10 +2,12 @@ describe 'render' do let(:component) { described_class.new(coordinate:, upper_coordinates: []) } let(:routing_rules_stable_ids) { [] } + let(:eligibilite_rules_used?) { false } before do Flipper.enable_actor(:engagement_juridique_type_de_champ, procedure) allow_any_instance_of(Procedure).to receive(:stable_ids_used_by_routing_rules).and_return(routing_rules_stable_ids) + allow_any_instance_of(ProcedureRevisionTypeDeChamp).to receive(:used_by_eligibilite_rules?).and_return(eligibilite_rules_used?) render_inline(component) end @@ -29,6 +31,15 @@ expect(page).to have_text(/utilisé pour\nle routage/) end end + + context 'drop down tdc used for eligibilite_rules' do + let(:eligibilite_rules_used?) { true } + + it do + expect(page).to have_css("select[disabled=\"disabled\"]") + expect(page).to have_text(/l’eligibilité des dossiers/) + end + end end describe 'tdc ej' do