From 1fca481c6e4846e4f8532c621e6938ed721ccfc6 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 28 Jul 2020 16:39:32 +0200 Subject: [PATCH 1/3] Cleanup after clone --- app/models/procedure.rb | 3 +++ spec/models/procedure_spec.rb | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index a316e15c3..39852ac14 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -299,6 +299,7 @@ class Procedure < ApplicationRecord procedure.unpublished_at = nil procedure.published_at = nil procedure.lien_notice = nil + procedure.published_revision = nil procedure.draft_revision.procedure = procedure if is_different_admin @@ -326,6 +327,8 @@ class Procedure < ApplicationRecord end procedure.save + procedure.draft_revision.types_de_champ.update_all(revision_id: procedure.draft_revision.id) + procedure.draft_revision.types_de_champ_private.update_all(revision_id: procedure.draft_revision.id) # FIXUP: needed during transition to revisions procedure.draft_revision.types_de_champ.each do |type_de_champ| diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 48fbb5454..c466ed8b8 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -387,14 +387,16 @@ describe Procedure do expect(subject.draft_revision.types_de_champ_private.size).to eq(procedure.draft_revision.types_de_champ_private.size) procedure.types_de_champ.zip(subject.types_de_champ).each do |ptc, stc| - expect(stc).to have_same_attributes_as(ptc) + expect(stc).to have_same_attributes_as(ptc, except: ["revision_id"]) + expect(stc.revision).to eq(subject.draft_revision) end procedure.types_de_champ.zip(procedure.draft_revision.types_de_champ).each do |ptc, rtc| expect(ptc).to eq(rtc) end subject.types_de_champ_private.zip(procedure.types_de_champ_private).each do |stc, ptc| - expect(stc).to have_same_attributes_as(ptc) + expect(stc).to have_same_attributes_as(ptc, except: ["revision_id"]) + expect(stc.revision).to eq(subject.draft_revision) end procedure.types_de_champ_private.zip(procedure.draft_revision.types_de_champ_private).each do |ptc, rtc| expect(ptc).to eq(rtc) From a6eef6fb19a09a23b4c173c7f01e2316d2170410 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 28 Jul 2020 16:43:17 +0200 Subject: [PATCH 2/3] Expose draft_revision types_de_champ to editor --- app/helpers/procedure_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/procedure_helper.rb b/app/helpers/procedure_helper.rb index 8d8362bc8..93ac0dc0f 100644 --- a/app/helpers/procedure_helper.rb +++ b/app/helpers/procedure_helper.rb @@ -22,7 +22,7 @@ module ProcedureHelper { isAnnotation: false, typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user), - typeDeChamps: procedure.types_de_champ.as_json_for_editor, + typeDeChamps: (procedure.draft_revision ? procedure.draft_revision : procedure).types_de_champ.as_json_for_editor, baseUrl: admin_procedure_types_de_champ_path(procedure), directUploadUrl: rails_direct_uploads_url } @@ -32,7 +32,7 @@ module ProcedureHelper { isAnnotation: true, typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user), - typeDeChamps: procedure.types_de_champ_private.as_json_for_editor, + typeDeChamps: (procedure.draft_revision ? procedure.draft_revision : procedure).types_de_champ_private.as_json_for_editor, baseUrl: admin_procedure_types_de_champ_path(procedure), directUploadUrl: rails_direct_uploads_url } From 6709b9ed20fb60bc74a7334df7cbbf5dfb268646 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 28 Jul 2020 17:10:12 +0200 Subject: [PATCH 3/3] Fix cloned revisions task --- .../20200728150458_fix_cloned_revisions.rake | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/tasks/deployment/20200728150458_fix_cloned_revisions.rake diff --git a/lib/tasks/deployment/20200728150458_fix_cloned_revisions.rake b/lib/tasks/deployment/20200728150458_fix_cloned_revisions.rake new file mode 100644 index 000000000..abfb8b23a --- /dev/null +++ b/lib/tasks/deployment/20200728150458_fix_cloned_revisions.rake @@ -0,0 +1,25 @@ +namespace :after_party do + desc 'Deployment task: fix_cloned_revisions' + task fix_cloned_revisions: :environment do + puts "Running deploy task 'fix_cloned_revisions'" + + Procedure.with_discarded.where(aasm_state: :brouillon).where.not(published_revision_id: nil).update_all(published_revision_id: nil) + + types_de_champ = TypeDeChamp.joins(:revision).where('types_de_champ.procedure_id != procedure_revisions.procedure_id') + progress = ProgressReport.new(types_de_champ.count) + + types_de_champ.find_each do |type_de_champ| + procedure = type_de_champ.procedure ? type_de_champ.procedure : Procedure.with_discarded.find(type_de_champ.procedure_id) + revision_id = procedure.published_revision_id || procedure.draft_revision_id + type_de_champ.update_column(:revision_id, 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