Skip to content

Commit

Permalink
feat(champ_rna): import error message on champ when rna not found. bu…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
mfo committed Nov 12, 2024
1 parent 783dcd7 commit a0864fb
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
5 changes: 3 additions & 2 deletions app/controllers/champs/rna_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions app/models/champs/rna_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']))
Expand Down
8 changes: 8 additions & 0 deletions config/locales/models/champs/rna_champ/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions config/locales/models/champs/rna_champ/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 0 additions & 9 deletions config/locales/shared.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
5 changes: 0 additions & 5 deletions config/locales/shared.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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é
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/champs/rna_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a0864fb

Please sign in to comment.