models: validate private types_de_champ

This commit is contained in:
Pierre de La Morinerie 2022-01-26 17:52:54 +01:00
parent e8e37cce15
commit d680602c84
5 changed files with 36 additions and 2 deletions

View file

@ -249,6 +249,10 @@ class Procedure < ApplicationRecord
'revisions/no_empty_repetition': true,
'revisions/no_empty_drop_down': true,
if: :validate_for_publication?
validates :draft_types_de_champ_private,
'revisions/no_empty_repetition': true,
'revisions/no_empty_drop_down': true,
if: :validate_for_publication?
validate :check_juridique
validates :path, presence: true, format: { with: /\A[a-z0-9_\-]{3,200}\z/ }, uniqueness: { scope: [:path, :closed_at, :hidden_at, :unpublished_at], case_sensitive: false }
validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }

View file

@ -171,11 +171,18 @@
%p.button Modifier
- if !@procedure.locked? || @procedure.feature_enabled?(:procedure_revisions)
= link_to annotations_admin_procedure_path(@procedure), class: 'card-admin' do
- @procedure.validate(:publication)
- error_messages = @procedure.errors.messages_for(:draft_types_de_champ_private).to_sentence
= link_to annotations_admin_procedure_path(@procedure), class: 'card-admin', title: error_messages do
- if @procedure.draft_types_de_champ_private.present?
%div
%span.icon.accept
%p.card-admin-status-accept Validé
- elsif error_messages.present?
%div
%span.icon.refuse
%p.card-admin-status-error À modifier
- else
%div
%span.icon.clock

View file

@ -25,3 +25,5 @@ fr:
invalid: 'na pas le bon format'
draft_types_de_champ:
format: 'Le champ %{message}'
draft_types_de_champ_private:
format: 'Lannotation privée %{message}'

View file

@ -352,6 +352,23 @@ describe Procedure do
expect(procedure.errors.full_messages_for(:draft_types_de_champ)).to include(invalid_drop_down_error_message)
end
end
context 'when the champ is private' do
let(:procedure) { create(:procedure, types_de_champ_private: [repetition, drop_down]) }
let(:invalid_repetition_error_message) { 'Lannotation privée « Enfants » doit comporter au moins un champ répétable' }
let(:invalid_drop_down_error_message) { 'Lannotation privée « Civilité » doit comporter au moins un choix sélectionnable' }
it 'validates that no repetition type de champ is empty' do
procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to include(invalid_repetition_error_message)
end
it 'validates that no drop-down type de champ is empty' do
procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message)
end
end
end
end

View file

@ -48,18 +48,22 @@ describe 'Publishing a procedure', js: true do
context 'when the procedure has invalid champs' do
let(:empty_repetition) { build(:type_de_champ_repetition, types_de_champ: [], libelle: 'Enfants') }
let(:empty_drop_down) { build(:type_de_champ_drop_down_list, :without_selectable_values, libelle: 'Civilité') }
let!(:procedure) do
create(:procedure,
:with_path,
:with_service,
instructeurs: instructeurs,
administrateur: administrateur,
types_de_champ: [empty_repetition])
types_de_champ: [empty_repetition],
types_de_champ_private: [empty_drop_down])
end
scenario 'an error message prevents the publication' do
expect(page).to have_content('Des problèmes empêchent la publication de la démarche')
expect(page).to have_content("Le champ « Enfants » doit comporter au moins un champ répétable")
expect(page).to have_content("Lannotation privée « Civilité » doit comporter au moins un choix sélectionnable")
expect(find_field('procedure_path').value).to eq procedure.path
fill_in 'lien_site_web', with: 'http://some.website'