From eb8b9a13cadd48bc842fe6e9df098dc3e03c24f3 Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 6 Sep 2024 11:05:21 +0200 Subject: [PATCH] fix(Champs::DossierLinkChamp): when required, should point to existing dossier --- .../dossier_link_component.html.haml | 8 ++--- app/models/champs/dossier_link_champ.rb | 7 +++++ .../models/champs/dossier_link_champ/en.yml | 7 +++++ .../models/champs/dossier_link_champ/fr.yml | 8 +++++ spec/models/champs/dossier_link_champ_spec.rb | 30 +++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) 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 5389f084d..da3cc176a 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 @@ -1,8 +1,4 @@ = @form.text_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, inputmode: :numeric, min: 1, pattern: "[0-9]{1,12}", autocomplete: 'off', required: @champ.required?, class: "width-33-desktop #{@champ.blank? ? '' : 'small-margin'}")) -- if !@champ.blank? - - if dossier.blank? - .fr-error-text.fr-mb-4w - = t('.not_found') - - else - .fr-info-text.fr-mb-4w= sanitize(dossier.text_summary) +- if !@champ.blank? && !dossier.blank? + .fr-info-text.fr-mb-4w= sanitize(dossier.text_summary) diff --git a/app/models/champs/dossier_link_champ.rb b/app/models/champs/dossier_link_champ.rb index b296d054e..110cb3ce4 100644 --- a/app/models/champs/dossier_link_champ.rb +++ b/app/models/champs/dossier_link_champ.rb @@ -2,9 +2,16 @@ class Champs::DossierLinkChamp < Champ validate :value_integerable, if: -> { value.present? }, on: :prefill + validate :dossier_exists, if: -> { validate_champ_value? && !value.nil? } private + def dossier_exists + if mandatory? && !Dossier.exists?(value) + errors.add(:value, :not_found) + end + end + def value_integerable Integer(value) rescue ArgumentError diff --git a/config/locales/models/champs/dossier_link_champ/en.yml b/config/locales/models/champs/dossier_link_champ/en.yml index c2968da61..a2c75a4f7 100644 --- a/config/locales/models/champs/dossier_link_champ/en.yml +++ b/config/locales/models/champs/dossier_link_champ/en.yml @@ -4,3 +4,10 @@ en: champs/dossier_link_champ: hints: value: "File number" + + errors: + models: + champs/dossier_link_champ: + attributes: + value: + not_found: "File not found" diff --git a/config/locales/models/champs/dossier_link_champ/fr.yml b/config/locales/models/champs/dossier_link_champ/fr.yml index b757afa74..73e7f68c3 100644 --- a/config/locales/models/champs/dossier_link_champ/fr.yml +++ b/config/locales/models/champs/dossier_link_champ/fr.yml @@ -4,3 +4,11 @@ fr: champs/dossier_link_champ: hints: value: "Numéro de dossier" + + + errors: + models: + champs/dossier_link_champ: + attributes: + value: + not_found: "Le dossier n'existe pas" diff --git a/spec/models/champs/dossier_link_champ_spec.rb b/spec/models/champs/dossier_link_champ_spec.rb index 566cdc1a6..411631c39 100644 --- a/spec/models/champs/dossier_link_champ_spec.rb +++ b/spec/models/champs/dossier_link_champ_spec.rb @@ -36,4 +36,34 @@ describe Champs::DossierLinkChamp, type: :model do end end end + + describe 'validation' do + let(:champ) { Champs::DossierLinkChamp.new(value:, dossier: build(:dossier)) } + + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_dossier_link, mandatory:)) + champ.run_callbacks(:validation) + end + + subject { champ.validate(:champs_public_value) } + + context 'when not mandatory' do + let(:mandatory) { false } + let(:value) { nil } + it { is_expected.to be_truthy } + end + + context 'when mandatory' do + let(:mandatory) { true } + context 'when valid id' do + let(:value) { create(:dossier).id } + it { is_expected.to be_truthy } + end + + context 'when invalid id' do + let(:value) { 'kthxbye' } + it { is_expected.to be_falsey } + end + end + end end