fix(attestation_template): handle attestation changes on procedures without revisions

This commit is contained in:
Paul Chavard 2022-02-11 08:43:29 +01:00
parent 76b1b85fa7
commit 08030bcdb7
4 changed files with 90 additions and 9 deletions

View file

@ -7,7 +7,7 @@ module Administrateurs
end end
def update def update
attestation_template = @procedure.draft_attestation_template.revise! attestation_template = @procedure.draft_attestation_template.find_or_revise!
if attestation_template.update(activated_attestation_params) if attestation_template.update(activated_attestation_params)
AttestationTemplate AttestationTemplate
@ -26,6 +26,11 @@ module Administrateurs
attestation_template = build_attestation(activated_attestation_params) attestation_template = build_attestation(activated_attestation_params)
if attestation_template.save if attestation_template.save
if @procedure.publiee? && !@procedure.feature_enabled?(:procedure_revisions)
# If revisions support is not enabled and procedure is published
# attach the same attestation template to published revision.
@procedure.published_revision.update(attestation_template: attestation_template)
end
flash.notice = "L'attestation a bien été sauvegardée" flash.notice = "L'attestation a bien été sauvegardée"
else else
flash.alert = attestation_template.errors.full_messages.join('<br>') flash.alert = attestation_template.errors.full_messages.join('<br>')

View file

@ -103,17 +103,19 @@ class AttestationTemplate < ApplicationRecord
} }
end end
def revise! def find_or_revise!
if revisions.size > 1 if revisions.size > 1 && procedure.feature_enabled?(:procedure_revisions)
# If attestation template belongs to more then one revision
# and procedure has revisions enabled revise attestation template.
attestation_template = dup attestation_template = dup
attestation_template.save! attestation_template.save!
revisions procedure.draft_revision.update!(attestation_template: attestation_template)
.last
.procedure
.draft_revision
.update!(attestation_template: attestation_template)
attestation_template attestation_template
else else
# If procedure has only one revision or revisions are not supported
# apply updates directly to the attestation template.
# If it is a published procedure with revisions disabled,
# draft and published attestation template will be the same.
self self
end end
end end

View file

@ -3,7 +3,7 @@ include ActionDispatch::TestProcess
describe Administrateurs::AttestationTemplatesController, type: :controller do describe Administrateurs::AttestationTemplatesController, type: :controller do
let(:admin) { create(:administrateur) } let(:admin) { create(:administrateur) }
let(:attestation_template) { build(:attestation_template) } let(:attestation_template) { build(:attestation_template) }
let!(:procedure) { create :procedure, administrateur: admin, attestation_template: attestation_template } let(:procedure) { create :procedure, administrateur: admin, attestation_template: attestation_template }
let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') } let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') }
let(:logo2) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') } let(:logo2) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') }
let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') } let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') }
@ -13,6 +13,7 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do
let(:invalid_logo) { fixture_file_upload('spec/fixtures/files/invalid_file_format.json', 'application/json') } let(:invalid_logo) { fixture_file_upload('spec/fixtures/files/invalid_file_format.json', 'application/json') }
before do before do
procedure
sign_in(admin.user) sign_in(admin.user)
Timecop.freeze(Time.zone.now) Timecop.freeze(Time.zone.now)
end end
@ -142,6 +143,42 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do
it { expect(flash.alert).to be_present } it { expect(flash.alert).to be_present }
it { expect(procedure.draft_attestation_template).to be nil } it { expect(procedure.draft_attestation_template).to be nil }
end end
context 'when procedure is published' do
let(:procedure) { create(:procedure, :published, administrateur: admin, attestation_template: attestation_template) }
let(:attestation_template) { nil }
let(:attestation_params) { { title: 't', body: 'b', footer: '', activated: true } }
let(:revisions_enabled) { false }
before do
if revisions_enabled
Flipper.enable(:procedure_revisions, procedure)
end
post :create,
params: {
procedure_id: procedure.id,
attestation_template: attestation_params
}
procedure.reload
end
context 'and revisions are not activated' do
it do
expect(procedure.draft_attestation_template).to eq(procedure.published_attestation_template)
expect(procedure.draft_attestation_template.title).to eq('t')
end
end
context 'and revisions are activated' do
let(:revisions_enabled) { true }
it do
expect(procedure.draft_attestation_template).not_to eq(procedure.published_attestation_template)
expect(procedure.draft_attestation_template.title).to eq('t')
expect(procedure.published_attestation_template).to be_nil
end
end
end
end end
describe 'PATCH #update' do describe 'PATCH #update' do
@ -184,5 +221,41 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
it { expect(flash.alert).to eq('nop') } it { expect(flash.alert).to eq('nop') }
end end
context 'when procedure is published' do
let(:procedure) { create(:procedure, :published, administrateur: admin, attestation_template: attestation_template) }
let(:attestation_template) { create(:attestation_template, title: 'a') }
let(:attestation_params) { { title: 't', body: 'b', footer: '', activated: true } }
let(:revisions_enabled) { false }
before do
if revisions_enabled
Flipper.enable(:procedure_revisions, procedure)
end
patch :update,
params: {
procedure_id: procedure.id,
attestation_template: attestation_params
}
procedure.reload
end
context 'and revisions are not activated' do
it do
expect(procedure.draft_attestation_template).to eq(procedure.published_attestation_template)
expect(procedure.draft_attestation_template.title).to eq('t')
end
end
context 'and revisions are activated' do
let(:revisions_enabled) { true }
it do
expect(procedure.draft_attestation_template).not_to eq(procedure.published_attestation_template)
expect(procedure.draft_attestation_template.title).to eq('t')
expect(procedure.published_attestation_template.title).to eq('a')
end
end
end
end end
end end

View file

@ -9,6 +9,7 @@ FactoryBot.define do
original = evaluator.from_original original = evaluator.from_original
revision.procedure = original.procedure revision.procedure = original.procedure
revision.attestation_template_id = original.attestation_template_id
original.revision_types_de_champ.each do |r_tdc| original.revision_types_de_champ.each do |r_tdc|
revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, from_original: r_tdc) revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, from_original: r_tdc)
end end