From a0115767579cb447ba5b4efac25adb61fb4152bc Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 5 Jun 2024 18:08:33 +0200 Subject: [PATCH] feat(procedure_revision.validates): ineligibilite_rules --- app/components/procedure/errors_summary.rb | 2 ++ app/models/procedure.rb | 9 ++++++++- app/models/procedure_revision.rb | 7 +++++++ config/locales/en.yml | 5 +++++ config/locales/fr.yml | 5 +++++ spec/components/procedures/errors_summary_spec.rb | 4 ++++ .../types_de_champ_editor/editor_component_spec.rb | 10 ++++++---- 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/components/procedure/errors_summary.rb b/app/components/procedure/errors_summary.rb index e185c81b37f..bf41ab3da0c 100644 --- a/app/components/procedure/errors_summary.rb +++ b/app/components/procedure/errors_summary.rb @@ -32,6 +32,8 @@ def errors def error_correction_page(error) case error.attribute + when :ineligibilite_rules + edit_admin_procedure_ineligibilite_rules_path(@procedure) when :draft_types_de_champ_public tdc = error.options[:type_de_champ] champs_admin_procedure_path(@procedure, anchor: dom_id(tdc.stable_self, :editor_error)) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 21f2723720a..4e080831c82 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -293,7 +293,7 @@ def revisions_with_pending_dossiers validates_with MonAvisEmbedValidator - validates_associated :draft_revision, on: :publication + validate :validates_associated_draft_revision_with_context validates_associated :initiated_mail, on: :publication validates_associated :received_mail, on: :publication validates_associated :closed_mail, on: :publication @@ -1020,6 +1020,13 @@ def reset_closing_params private + def validates_associated_draft_revision_with_context + return if draft_revision.blank? + return if draft_revision.validate(validation_context) + + draft_revision.errors.map { errors.import(_1) } + end + def validate_auto_archive_on_in_the_future return if auto_archive_on.nil? return if auto_archive_on.future? diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 7e4f3086029..bb7dbb43e14 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -496,6 +496,13 @@ def ineligibilite_rules_are_valid? end end + def ineligibilite_rules_are_valid? + if ineligibilite_rules + ineligibilite_rules.errors(types_de_champ_for(scope: :public).to_a) + .each { errors.add(:ineligibilite_rules, :invalid) } + end + end + def replace_type_de_champ_by_clone(coordinate) cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy| ClonePiecesJustificativesService.clone_attachments(original, kopy) diff --git a/config/locales/en.yml b/config/locales/en.yml index 17b7328d0c0..a05c394b212 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -606,6 +606,7 @@ en: otp_attempt: 'OTP code (only if you have already activated 2FA)' procedure: zone: This procedure is run by + ineligibilite_rules: "Eligibility rules" champs: value: Value default_mail_attributes: &default_mail_attributes @@ -667,6 +668,10 @@ en: path: taken: is already used for procedure. You cannot use it because it belongs to another administrator. invalid: is not valid. It must countain between 3 and 200 characters among a-z, 0-9, '_' and '-'. + procedure_revision: + attributes: + ineligibilite_rules: + invalid: are invalid "dossier/champs": format: "%{message}" attributes: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 227f89a6ef7..624e141d25b 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -610,6 +610,7 @@ fr: otp_attempt: 'Code OTP (uniquement si vous avez déjà activé 2FA)' procedure: zone: La démarche est mise en œuvre par + ineligibilite_rules: "Les règles d’Inéligibilité" champs: value: Valeur du champ default_mail_attributes: &default_mail_attributes @@ -669,6 +670,10 @@ fr: path: taken: est déjà utilisé par une démarche. Vous ne pouvez pas l’utiliser car il appartient à un autre administrateur. invalid: n’est pas valide. Il doit comporter au moins 3 caractères, au plus 200 caractères et seuls les caractères a-z, 0-9, '_' et '-' sont autorisés. + procedure_revision: + attributes: + ineligibilite_rules: + invalid: ne sont pas valides "dossier/champs": format: "%{message}" attributes: diff --git a/spec/components/procedures/errors_summary_spec.rb b/spec/components/procedures/errors_summary_spec.rb index 3e64cc790f9..ebeb1096da3 100644 --- a/spec/components/procedures/errors_summary_spec.rb +++ b/spec/components/procedures/errors_summary_spec.rb @@ -74,6 +74,8 @@ end describe 'render error for other kind of associated objects' do + include Logic + let(:validation_context) { :publication } let(:procedure) { create(:procedure, attestation_template:, initiated_mail:) } let(:attestation_template) { build(:attestation_template) } @@ -81,10 +83,12 @@ before do [:attestation_template, :initiated_mail].map { procedure.send(_1).update_column(:body, '--invalidtag--') } + procedure.draft_revision.update(ineligibilite_enabled: true, ineligibilite_rules: ds_eq(constant(true), constant(1)), ineligibilite_message: 'ko') subject end it 'render error nicely' do + expect(page).to have_selector("a", text: "Les règles d’inéligibilité") expect(page).to have_selector("a", text: "Le modèle d’attestation") expect(page).to have_selector("a", text: "L’email de notification de passage de dossier en instruction") expect(page).to have_text("n'est pas valide", count: 2) diff --git a/spec/components/types_de_champ_editor/editor_component_spec.rb b/spec/components/types_de_champ_editor/editor_component_spec.rb index 7b4a19e4602..5b643995c4a 100644 --- a/spec/components/types_de_champ_editor/editor_component_spec.rb +++ b/spec/components/types_de_champ_editor/editor_component_spec.rb @@ -10,16 +10,18 @@ context 'types_de_champ_public' do let(:is_annotation) { false } it 'does not render private champs errors' do - expect(subject).not_to have_text("« private » doit comporter au moins un choix sélectionnable") - expect(subject).to have_text("« public » doit comporter au moins un choix sélectionnable") + expect(subject).not_to have_text("private") + expect(subject).to have_selector("a", text: "public") + expect(subject).to have_text("doit comporter au moins un choix sélectionnable") end end context 'types_de_champ_private' do let(:is_annotation) { true } it 'does not render public champs errors' do - expect(subject).to have_text("« private » doit comporter au moins un choix sélectionnable") - expect(subject).not_to have_text("« public » doit comporter au moins un choix sélectionnable") + expect(subject).to have_selector("a", "private") + expect(subject).to have_text("doit comporter au moins un choix sélectionnable") + expect(subject).not_to have_text("public") end end end