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 } 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/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 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)