From 08030bcdb776b38f428f3a6f85b280dc9791bc58 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 11 Feb 2022 08:43:29 +0100 Subject: [PATCH] fix(attestation_template): handle attestation changes on procedures without revisions --- .../attestation_templates_controller.rb | 7 +- app/models/attestation_template.rb | 16 ++-- .../attestation_templates_controller_spec.rb | 75 ++++++++++++++++++- spec/factories/procedure_revision.rb | 1 + 4 files changed, 90 insertions(+), 9 deletions(-) diff --git a/app/controllers/administrateurs/attestation_templates_controller.rb b/app/controllers/administrateurs/attestation_templates_controller.rb index d1f66ebad..939cb8a25 100644 --- a/app/controllers/administrateurs/attestation_templates_controller.rb +++ b/app/controllers/administrateurs/attestation_templates_controller.rb @@ -7,7 +7,7 @@ module Administrateurs end 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) AttestationTemplate @@ -26,6 +26,11 @@ module Administrateurs attestation_template = build_attestation(activated_attestation_params) 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" else flash.alert = attestation_template.errors.full_messages.join('
') diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 668447938..eebfc83f6 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -103,17 +103,19 @@ class AttestationTemplate < ApplicationRecord } end - def revise! - if revisions.size > 1 + def find_or_revise! + 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.save! - revisions - .last - .procedure - .draft_revision - .update!(attestation_template: attestation_template) + procedure.draft_revision.update!(attestation_template: attestation_template) attestation_template 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 end end diff --git a/spec/controllers/administrateurs/attestation_templates_controller_spec.rb b/spec/controllers/administrateurs/attestation_templates_controller_spec.rb index 451bd30f0..5d56b2d39 100644 --- a/spec/controllers/administrateurs/attestation_templates_controller_spec.rb +++ b/spec/controllers/administrateurs/attestation_templates_controller_spec.rb @@ -3,7 +3,7 @@ include ActionDispatch::TestProcess describe Administrateurs::AttestationTemplatesController, type: :controller do let(:admin) { create(:administrateur) } 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(:logo2) { fixture_file_upload('spec/fixtures/files/white.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') } before do + procedure sign_in(admin.user) Timecop.freeze(Time.zone.now) end @@ -142,6 +143,42 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do it { expect(flash.alert).to be_present } it { expect(procedure.draft_attestation_template).to be nil } 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 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(flash.alert).to eq('nop') } 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 diff --git a/spec/factories/procedure_revision.rb b/spec/factories/procedure_revision.rb index 1d7ee0fc3..d2887aa9f 100644 --- a/spec/factories/procedure_revision.rb +++ b/spec/factories/procedure_revision.rb @@ -9,6 +9,7 @@ FactoryBot.define do original = evaluator.from_original revision.procedure = original.procedure + revision.attestation_template_id = original.attestation_template_id original.revision_types_de_champ.each do |r_tdc| revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, from_original: r_tdc) end