From 74219c005af3c0ffd0637c607caed240ebd50fd5 Mon Sep 17 00:00:00 2001 From: benoitqueyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:25:42 +0200 Subject: [PATCH] ajout d'un nettoyage des options des types_de_champ lors de la publication d'une procedure ou d'une nouvelle revision --- app/models/procedure.rb | 8 ++ app/models/type_de_champ.rb | 24 ++++++ spec/models/type_de_champ_spec.rb | 123 ++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 168c486ec9b..8b6bcd16e8d 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -325,6 +325,7 @@ def publish_or_reopen!(administrateur) Procedure.transaction do if brouillon? reset! + cleanup_types_de_champ_options! end other_procedure = other_procedure_with_path(path) @@ -347,6 +348,12 @@ def reset! end end + def cleanup_types_de_champ_options! + draft_revision.types_de_champ.each do |type_de_champ| + type_de_champ.update!(options: type_de_champ.clean_options) + end + end + def suggested_path(administrateur) if path_customized? return path @@ -807,6 +814,7 @@ def average_dossier_weight def publish_revision! reset! + cleanup_types_de_champ_options! transaction do self.published_revision = draft_revision self.draft_revision = create_new_revision diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index c59db6b4819..e1a6431386c 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -685,6 +685,30 @@ def libelle_as_filename .parameterize end + def clean_options + if header_section? + options.slice(:header_section_level.to_s) + elsif explication? + options.slice(:collapsible_explanation_enabled.to_s, :collapsible_explanation_text.to_s) + elsif textarea? + options.slice(:character_limit.to_s) + elsif carte? + options.slice(*TypesDeChamp::CarteTypeDeChamp::LAYERS.map(&:to_s)) + elsif simple_drop_down_list? + options.slice(:drop_down_other.to_s, :drop_down_options.to_s) + elsif multiple_drop_down_list? + options.slice(:drop_down_options.to_s) + elsif linked_drop_down_list? + options.slice(:drop_down_options.to_s, :drop_down_secondary_libelle.to_s, :drop_down_secondary_description.to_s) + elsif piece_justificative? + options.slice(:old_pj.to_s, :skip_pj_validation.to_s, :skip_content_type_pj_validation.to_s) + elsif expression_reguliere? + options.slice(:expression_reguliere.to_s, :expression_reguliere_error_message.to_s, :expression_reguliere_exemple_text.to_s) + else + {} + end + end + class << self def champ_value(type_champ, champ) dynamic_type_class = type_champ_to_class_name(type_champ).constantize diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 5fb9d8f2a95..059fb54b28c 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -290,4 +290,127 @@ def never_valid it { is_expected.to eq("1-tres-interessant-bilan") } end + + describe '#clean_options' do + subject { procedure.published_revision.types_de_champ.first.options } + + let(:procedure) { create(:procedure) } + + context "Header section" do + let(:type_de_champ) { create(:type_de_champ_header_section, procedure:) } + + before do + type_de_champ.update!(options: { 'header_section_level' => '1', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the header_section_level' do + is_expected.to eq({ 'header_section_level' => '1' }) + end + end + + context "Explication" do + let(:type_de_champ) { create(:type_de_champ_explication, procedure:) } + + before do + type_de_champ.update!(options: { 'collapsible_explanation_enabled' => '1', 'collapsible_explanation_text' => 'hello', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the collapsible_explanation keys' do + is_expected.to eq({ 'collapsible_explanation_enabled' => '1', 'collapsible_explanation_text' => 'hello' }) + end + end + + context "Text area" do + let(:type_de_champ) { create(:type_de_champ_textarea, procedure:) } + + before do + type_de_champ.update!(options: { 'character_limit' => '400', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the character limit' do + is_expected.to eq({ 'character_limit' => '400' }) + end + end + + context "Carte" do + let(:type_de_champ) { create(:type_de_champ_carte, procedure:) } + + before do + type_de_champ.update!(options: { 'unesco' => '0', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the layers' do + is_expected.to eq({ 'unesco' => '0' }) + end + end + + context "Simple drop down_list" do + let(:type_de_champ) { create(:type_de_champ_drop_down_list, procedure:) } + + before do + type_de_champ.update!(options: { 'drop_down_other' => '0', 'drop_down_options' => ['', 'Premier choix', 'Deuxième choix'], 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the drop_down_other and drop_down_options' do + is_expected.to eq({ 'drop_down_other' => '0', 'drop_down_options' => ['', 'Premier choix', 'Deuxième choix'] }) + end + end + + context "Multiple drop down_list" do + let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure:) } + + before do + type_de_champ.update!(options: { 'drop_down_options' => ['', 'Premier choix', 'Deuxième choix'], 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the drop_down_options' do + is_expected.to eq({ 'drop_down_options' => ['', 'Premier choix', 'Deuxième choix'] }) + end + end + + context "Linked drop down list" do + let(:type_de_champ) { create(:type_de_champ_linked_drop_down_list, procedure:) } + + before do + type_de_champ.update!(options: { 'drop_down_options' => ['', '--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes'], 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the drop_down_options' do + is_expected.to eq({ 'drop_down_options' => ['', '--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes'] }) + end + end + + context "Piece justificative" do + let(:type_de_champ) { create(:type_de_champ_piece_justificative, procedure:) } + + before do + type_de_champ.update!(options: { 'old_pj' => '123', 'skip_pj_validation' => '1', 'skip_content_type_pj_validation' => '1', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the old_pj, skip_validation_pj and skip_content_type_pj_validation' do + is_expected.to eq({ 'old_pj' => '123', 'skip_pj_validation' => '1', 'skip_content_type_pj_validation' => '1' }) + end + end + + context "Expression reguliere" do + let(:type_de_champ) { create(:type_de_champ_expression_reguliere, procedure:) } + + before do + type_de_champ.update!(options: { 'expression_reguliere' => '\d{9}', 'expression_reguliere_error_message' => 'error', 'expression_reguliere_exemple_text' => '123456789', 'key' => 'value' }) + procedure.publish_revision! + end + + it 'keeping only the expression_reguliere, expression_reguliere_error_message and expression_reguliere_exemple_text' do + is_expected.to eq({ 'expression_reguliere' => '\d{9}', 'expression_reguliere_error_message' => 'error', 'expression_reguliere_exemple_text' => '123456789' }) + end + end + end end