From 528b7ec3f81f935471fb9dcd391af92b5ac3ac3a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 17 Oct 2022 10:05:59 +0200 Subject: [PATCH 1/5] refactor(champ): add required? used for the `required` html attribute. It check visibility to avoid hidden required input which prevent the form from being sent. --- .../address_component/address_component.html.haml | 2 +- .../annuaire_education_component.html.haml | 2 +- .../checkbox_component/checkbox_component.html.haml | 2 +- .../editable_champ/cnaf_component/cnaf_component.html.haml | 4 ++-- .../communes_component/communes_component.html.haml | 2 +- .../editable_champ/date_component/date_component.html.haml | 2 +- .../decimal_number_component.html.haml | 2 +- .../departements_component.html.haml | 2 +- .../dgfip_component/dgfip_component.html.haml | 4 ++-- .../dossier_link_component.html.haml | 2 +- .../email_component/email_component.html.haml | 2 +- .../engagement_component/engagement_component.html.haml | 2 +- .../editable_champ/iban_component/iban_component.html.haml | 2 +- .../integer_number_component.html.haml | 2 +- .../linked_drop_down_list_component.html.haml | 4 ++-- .../mesri_component/mesri_component.html.haml | 2 +- .../number_component/number_component.html.haml | 2 +- .../editable_champ/pays_component/pays_component.html.haml | 2 +- .../phone_component/phone_component.html.haml | 2 +- .../pole_emploi_component/pole_emploi_component.html.haml | 2 +- .../regions_component/regions_component.html.haml | 2 +- .../editable_champ/rna_component/rna_component.html.haml | 2 +- .../siret_component/siret_component.html.haml | 2 +- .../editable_champ/text_component/text_component.html.haml | 2 +- .../textarea_component/textarea_component.html.haml | 2 +- app/models/champ.rb | 7 +++++++ 26 files changed, 35 insertions(+), 28 deletions(-) diff --git a/app/components/editable_champ/address_component/address_component.html.haml b/app/components/editable_champ/address_component/address_component.html.haml index 852775087..da4fa6092 100644 --- a/app/components/editable_champ/address_component/address_component.html.haml +++ b/app/components/editable_champ/address_component/address_component.html.haml @@ -1,6 +1,6 @@ = @form.hidden_field :value = @form.hidden_field :external_id = react_component("ComboAdresseSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, describedby: @champ.describedby_id) diff --git a/app/components/editable_champ/annuaire_education_component/annuaire_education_component.html.haml b/app/components/editable_champ/annuaire_education_component/annuaire_education_component.html.haml index c6090b90f..1a60a1278 100644 --- a/app/components/editable_champ/annuaire_education_component/annuaire_education_component.html.haml +++ b/app/components/editable_champ/annuaire_education_component/annuaire_education_component.html.haml @@ -1,6 +1,6 @@ = @form.hidden_field :value = @form.hidden_field :external_id = react_component("ComboAnnuaireEducationSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, describedby: @champ.describedby_id) diff --git a/app/components/editable_champ/checkbox_component/checkbox_component.html.haml b/app/components/editable_champ/checkbox_component/checkbox_component.html.haml index 68a5f1875..43951421e 100644 --- a/app/components/editable_champ/checkbox_component/checkbox_component.html.haml +++ b/app/components/editable_champ/checkbox_component/checkbox_component.html.haml @@ -1,4 +1,4 @@ = @form.check_box :value, - { required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, + { required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, 'on', 'off' diff --git a/app/components/editable_champ/cnaf_component/cnaf_component.html.haml b/app/components/editable_champ/cnaf_component/cnaf_component.html.haml index 9a3206c6d..77c02b4d2 100644 --- a/app/components/editable_champ/cnaf_component/cnaf_component.html.haml +++ b/app/components/editable_champ/cnaf_component/cnaf_component.html.haml @@ -3,7 +3,7 @@ = @form.label :numero_allocataire, t('.numero_allocataire_label') %p.notice= t('.numero_allocataire_notice') = @form.text_field :numero_allocataire, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" @@ -11,6 +11,6 @@ = @form.label :code_postal, t('.code_postal_label') %p.notice= t('.code_postal_notice') = @form.text_field :code_postal, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" diff --git a/app/components/editable_champ/communes_component/communes_component.html.haml b/app/components/editable_champ/communes_component/communes_component.html.haml index 69b0983c6..ba2f155a0 100644 --- a/app/components/editable_champ/communes_component/communes_component.html.haml +++ b/app/components/editable_champ/communes_component/communes_component.html.haml @@ -3,7 +3,7 @@ = @form.hidden_field :departement = @form.hidden_field :code_departement = react_component("ComboCommunesSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, classNameDepartement: "width-33-desktop width-100-mobile", className: "width-66-desktop width-100-mobile", diff --git a/app/components/editable_champ/date_component/date_component.html.haml b/app/components/editable_champ/date_component/date_component.html.haml index 80bf14516..4e91abc00 100644 --- a/app/components/editable_champ/date_component/date_component.html.haml +++ b/app/components/editable_champ/date_component/date_component.html.haml @@ -2,6 +2,6 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, value: @champ.value, - required: @champ.mandatory?, + required: @champ.required?, placeholder: 'aaaa-mm-jj', class: "width-33-desktop" diff --git a/app/components/editable_champ/decimal_number_component/decimal_number_component.html.haml b/app/components/editable_champ/decimal_number_component/decimal_number_component.html.haml index e43cb36c0..f0cd272a5 100644 --- a/app/components/editable_champ/decimal_number_component/decimal_number_component.html.haml +++ b/app/components/editable_champ/decimal_number_component/decimal_number_component.html.haml @@ -3,4 +3,4 @@ aria: { describedby: @champ.describedby_id }, step: :any, placeholder: "3.14", - required: @champ.mandatory? + required: @champ.required? diff --git a/app/components/editable_champ/departements_component/departements_component.html.haml b/app/components/editable_champ/departements_component/departements_component.html.haml index 8d1d9e051..61b668a7f 100644 --- a/app/components/editable_champ/departements_component/departements_component.html.haml +++ b/app/components/editable_champ/departements_component/departements_component.html.haml @@ -1,7 +1,7 @@ = @form.hidden_field :value = @form.hidden_field :external_id = react_component("ComboDepartementsSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, className: "width-33-desktop width-100-mobile", describedby: @champ.describedby_id) diff --git a/app/components/editable_champ/dgfip_component/dgfip_component.html.haml b/app/components/editable_champ/dgfip_component/dgfip_component.html.haml index ce8d1cc88..c5f5e08de 100644 --- a/app/components/editable_champ/dgfip_component/dgfip_component.html.haml +++ b/app/components/editable_champ/dgfip_component/dgfip_component.html.haml @@ -3,7 +3,7 @@ = @form.label :numero_fiscal, t('.numero_fiscal_label') %p.notice= t('.numero_fiscal_notice') = @form.text_field :numero_fiscal, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" @@ -11,6 +11,6 @@ = @form.label :reference_avis, t('.reference_avis_label') %p.notice= t('.reference_avis_notice') = @form.text_field :reference_avis, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" diff --git a/app/components/editable_champ/dossier_link_component/dossier_link_component.html.haml b/app/components/editable_champ/dossier_link_component/dossier_link_component.html.haml index f38b09921..94c6c9369 100644 --- a/app/components/editable_champ/dossier_link_component/dossier_link_component.html.haml +++ b/app/components/editable_champ/dossier_link_component/dossier_link_component.html.haml @@ -4,7 +4,7 @@ aria: { describedby: @champ.describedby_id }, placeholder: "Numéro de dossier", autocomplete: 'off', - required: @champ.mandatory?, + required: @champ.required?, data: { controller: 'turbo-input', turbo_input_url_value: champs_dossier_link_path(@champ.id) }, class: "width-33-desktop" diff --git a/app/components/editable_champ/email_component/email_component.html.haml b/app/components/editable_champ/email_component/email_component.html.haml index 091ec501b..152253c70 100644 --- a/app/components/editable_champ/email_component/email_component.html.haml +++ b/app/components/editable_champ/email_component/email_component.html.haml @@ -2,4 +2,4 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, placeholder: t(".placeholder"), - required: @champ.mandatory? + required: @champ.required? diff --git a/app/components/editable_champ/engagement_component/engagement_component.html.haml b/app/components/editable_champ/engagement_component/engagement_component.html.haml index 68a5f1875..43951421e 100644 --- a/app/components/editable_champ/engagement_component/engagement_component.html.haml +++ b/app/components/editable_champ/engagement_component/engagement_component.html.haml @@ -1,4 +1,4 @@ = @form.check_box :value, - { required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, + { required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } }, 'on', 'off' diff --git a/app/components/editable_champ/iban_component/iban_component.html.haml b/app/components/editable_champ/iban_component/iban_component.html.haml index dd6cefab6..65e233bb4 100644 --- a/app/components/editable_champ/iban_component/iban_component.html.haml +++ b/app/components/editable_champ/iban_component/iban_component.html.haml @@ -1,7 +1,7 @@ = @form.text_field :value, id: @champ.input_id, placeholder: t(".placeholder"), - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, data: { controller: 'iban-input'}, class: "width-66-desktop", diff --git a/app/components/editable_champ/integer_number_component/integer_number_component.html.haml b/app/components/editable_champ/integer_number_component/integer_number_component.html.haml index 955727631..cb30df24d 100644 --- a/app/components/editable_champ/integer_number_component/integer_number_component.html.haml +++ b/app/components/editable_champ/integer_number_component/integer_number_component.html.haml @@ -2,4 +2,4 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, placeholder: 5, - required: @champ.mandatory? + required: @champ.required? diff --git a/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml b/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml index cac653669..8cb380438 100644 --- a/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml +++ b/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml @@ -2,7 +2,7 @@ = @form.select :primary_value, @champ.primary_options, {}, - { data: { secondary_options: @champ.secondary_options }, required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } } + { data: { secondary_options: @champ.secondary_options }, required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id } } .secondary{ class: @champ.has_secondary_options_for_primary? ? '' : 'hidden' } = @form.label :secondary_value, for: "#{@champ.input_id}-secondary" do @@ -14,5 +14,5 @@ = @form.select :secondary_value, @champ.secondary_options[@champ.primary_value], {}, - { data: { secondary: true }, disabled: !@champ.has_secondary_options_for_primary?, required: @champ.mandatory?, id: "#{@champ.input_id}-secondary", aria: { describedby: "#{@champ.describedby_id}-secondary" } } + { data: { secondary: true }, disabled: !@champ.has_secondary_options_for_primary?, required: @champ.required?, id: "#{@champ.input_id}-secondary", aria: { describedby: "#{@champ.describedby_id}-secondary" } } = @form.hidden_field :secondary_value, value: '', disabled: @champ.has_secondary_options_for_primary? diff --git a/app/components/editable_champ/mesri_component/mesri_component.html.haml b/app/components/editable_champ/mesri_component/mesri_component.html.haml index 9ee80d057..b0ac7ee2e 100644 --- a/app/components/editable_champ/mesri_component/mesri_component.html.haml +++ b/app/components/editable_champ/mesri_component/mesri_component.html.haml @@ -3,7 +3,7 @@ = @form.label :ine, t('.ine_label') %p.notice= t('.ine_notice') = @form.text_field :ine, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" diff --git a/app/components/editable_champ/number_component/number_component.html.haml b/app/components/editable_champ/number_component/number_component.html.haml index 452374c87..8ee884bcc 100644 --- a/app/components/editable_champ/number_component/number_component.html.haml +++ b/app/components/editable_champ/number_component/number_component.html.haml @@ -2,4 +2,4 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, placeholder: @champ.libelle, - required: @champ.mandatory? + required: @champ.required? diff --git a/app/components/editable_champ/pays_component/pays_component.html.haml b/app/components/editable_champ/pays_component/pays_component.html.haml index 1817c3677..d2625b6f3 100644 --- a/app/components/editable_champ/pays_component/pays_component.html.haml +++ b/app/components/editable_champ/pays_component/pays_component.html.haml @@ -1,7 +1,7 @@ = @form.hidden_field :value = @form.hidden_field :external_id = react_component("ComboPaysSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, className: "width-33-desktop width-100-mobile", describedby: @champ.describedby_id) diff --git a/app/components/editable_champ/phone_component/phone_component.html.haml b/app/components/editable_champ/phone_component/phone_component.html.haml index 51bc00fd7..46258c298 100644 --- a/app/components/editable_champ/phone_component/phone_component.html.haml +++ b/app/components/editable_champ/phone_component/phone_component.html.haml @@ -5,5 +5,5 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, placeholder: t(".placeholder"), - required: @champ.mandatory?, + required: @champ.required?, pattern: "[^a-z^A-Z]+" diff --git a/app/components/editable_champ/pole_emploi_component/pole_emploi_component.html.haml b/app/components/editable_champ/pole_emploi_component/pole_emploi_component.html.haml index 9ca65e4cf..20bf0994f 100644 --- a/app/components/editable_champ/pole_emploi_component/pole_emploi_component.html.haml +++ b/app/components/editable_champ/pole_emploi_component/pole_emploi_component.html.haml @@ -3,6 +3,6 @@ = @form.label :identifiant, t('.identifiant_label') %p.notice= t('.identifiant_notice') = @form.text_field :identifiant, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop" diff --git a/app/components/editable_champ/regions_component/regions_component.html.haml b/app/components/editable_champ/regions_component/regions_component.html.haml index 6bc836d80..e9961421a 100644 --- a/app/components/editable_champ/regions_component/regions_component.html.haml +++ b/app/components/editable_champ/regions_component/regions_component.html.haml @@ -1,7 +1,7 @@ = @form.hidden_field :value = @form.hidden_field :external_id = react_component("ComboRegionsSearch", - required: @champ.mandatory?, + required: @champ.required?, id: @champ.input_id, className: "width-33-desktop width-100-mobile", describedby: @champ.describedby_id) diff --git a/app/components/editable_champ/rna_component/rna_component.html.haml b/app/components/editable_champ/rna_component/rna_component.html.haml index 12ee063a4..c16edb4bf 100644 --- a/app/components/editable_champ/rna_component/rna_component.html.haml +++ b/app/components/editable_champ/rna_component/rna_component.html.haml @@ -3,7 +3,7 @@ aria: { describedby: @champ.describedby_id }, placeholder: t(".placeholder"), data: { controller: 'turbo-input', turbo_input_url_value: champs_rna_path(@champ.id) }, - required: @champ.mandatory?, + required: @champ.required?, pattern: "W[0-9]{9}", title: t(".title"), class: "width-33-desktop", diff --git a/app/components/editable_champ/siret_component/siret_component.html.haml b/app/components/editable_champ/siret_component/siret_component.html.haml index 7177950ec..0bcdcdb47 100644 --- a/app/components/editable_champ/siret_component/siret_component.html.haml +++ b/app/components/editable_champ/siret_component/siret_component.html.haml @@ -3,7 +3,7 @@ aria: { describedby: @champ.describedby_id }, placeholder: t(".placeholder"), data: { controller: 'turbo-input', turbo_input_url_value: champs_siret_path(@champ.id) }, - required: @champ.mandatory?, + required: @champ.required?, pattern: "[0-9]{14}", title: t(".title"), class: "width-33-desktop", diff --git a/app/components/editable_champ/text_component/text_component.html.haml b/app/components/editable_champ/text_component/text_component.html.haml index 8d245b564..326166a21 100644 --- a/app/components/editable_champ/text_component/text_component.html.haml +++ b/app/components/editable_champ/text_component/text_component.html.haml @@ -1,4 +1,4 @@ = @form.text_field :value, id: @champ.input_id, - required: @champ.mandatory?, + required: @champ.required?, aria: { describedby: @champ.describedby_id } diff --git a/app/components/editable_champ/textarea_component/textarea_component.html.haml b/app/components/editable_champ/textarea_component/textarea_component.html.haml index 3ea23fdee..4eeb28a1c 100644 --- a/app/components/editable_champ/textarea_component/textarea_component.html.haml +++ b/app/components/editable_champ/textarea_component/textarea_component.html.haml @@ -2,5 +2,5 @@ id: @champ.input_id, aria: { describedby: @champ.describedby_id }, rows: 6, - required: @champ.mandatory?, + required: @champ.required?, value: html_to_string(@champ.value) diff --git a/app/models/champ.rb b/app/models/champ.rb index e5c1a72b1..919582cf0 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -97,6 +97,13 @@ class Champ < ApplicationRecord type_de_champ.mandatory? && visible? end + # used for the `required` html attribute + # check visibility to avoid hidden required input + # which prevent the form from being sent. + def required? + type_de_champ.mandatory? && visible? + end + def mandatory_blank_and_visible? mandatory? && blank? end From 71146232852bef815b5a1e270f883e3ff6229c66 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 17 Oct 2022 10:16:30 +0200 Subject: [PATCH 2/5] fix(champ): rewire champ.mandatory? to type_de_champ.mandatory? it fixes behaviour in drop_down_list_component which add option if not mandatory (!= not required) --- app/models/champ.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 919582cf0..0d8d766a8 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -60,6 +60,7 @@ class Champ < ApplicationRecord :rna?, :siret?, :stable_id, + :mandatory?, to: :type_de_champ scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) } @@ -93,10 +94,6 @@ class Champ < ApplicationRecord @sections ||= dossier.sections_for(self) end - def mandatory? - type_de_champ.mandatory? && visible? - end - # used for the `required` html attribute # check visibility to avoid hidden required input # which prevent the form from being sent. From 6832b12bd4a08f2cde46acae0460f006faa9dac8 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 17 Oct 2022 10:16:52 +0200 Subject: [PATCH 3/5] clean(haml): remove commented code --- .../linked_drop_down_list_component.html.haml | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml b/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml index 8cb380438..04d9962e1 100644 --- a/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml +++ b/app/components/editable_champ/linked_drop_down_list_component/linked_drop_down_list_component.html.haml @@ -7,8 +7,6 @@ .secondary{ class: @champ.has_secondary_options_for_primary? ? '' : 'hidden' } = @form.label :secondary_value, for: "#{@champ.input_id}-secondary" do - sanitize((@champ.drop_down_secondary_libelle.presence || "Valeur secondaire dépendant de la première") + (@champ.type_de_champ.mandatory? ? tag.span(' *', class: 'mandatory') : '')) - -# - if @champ.type_de_champ.mandatory? - -# %span.mandatory * - if @champ.drop_down_secondary_description.present? .notice{ id: "#{@champ.describedby_id}-secondary" }= string_to_html(@champ.drop_down_secondary_description) = @form.select :secondary_value, From 9bba2093be698aa4885acb6c232a67dda5e5e1ee Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 17 Oct 2022 10:38:23 +0200 Subject: [PATCH 4/5] refactor(champ): mandatory_blank_and_visible? -> mandatory_blank? As the method does not check visibility --- app/models/champ.rb | 2 +- app/models/champs/checkbox_champ.rb | 2 +- app/models/champs/piece_justificative_champ.rb | 2 +- app/models/champs/siret_champ.rb | 2 +- app/models/champs/titre_identite_champ.rb | 2 +- app/models/dossier.rb | 2 +- spec/models/champ_shared_example.rb | 14 +++++++------- .../champs/linked_drop_down_list_champ_spec.rb | 10 +++++----- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 0d8d766a8..ac7439d9f 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -101,7 +101,7 @@ class Champ < ApplicationRecord type_de_champ.mandatory? && visible? end - def mandatory_blank_and_visible? + def mandatory_blank? mandatory? && blank? end diff --git a/app/models/champs/checkbox_champ.rb b/app/models/champs/checkbox_champ.rb index 5f52edfd1..c9bbfb584 100644 --- a/app/models/champs/checkbox_champ.rb +++ b/app/models/champs/checkbox_champ.rb @@ -28,7 +28,7 @@ class Champs::CheckboxChamp < Champs::YesNoChamp true? ? 'on' : 'off' end - def mandatory_blank_and_visible? + def mandatory_blank? mandatory? && (blank? || !true?) end end diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 96923f3c2..dfde878e5 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -38,7 +38,7 @@ class Champs::PieceJustificativeChamp < Champ # We don’t know how to search inside documents yet end - def mandatory_blank_and_visible? + def mandatory_blank? mandatory? && !piece_justificative_file.attached? end diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb index d9c9d9852..2b527caf9 100644 --- a/app/models/champs/siret_champ.rb +++ b/app/models/champs/siret_champ.rb @@ -24,7 +24,7 @@ class Champs::SiretChamp < Champ etablissement.present? ? etablissement.search_terms : [value] end - def mandatory_blank_and_visible? + def mandatory_blank? mandatory? && Siret.new(siret: value).invalid? end end diff --git a/app/models/champs/titre_identite_champ.rb b/app/models/champs/titre_identite_champ.rb index a89486112..ae2f2aa9b 100644 --- a/app/models/champs/titre_identite_champ.rb +++ b/app/models/champs/titre_identite_champ.rb @@ -32,7 +32,7 @@ class Champs::TitreIdentiteChamp < Champ # We don’t know how to search inside documents yet end - def mandatory_blank_and_visible? + def mandatory_blank? mandatory? && !piece_justificative_file.attached? end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 487d61bb2..1442e4b41 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -983,7 +983,7 @@ class Dossier < ApplicationRecord def check_mandatory_champs (champs + champs.filter(&:block?).filter(&:visible?).flat_map(&:champs)) - .filter(&:mandatory_blank_and_visible?) + .filter(&:mandatory_blank?) .map do |champ| "Le champ #{champ.libelle.truncate(200)} doit être rempli." end diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index 852767dec..4d9c3d3aa 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -1,26 +1,26 @@ shared_examples 'champ_spec' do - describe 'mandatory_blank_and_visible?' do + describe 'mandatory_blank?' do let(:type_de_champ) { build(:type_de_champ, mandatory: mandatory) } let(:champ) { build(:champ, type_de_champ: type_de_champ, value: value) } let(:value) { '' } let(:mandatory) { true } context 'when mandatory and blank' do - it { expect(champ.mandatory_blank_and_visible?).to be(true) } + it { expect(champ.mandatory_blank?).to be(true) } end context 'when carte mandatory and blank' do let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) } let(:champ) { build(:champ_carte, type_de_champ: type_de_champ, value: value) } let(:value) { nil } - it { expect(champ.mandatory_blank_and_visible?).to be(true) } + it { expect(champ.mandatory_blank?).to be(true) } end context 'when multiple_drop_down_list mandatory and blank' do let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, mandatory: mandatory) } let(:champ) { build(:champ_multiple_drop_down_list, type_de_champ: type_de_champ, value: value) } let(:value) { '[]' } - it { expect(champ.mandatory_blank_and_visible?).to be(true) } + it { expect(champ.mandatory_blank?).to be(true) } end context 'when repetition blank' do @@ -39,18 +39,18 @@ shared_examples 'champ_spec' do context 'when not blank' do let(:value) { 'yop' } - it { expect(champ.mandatory_blank_and_visible?).to be(false) } + it { expect(champ.mandatory_blank?).to be(false) } end context 'when not mandatory' do let(:mandatory) { false } - it { expect(champ.mandatory_blank_and_visible?).to be(false) } + it { expect(champ.mandatory_blank?).to be(false) } end context 'when not mandatory or blank' do let(:value) { 'u' } let(:mandatory) { false } - it { expect(champ.mandatory_blank_and_visible?).to be(false) } + it { expect(champ.mandatory_blank?).to be(false) } end end diff --git a/spec/models/champs/linked_drop_down_list_champ_spec.rb b/spec/models/champs/linked_drop_down_list_champ_spec.rb index d58889cbf..75a668647 100644 --- a/spec/models/champs/linked_drop_down_list_champ_spec.rb +++ b/spec/models/champs/linked_drop_down_list_champ_spec.rb @@ -78,7 +78,7 @@ describe Champs::LinkedDropDownListChamp do let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list_value: value) } it 'blank is fine' do - is_expected.not_to be_mandatory_blank_and_visible + is_expected.not_to be_mandatory_blank end end @@ -86,27 +86,27 @@ describe Champs::LinkedDropDownListChamp do let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: true, drop_down_list_value: value) } context 'when there is no value' do - it { is_expected.to be_mandatory_blank_and_visible } + it { is_expected.to be_mandatory_blank } end context 'when there is a primary value' do before { subject.primary_value = 'Primary' } context 'when there is no secondary value' do - it { is_expected.to be_mandatory_blank_and_visible } + it { is_expected.to be_mandatory_blank } end context 'when there is a secondary value' do before { subject.secondary_value = 'Secondary' } - it { is_expected.not_to be_mandatory_blank_and_visible } + it { is_expected.not_to be_mandatory_blank } end context 'when there is nothing to select for the secondary value' do let(:value) { "--A--\nAbbott\nAbelard\n--B--\n--C--\nCynthia" } before { subject.primary_value = 'B' } - it { is_expected.not_to be_mandatory_blank_and_visible } + it { is_expected.not_to be_mandatory_blank } end end end From fbf089d60e797832ae53fd111560ddf87799528c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 17 Oct 2022 10:41:47 +0200 Subject: [PATCH 5/5] refactor(dossier): rename check_mandatory_champs -> check_mandatory_and_visible_champs --- app/controllers/users/dossiers_controller.rb | 4 ++-- app/models/dossier.rb | 3 ++- spec/models/dossier_spec.rb | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index f4f5aa37e..aac253f80 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -462,7 +462,7 @@ module Users end if dossier.en_construction? - errors += @dossier.check_mandatory_champs + errors += @dossier.check_mandatory_and_visible_champs end errors @@ -473,7 +473,7 @@ module Users @dossier.valid?(**submit_validation_options) errors += @dossier.errors.full_messages - errors += @dossier.check_mandatory_champs + errors += @dossier.check_mandatory_and_visible_champs if should_fill_groupe_instructeur? @dossier.assign_to_groupe_instructeur(defaut_groupe_instructeur) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 1442e4b41..bc97fa9e6 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -981,8 +981,9 @@ class Dossier < ApplicationRecord champs.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later) end - def check_mandatory_champs + def check_mandatory_and_visible_champs (champs + champs.filter(&:block?).filter(&:visible?).flat_map(&:champs)) + .filter(&:visible?) .filter(&:mandatory_blank?) .map do |champ| "Le champ #{champ.libelle.truncate(200)} doit être rempli." diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 6a22ffc67..268540ea5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1172,14 +1172,14 @@ describe Dossier do end end - describe "#check_mandatory_champs" do + describe "#check_mandatory_and_visible_champs" do include Logic let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } let(:dossier) { create(:dossier, procedure: procedure) } let(:types_de_champ) { [type_de_champ] } let(:type_de_champ) { {} } - let(:errors) { dossier.check_mandatory_champs } + let(:errors) { dossier.check_mandatory_and_visible_champs } it 'no mandatory champs' do expect(errors).to be_empty