views: prevent procedure publication on validation error

This commit is contained in:
Pierre de La Morinerie 2021-11-24 10:09:58 +00:00
parent 9db62178d2
commit b86dec8251
3 changed files with 40 additions and 2 deletions

View file

@ -24,6 +24,12 @@ module ProcedureHelper
t(action, scope: [:modal, :publish, key])
end
# Returns a hash of { attribute: full_message } errors.
def procedure_publication_errors(procedure)
procedure.validate(:publication)
procedure.errors.to_hash(full_messages: true).except(:path)
end
def types_de_champ_data(procedure)
{
isAnnotation: false,

View file

@ -1,6 +1,13 @@
.card.mb-4
%h2.card-title Publiez votre démarche
= form_tag admin_procedure_publish_path(procedure_id: procedure.id), method: :put, class: 'form' do
- publication_errors = procedure_publication_errors(procedure)
- if publication_errors.present?
.card.warning
.card-title Des problèmes empêchent la publication de la démarche
- publication_errors.each do |_attribute, full_messages|
%p= full_messages.to_sentence
- if procedure.draft_changed?
%p.mb-4 Publiez une nouvelle version de votre démarche. Les modifications suivantes seront appliquées :
= render partial: 'revision_changes', locals: { changes: procedure.revision_changes }
@ -31,4 +38,4 @@
placeholder: 'https://exemple.gouv.fr/ma_demarche')
.flex.justify-end
= submit_tag procedure_publish_label(procedure, :submit), { class: "button primary", id: 'publish' }
= submit_tag procedure_publish_label(procedure, :submit), { disabled: publication_errors.present?, class: "button primary", id: 'publish' }

View file

@ -31,10 +31,13 @@ describe 'Publishing a procedure', js: true do
end
context 'when a procedure isnt published yet' do
scenario 'an admin can publish it' do
before do
visit admin_procedures_path(statut: "brouillons")
click_on procedure.libelle
find('#publish-procedure-link').click
end
scenario 'an admin can publish it' do
expect(find_field('procedure_path').value).to eq procedure.path
fill_in 'lien_site_web', with: 'http://some.website'
click_on 'Publier'
@ -42,6 +45,28 @@ describe 'Publishing a procedure', js: true do
expect(page).to have_text('Démarche publiée')
expect(page).to have_selector('#preview-procedure')
end
context 'when the procedure has invalid champs' do
let(:empty_repetition) { build(:type_de_champ_repetition, types_de_champ: []) }
let!(:procedure) do
create(:procedure,
:with_path,
:with_service,
instructeurs: instructeurs,
administrateur: administrateur,
types_de_champ: [empty_repetition])
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 bloc répétable « #{empty_repetition.libelle} » doit comporter au moins un champ")
expect(find_field('procedure_path').value).to eq procedure.path
fill_in 'lien_site_web', with: 'http://some.website'
expect(page).to have_button('Publier', disabled: true)
end
end
end
context 'when a procedure is closed' do