diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 895666182..a771d6231 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -20,7 +20,12 @@ module Administrateurs def update type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id]) - if type_de_champ.update(type_de_champ_update_params) + if type_de_champ.revision_type_de_champ.used_by_routing_rules? && changing_of_type?(type_de_champ) + coordinate = draft.coordinate_for(type_de_champ) + errors = "« #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas modifier son type." + @morphed = [champ_component_from(coordinate, focused: false, errors:)] + flash.alert = errors + elsif type_de_champ.update(type_de_champ_update_params) @coordinate = draft.coordinate_for(type_de_champ) @morphed = champ_components_starting_at(@coordinate) @@ -86,6 +91,10 @@ module Administrateurs private + def changing_of_type?(type_de_champ) + type_de_champ_update_params['type_champ'].present? && (type_de_champ_update_params['type_champ'] != type_de_champ.type_champ) + end + def champ_components_starting_at(coordinate, offset = 0) coordinate .siblings_starting_at(offset) @@ -93,11 +102,12 @@ module Administrateurs .map { |c| champ_component_from(c) } end - def champ_component_from(coordinate, focused: false) + def champ_component_from(coordinate, focused: false, errors: '') TypesDeChampEditor::ChampComponent.new( - coordinate: coordinate, + coordinate:, upper_coordinates: coordinate.upper_siblings, - focused: focused + focused: focused, + errors: ) end diff --git a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb index 368804097..4b26f71bb 100644 --- a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb +++ b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb @@ -3,7 +3,7 @@ describe Administrateurs::TypesDeChampController, type: :controller do create(:procedure).tap do |p| p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1') p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l2') - p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l3') + p.draft_revision.add_type_de_champ(type_champ: :drop_down_list, libelle: 'l3') p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'bon dossier', private: true) end end @@ -97,6 +97,21 @@ describe Administrateurs::TypesDeChampController, type: :controller do expect(flash.alert).to eq(["Le champ « Libelle » doit être rempli"]) end end + + context 'rejected if type changed and routing involved' do + let(:params) do + default_params.deep_merge(type_de_champ: { type_champ: 'text', stable_id: third_coordinate.stable_id }) + end + + before do + allow_any_instance_of(ProcedureRevisionTypeDeChamp).to receive(:used_by_routing_rules?).and_return(true) + end + + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to include("utilisé pour le routage") + end + end end # l1, l2, l3 => l1, l3, l2