From f33544f06733131c042a9e5b32c0408a39978f42 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 18 Dec 2020 18:06:59 +0100 Subject: [PATCH] Fix type_de_champ repetition revision after clone fix #5769 --- app/models/procedure.rb | 1 + ...18163035_fix_types_de_champ_revisions.rake | 19 +++++++++++++++++++ spec/models/procedure_spec.rb | 14 +++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/deployment/20201218163035_fix_types_de_champ_revisions.rake diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 794702a1c..4055bb7c9 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -416,6 +416,7 @@ class Procedure < ApplicationRecord procedure.save procedure.draft_types_de_champ.update_all(revision_id: procedure.draft_revision.id) procedure.draft_types_de_champ_private.update_all(revision_id: procedure.draft_revision.id) + TypeDeChamp.where(parent: procedure.draft_types_de_champ.repetition + procedure.draft_types_de_champ_private.repetition).update_all(revision_id: procedure.draft_revision.id) if is_different_admin || from_library procedure.draft_types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } diff --git a/lib/tasks/deployment/20201218163035_fix_types_de_champ_revisions.rake b/lib/tasks/deployment/20201218163035_fix_types_de_champ_revisions.rake new file mode 100644 index 000000000..c4c118ce0 --- /dev/null +++ b/lib/tasks/deployment/20201218163035_fix_types_de_champ_revisions.rake @@ -0,0 +1,19 @@ +namespace :after_party do + desc 'Deployment task: fix_types_de_champ_revisions' + task fix_types_de_champ_revisions: :environment do + puts "Running deploy task 'fix_types_de_champ_revisions'" + + types_de_champ = TypeDeChamp.joins(:parent).where('types_de_champ.revision_id != parents_types_de_champ.revision_id') + progress = ProgressReport.new(types_de_champ.count) + types_de_champ.find_each do |type_de_champ| + type_de_champ.update_column(:revision_id, type_de_champ.parent.revision_id) + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 2136c8c42..7df85ef04 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -325,14 +325,16 @@ describe Procedure do describe 'clone' do let(:service) { create(:service) } - let(:procedure) { create(:procedure, received_mail: received_mail, service: service, types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj], types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2]) } + let(:procedure) { create(:procedure, received_mail: received_mail, service: service, types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj, type_de_champ_repetition], types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2, type_de_champ_private_repetition]) } let(:type_de_champ_0) { build(:type_de_champ, position: 0) } let(:type_de_champ_1) { build(:type_de_champ, position: 1) } let(:type_de_champ_2) { build(:type_de_champ_drop_down_list, position: 2) } let(:type_de_champ_pj) { build(:type_de_champ_piece_justificative, position: 3, old_pj: { stable_id: 2713 }) } + let(:type_de_champ_repetition) { build(:type_de_champ_repetition, position: 4, types_de_champ: [build(:type_de_champ)]) } let(:type_de_champ_private_0) { build(:type_de_champ, :private, position: 0) } let(:type_de_champ_private_1) { build(:type_de_champ, :private, position: 1) } let(:type_de_champ_private_2) { build(:type_de_champ_drop_down_list, :private, position: 2) } + let(:type_de_champ_private_repetition) { build(:type_de_champ_repetition, :private, position: 3, types_de_champ: [build(:type_de_champ, :private)]) } let(:received_mail) { build(:received_mail) } let(:from_library) { false } let(:administrateur) { procedure.administrateurs.first } @@ -378,11 +380,21 @@ describe Procedure do expect(stc.revision).to eq(subject.draft_revision) end + TypeDeChamp.where(parent: procedure.draft_types_de_champ.repetition).zip(TypeDeChamp.where(parent: subject.draft_types_de_champ.repetition)).each do |ptc, stc| + expect(stc).to have_same_attributes_as(ptc, except: ["revision_id", "parent_id"]) + expect(stc.revision).to eq(subject.draft_revision) + end + procedure.draft_types_de_champ_private.zip(subject.draft_types_de_champ_private).each do |ptc, stc| expect(stc).to have_same_attributes_as(ptc, except: ["revision_id"]) expect(stc.revision).to eq(subject.draft_revision) end + TypeDeChamp.where(parent: procedure.draft_types_de_champ_private.repetition).zip(TypeDeChamp.where(parent: subject.draft_types_de_champ_private.repetition)).each do |ptc, stc| + expect(stc).to have_same_attributes_as(ptc, except: ["revision_id", "parent_id"]) + expect(stc.revision).to eq(subject.draft_revision) + end + expect(subject.attestation_template.title).to eq(procedure.attestation_template.title) expect(subject.cloned_from_library).to be(false)