From a0864fbb3cdba042989f0a3d0be61d232553aabb Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 7 Nov 2024 11:24:56 +0100 Subject: [PATCH] feat(champ_rna): import error message on champ when rna not found. but do not import error when champ is blank because we do not show empty errors on the fly feat(rna): extract error i18n within dedicated file feat(champ_rna): do not use custom i18n empty message when there is already existing blank error message --- app/controllers/champs/rna_controller.rb | 5 +++-- app/models/champs/rna_champ.rb | 4 +--- .../concerns/rna_champ_association_fetchable_concern.rb | 4 ++-- config/locales/models/champs/rna_champ/en.yml | 8 ++++++++ config/locales/models/champs/rna_champ/fr.yml | 8 ++++++++ config/locales/shared.en.yml | 9 --------- config/locales/shared.fr.yml | 5 ----- spec/controllers/champs/rna_controller_spec.rb | 2 +- 8 files changed, 23 insertions(+), 22 deletions(-) diff --git a/app/controllers/champs/rna_controller.rb b/app/controllers/champs/rna_controller.rb index a64bfb7cd6c..56b15e019d7 100644 --- a/app/controllers/champs/rna_controller.rb +++ b/app/controllers/champs/rna_controller.rb @@ -5,8 +5,9 @@ def show champs_attributes = params.dig(:dossier, :champs_public_attributes) || params.dig(:dossier, :champs_private_attributes) rna = champs_attributes.values.first[:value] - unless @champ.fetch_association!(rna) - @error = @champ.association_fetch_error_key + if !@champ.fetch_association!(rna) && @champ.association_fetch_error_key != :blank + err = ActiveModel::Error.new(@champ, :value, @champ.association_fetch_error_key) + @champ.errors.import(err) end end end diff --git a/app/models/champs/rna_champ.rb b/app/models/champs/rna_champ.rb index bdade06ee48..f0cc8af99ef 100644 --- a/app/models/champs/rna_champ.rb +++ b/app/models/champs/rna_champ.rb @@ -3,9 +3,7 @@ class Champs::RNAChamp < Champ include RNAChampAssociationFetchableConcern - validates :value, allow_blank: true, format: { - with: /\AW[0-9A-Z]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages') - }, if: :validate_champ_value_or_prefill? + validates :value, allow_blank: true, format: { with: /\AW[0-9A-Z]{9}\z/, message: :invalid_rna }, if: :validate_champ_value_or_prefill? delegate :id, to: :procedure, prefix: true diff --git a/app/models/concerns/rna_champ_association_fetchable_concern.rb b/app/models/concerns/rna_champ_association_fetchable_concern.rb index b1e95c97ee5..264be38d8ac 100644 --- a/app/models/concerns/rna_champ_association_fetchable_concern.rb +++ b/app/models/concerns/rna_champ_association_fetchable_concern.rb @@ -8,8 +8,8 @@ module RNAChampAssociationFetchableConcern def fetch_association!(rna) self.value = rna - return clear_association!(:empty) if rna.empty? - return clear_association!(:invalid) unless valid_champ_value? + return clear_association!(:blank) if rna.empty? + return clear_association!(:invalid_rna) unless valid_champ_value? return clear_association!(:not_found) if (data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params).blank? update!(data:, value_json: APIGeoService.parse_rna_address(data['adresse'])) diff --git a/config/locales/models/champs/rna_champ/en.yml b/config/locales/models/champs/rna_champ/en.yml index b67542e65b9..abd041e5c05 100644 --- a/config/locales/models/champs/rna_champ/en.yml +++ b/config/locales/models/champs/rna_champ/en.yml @@ -12,6 +12,14 @@ en: paste: Copy the RNA to the clipboard paste_success: The RNA has been copied to the clipboard activerecord: + errors: + models: + champs/rna_champ: + attributes: + value: + invalid_rna: "must begin with a capital W followed by 9 digits or letters" + not_found: No association found + network_error: A network error has prevented the association associated with this RNA to be fetched attributes: champs/rna_champ: hints: diff --git a/config/locales/models/champs/rna_champ/fr.yml b/config/locales/models/champs/rna_champ/fr.yml index 42d3ecfc283..7ad6b1f6542 100644 --- a/config/locales/models/champs/rna_champ/fr.yml +++ b/config/locales/models/champs/rna_champ/fr.yml @@ -12,6 +12,14 @@ fr: paste: Copier le RNA dans le presse-papier paste_success: Le RNA a été copié dans le presse-papier activerecord: + errors: + models: + champs/rna_champ: + attributes: + value: + invalid_rna: "doit commencer par un W majuscule suivi de 9 chiffres ou lettres" + not_found: "Aucun établissement trouvé" + network_error: "Une erreur réseau a empêché l’association liée à ce RNA d’être trouvée" attributes: champs/rna_champ: hints: diff --git a/config/locales/shared.en.yml b/config/locales/shared.en.yml index 8f057684da1..519c3b52a29 100644 --- a/config/locales/shared.en.yml +++ b/config/locales/shared.en.yml @@ -23,15 +23,6 @@ en: fetching_data: "Fetching data for recipient No. %{numero_allocataire} with postal code %{code_postal}." data_fetched: "Data concerning %{sources} linked to the account Nº %{numero_allocataire} with the postal code %{code_postal} has been received from the CAF." data_fetched_title: "Data received from la Caisse nationale d’allocations familiales" - rna: - show: - not_filled: not filled - not_found: "RNA number %{rna} (no association found)" - association: - data_fetched: "This RNA number is linked to: %{title}, %{address}" - not_found: "No association found" - network_error: "A network error has prevented the association associated with this RNA to be fetched" - invalid_number: "The RNA number must begin with a capital W followed by 9 digits or letters" rnf: show: not_found: "RNF %{rnf} (no foundation found)" diff --git a/config/locales/shared.fr.yml b/config/locales/shared.fr.yml index 9683097306f..b1e92fc4306 100644 --- a/config/locales/shared.fr.yml +++ b/config/locales/shared.fr.yml @@ -31,12 +31,7 @@ fr: not_found: "RNA %{rna} (aucun établissement trouvé)" association: data_fetched: "Ce RNA correspond à : %{title}, %{address}" - not_found: "Aucun établissement trouvé" - network_error: "Une erreur réseau a empêché l’association liée à ce RNA d’être trouvée" invalid_number: "Le numéro RNA doit commencer par un W majuscule suivi de 9 chiffres ou lettres" - rnf: - show: - not_found: "RNF %{rnf} (aucune fondation trouvée)" dgfip: show: not_filled: non renseigné diff --git a/spec/controllers/champs/rna_controller_spec.rb b/spec/controllers/champs/rna_controller_spec.rb index 748387e0590..ecd3b89e9a6 100644 --- a/spec/controllers/champs/rna_controller_spec.rb +++ b/spec/controllers/champs/rna_controller_spec.rb @@ -61,7 +61,7 @@ end it 'displays a “RNA is invalid” error message' do - expect(response.body).to include("Le numéro RNA doit commencer par un W majuscule suivi de 9 chiffres ou lettres") + expect(response.body).to include("doit commencer par un W majuscule suivi de 9 chiffres ou lettres") end end