Migrate procedures “on demand”

This commit is contained in:
Paul Chavard 2020-06-26 12:00:21 +02:00 committed by Paul Chavard
parent 0a0912503b
commit d18a9c8162
5 changed files with 79 additions and 23 deletions

View file

@ -33,6 +33,8 @@ module NewAdministrateur
else else
flash.notice = 'Démarche enregistrée.' flash.notice = 'Démarche enregistrée.'
current_administrateur.instructeur.assign_to_procedure(@procedure) current_administrateur.instructeur.assign_to_procedure(@procedure)
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(@procedure)
redirect_to champs_admin_procedure_path(@procedure) redirect_to champs_admin_procedure_path(@procedure)
end end

View file

@ -2,11 +2,12 @@ module NewAdministrateur
class TypesDeChampController < AdministrateurController class TypesDeChampController < AdministrateurController
before_action :retrieve_procedure, only: [:create, :update, :move, :destroy] before_action :retrieve_procedure, only: [:create, :update, :move, :destroy]
before_action :procedure_locked?, only: [:create, :update, :move, :destroy] before_action :procedure_locked?, only: [:create, :update, :move, :destroy]
before_action :revisions_migration
def create def create
type_de_champ = TypeDeChamp.new(type_de_champ_create_params) type_de_champ = @procedure.draft_revision.add_type_de_champ(type_de_champ_create_params)
if type_de_champ.save if type_de_champ.valid?
reset_procedure reset_procedure
render json: serialize_type_de_champ(type_de_champ), status: :created render json: serialize_type_de_champ(type_de_champ), status: :created
else else
@ -15,7 +16,7 @@ module NewAdministrateur
end end
def update def update
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id]) type_de_champ = @procedure.draft_revision.find_or_clone_type_de_champ(type_de_champ_stable_id)
if type_de_champ.update(type_de_champ_update_params) if type_de_champ.update(type_de_champ_update_params)
reset_procedure reset_procedure
@ -26,18 +27,13 @@ module NewAdministrateur
end end
def move def move
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id]) @procedure.draft_revision.move_type_de_champ(type_de_champ_stable_id, params[:position].to_i)
new_index = params[:order_place].to_i
@procedure.move_type_de_champ(type_de_champ, new_index)
head :no_content head :no_content
end end
def destroy def destroy
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id]) @procedure.draft_revision.remove_type_de_champ(type_de_champ_stable_id)
type_de_champ.destroy!
reset_procedure reset_procedure
head :no_content head :no_content
@ -45,6 +41,15 @@ module NewAdministrateur
private private
def type_de_champ_stable_id
TypeDeChamp.find(params[:id]).stable_id
end
def revisions_migration
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(@procedure)
end
def serialize_type_de_champ(type_de_champ) def serialize_type_de_champ(type_de_champ)
{ {
type_de_champ: type_de_champ.as_json( type_de_champ: type_de_champ.as_json(
@ -55,6 +60,7 @@ module NewAdministrateur
:parent_id, :parent_id,
:private, :private,
:procedure_id, :procedure_id,
:revision_id,
:stable_id, :stable_id,
:type, :type,
:updated_at :updated_at
@ -72,7 +78,7 @@ module NewAdministrateur
end end
def type_de_champ_create_params def type_de_champ_create_params
params.required(:type_de_champ).permit(:cadastres, type_de_champ_params = params.required(:type_de_champ).permit(:cadastres,
:description, :description,
:drop_down_list_value, :drop_down_list_value,
:libelle, :libelle,
@ -83,7 +89,13 @@ module NewAdministrateur
:piece_justificative_template, :piece_justificative_template,
:private, :private,
:quartiers_prioritaires, :quartiers_prioritaires,
:type_champ).merge(procedure: @procedure) :type_champ)
if type_de_champ_params[:parent_id].present?
type_de_champ_params[:parent_id] = TypeDeChamp.find(type_de_champ_params[:parent_id]).stable_id
end
type_de_champ_params
end end
def type_de_champ_update_params def type_de_champ_update_params

View file

@ -258,7 +258,10 @@ module Users
return redirect_to url_for dossiers_path return redirect_to url_for dossiers_path
end end
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(procedure)
dossier = Dossier.new( dossier = Dossier.new(
revision: procedure.active_revision,
groupe_instructeur: procedure.defaut_groupe_instructeur, groupe_instructeur: procedure.defaut_groupe_instructeur,
user: current_user, user: current_user,
state: Dossier.states.fetch(:brouillon) state: Dossier.states.fetch(:brouillon)

View file

@ -246,6 +246,7 @@ class Procedure < ApplicationRecord
def new_dossier def new_dossier
Dossier.new( Dossier.new(
procedure: self, procedure: self,
revision: active_revision,
champs: build_champs, champs: build_champs,
champs_private: build_champs_private, champs_private: build_champs_private,
groupe_instructeur: defaut_groupe_instructeur groupe_instructeur: defaut_groupe_instructeur
@ -296,13 +297,22 @@ class Procedure < ApplicationRecord
end end
def clone(admin, from_library) def clone(admin, from_library)
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(self)
is_different_admin = !admin.owns?(self) is_different_admin = !admin.owns?(self)
populate_champ_stable_ids populate_champ_stable_ids
include_list = { include_list = {
attestation_template: nil, attestation_template: [],
types_de_champ: [:types_de_champ], draft_revision: {
types_de_champ_private: [:types_de_champ] revision_types_de_champ: {
type_de_champ: :types_de_champ
},
revision_types_de_champ_private: {
type_de_champ: :types_de_champ
}
}
} }
include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin
procedure = self.deep_clone(include: include_list, &method(:clone_attachments)) procedure = self.deep_clone(include: include_list, &method(:clone_attachments))
@ -312,10 +322,7 @@ class Procedure < ApplicationRecord
procedure.unpublished_at = nil procedure.unpublished_at = nil
procedure.published_at = nil procedure.published_at = nil
procedure.lien_notice = nil procedure.lien_notice = nil
procedure.draft_revision.procedure = procedure
if is_different_admin || from_library
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
end
if is_different_admin if is_different_admin
procedure.administrateurs = [admin] procedure.administrateurs = [admin]
@ -343,6 +350,18 @@ class Procedure < ApplicationRecord
procedure.save procedure.save
# FIXUP: needed during transition to revisions
procedure.draft_revision.types_de_champ.each do |type_de_champ|
procedure.types_de_champ << type_de_champ
end
procedure.draft_revision.types_de_champ_private.each do |type_de_champ|
procedure.types_de_champ_private << type_de_champ
end
if is_different_admin || from_library
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
end
procedure procedure
end end
@ -582,6 +601,10 @@ class Procedure < ApplicationRecord
ApiEntrepriseToken.new(api_entreprise_token).expired? ApiEntrepriseToken.new(api_entreprise_token).expired?
end end
def create_new_revision
draft_revision.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private])
end
private private
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index) def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)
@ -605,15 +628,23 @@ class Procedure < ApplicationRecord
end end
def after_publish(canonical_procedure = nil) def after_publish(canonical_procedure = nil)
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure) # FIXUP: needed during transition to revisions
if RevisionsMigration.add_revisions(self)
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure)
else
update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure, draft_revision: create_new_revision, published_revision: draft_revision)
end
end end
def after_close def after_close
now = Time.zone.now # FIXUP: needed during transition to revisions
update!(closed_at: now) RevisionsMigration.add_revisions(self)
update!(closed_at: Time.zone.now)
end end
def after_unpublish def after_unpublish
# FIXUP: needed during transition to revisions
RevisionsMigration.add_revisions(self)
update!(unpublished_at: Time.zone.now) update!(unpublished_at: Time.zone.now)
end end

View file

@ -417,14 +417,22 @@ describe Procedure do
expect(subject.types_de_champ_private.size).to eq procedure.types_de_champ_private.size expect(subject.types_de_champ_private.size).to eq procedure.types_de_champ_private.size
expect(subject.types_de_champ.map(&:drop_down_options).compact.size).to eq procedure.types_de_champ.map(&:drop_down_options).compact.size expect(subject.types_de_champ.map(&:drop_down_options).compact.size).to eq procedure.types_de_champ.map(&:drop_down_options).compact.size
expect(subject.types_de_champ_private.map(&:drop_down_options).compact.size).to eq procedure.types_de_champ_private.map(&:drop_down_options).compact.size expect(subject.types_de_champ_private.map(&:drop_down_options).compact.size).to eq procedure.types_de_champ_private.map(&:drop_down_options).compact.size
expect(subject.draft_revision.types_de_champ.size).to eq(procedure.draft_revision.types_de_champ.size)
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| 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)
end 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| 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)
end end
procedure.types_de_champ_private.zip(procedure.draft_revision.types_de_champ_private).each do |ptc, rtc|
expect(ptc).to eq(rtc)
end
expect(subject.attestation_template.title).to eq(procedure.attestation_template.title) expect(subject.attestation_template.title).to eq(procedure.attestation_template.title)
@ -432,7 +440,7 @@ describe Procedure do
cloned_procedure = subject cloned_procedure = subject
cloned_procedure.parent_procedure_id = nil cloned_procedure.parent_procedure_id = nil
expect(cloned_procedure).to have_same_attributes_as(procedure, except: ["path"]) expect(cloned_procedure).to have_same_attributes_as(procedure, except: ["path", "draft_revision_id"])
end end
context 'when the procedure is cloned from the library' do context 'when the procedure is cloned from the library' do