Skip to content

Commit

Permalink
feat(procedure_revision.validates): ineligibilite_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
mfo committed Jun 6, 2024
1 parent 75e456f commit c799692
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/components/procedure/errors_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 8 additions & 1 deletion app/models/procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
7 changes: 7 additions & 0 deletions app/models/procedure_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,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
Expand Down Expand Up @@ -666,6 +667,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:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,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
Expand Down Expand Up @@ -668,6 +669,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:
Expand Down
4 changes: 4 additions & 0 deletions spec/components/procedures/errors_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,21 @@
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) }
let(:initiated_mail) { build(:initiated_mail) }

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)
Expand Down
10 changes: 6 additions & 4 deletions spec/components/types_de_champ_editor/editor_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c799692

Please sign in to comment.