Migrate procedures “on demand”
This commit is contained in:
parent
0a0912503b
commit
d18a9c8162
5 changed files with 79 additions and 23 deletions
|
@ -33,6 +33,8 @@ module NewAdministrateur
|
|||
else
|
||||
flash.notice = 'Démarche enregistrée.'
|
||||
current_administrateur.instructeur.assign_to_procedure(@procedure)
|
||||
# FIXUP: needed during transition to revisions
|
||||
RevisionsMigration.add_revisions(@procedure)
|
||||
|
||||
redirect_to champs_admin_procedure_path(@procedure)
|
||||
end
|
||||
|
|
|
@ -2,11 +2,12 @@ module NewAdministrateur
|
|||
class TypesDeChampController < AdministrateurController
|
||||
before_action :retrieve_procedure, only: [:create, :update, :move, :destroy]
|
||||
before_action :procedure_locked?, only: [:create, :update, :move, :destroy]
|
||||
before_action :revisions_migration
|
||||
|
||||
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
|
||||
render json: serialize_type_de_champ(type_de_champ), status: :created
|
||||
else
|
||||
|
@ -15,7 +16,7 @@ module NewAdministrateur
|
|||
end
|
||||
|
||||
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)
|
||||
reset_procedure
|
||||
|
@ -26,18 +27,13 @@ module NewAdministrateur
|
|||
end
|
||||
|
||||
def move
|
||||
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id])
|
||||
new_index = params[:order_place].to_i
|
||||
|
||||
@procedure.move_type_de_champ(type_de_champ, new_index)
|
||||
@procedure.draft_revision.move_type_de_champ(type_de_champ_stable_id, params[:position].to_i)
|
||||
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def destroy
|
||||
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id])
|
||||
|
||||
type_de_champ.destroy!
|
||||
@procedure.draft_revision.remove_type_de_champ(type_de_champ_stable_id)
|
||||
reset_procedure
|
||||
|
||||
head :no_content
|
||||
|
@ -45,6 +41,15 @@ module NewAdministrateur
|
|||
|
||||
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)
|
||||
{
|
||||
type_de_champ: type_de_champ.as_json(
|
||||
|
@ -55,6 +60,7 @@ module NewAdministrateur
|
|||
:parent_id,
|
||||
:private,
|
||||
:procedure_id,
|
||||
:revision_id,
|
||||
:stable_id,
|
||||
:type,
|
||||
:updated_at
|
||||
|
@ -72,7 +78,7 @@ module NewAdministrateur
|
|||
end
|
||||
|
||||
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,
|
||||
:drop_down_list_value,
|
||||
:libelle,
|
||||
|
@ -83,7 +89,13 @@ module NewAdministrateur
|
|||
:piece_justificative_template,
|
||||
:private,
|
||||
: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
|
||||
|
||||
def type_de_champ_update_params
|
||||
|
|
|
@ -258,7 +258,10 @@ module Users
|
|||
return redirect_to url_for dossiers_path
|
||||
end
|
||||
|
||||
# FIXUP: needed during transition to revisions
|
||||
RevisionsMigration.add_revisions(procedure)
|
||||
dossier = Dossier.new(
|
||||
revision: procedure.active_revision,
|
||||
groupe_instructeur: procedure.defaut_groupe_instructeur,
|
||||
user: current_user,
|
||||
state: Dossier.states.fetch(:brouillon)
|
||||
|
|
|
@ -246,6 +246,7 @@ class Procedure < ApplicationRecord
|
|||
def new_dossier
|
||||
Dossier.new(
|
||||
procedure: self,
|
||||
revision: active_revision,
|
||||
champs: build_champs,
|
||||
champs_private: build_champs_private,
|
||||
groupe_instructeur: defaut_groupe_instructeur
|
||||
|
@ -296,13 +297,22 @@ class Procedure < ApplicationRecord
|
|||
end
|
||||
|
||||
def clone(admin, from_library)
|
||||
# FIXUP: needed during transition to revisions
|
||||
RevisionsMigration.add_revisions(self)
|
||||
|
||||
is_different_admin = !admin.owns?(self)
|
||||
|
||||
populate_champ_stable_ids
|
||||
include_list = {
|
||||
attestation_template: nil,
|
||||
types_de_champ: [:types_de_champ],
|
||||
types_de_champ_private: [:types_de_champ]
|
||||
attestation_template: [],
|
||||
draft_revision: {
|
||||
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
|
||||
procedure = self.deep_clone(include: include_list, &method(:clone_attachments))
|
||||
|
@ -312,10 +322,7 @@ class Procedure < ApplicationRecord
|
|||
procedure.unpublished_at = nil
|
||||
procedure.published_at = nil
|
||||
procedure.lien_notice = nil
|
||||
|
||||
if is_different_admin || from_library
|
||||
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
|
||||
end
|
||||
procedure.draft_revision.procedure = procedure
|
||||
|
||||
if is_different_admin
|
||||
procedure.administrateurs = [admin]
|
||||
|
@ -343,6 +350,18 @@ class Procedure < ApplicationRecord
|
|||
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -582,6 +601,10 @@ class Procedure < ApplicationRecord
|
|||
ApiEntrepriseToken.new(api_entreprise_token).expired?
|
||||
end
|
||||
|
||||
def create_new_revision
|
||||
draft_revision.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)
|
||||
|
@ -605,15 +628,23 @@ class Procedure < ApplicationRecord
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
def after_close
|
||||
now = Time.zone.now
|
||||
update!(closed_at: now)
|
||||
# FIXUP: needed during transition to revisions
|
||||
RevisionsMigration.add_revisions(self)
|
||||
update!(closed_at: Time.zone.now)
|
||||
end
|
||||
|
||||
def after_unpublish
|
||||
# FIXUP: needed during transition to revisions
|
||||
RevisionsMigration.add_revisions(self)
|
||||
update!(unpublished_at: Time.zone.now)
|
||||
end
|
||||
|
||||
|
|
|
@ -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.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.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|
|
||||
expect(stc).to have_same_attributes_as(ptc)
|
||||
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)
|
||||
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)
|
||||
|
||||
|
@ -432,7 +440,7 @@ describe Procedure do
|
|||
|
||||
cloned_procedure = subject
|
||||
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
|
||||
|
||||
context 'when the procedure is cloned from the library' do
|
||||
|
|
Loading…
Reference in a new issue