refactor(dossier): stop destroying champs in rebase
This commit is contained in:
parent
0594887401
commit
6cbf66dfb8
3 changed files with 19 additions and 52 deletions
|
@ -49,59 +49,27 @@ module DossierRebaseConcern
|
|||
# revision we are rebasing to
|
||||
target_revision = procedure.published_revision
|
||||
|
||||
# index published types de champ coordinates by stable_id
|
||||
target_coordinates_by_stable_id = target_revision
|
||||
.revision_types_de_champ
|
||||
.includes(:parent)
|
||||
.index_by(&:stable_id)
|
||||
|
||||
changes_by_op = pending_changes
|
||||
changed_stable_ids_by_op = pending_changes
|
||||
.group_by(&:op)
|
||||
.tap { _1.default = [] }
|
||||
|
||||
champs_by_stable_id = champs
|
||||
.group_by(&:stable_id)
|
||||
.transform_values { Champ.where(id: _1) }
|
||||
.tap { _1.default = Champ.none }
|
||||
|
||||
# remove champ
|
||||
changes_by_op[:remove].each { champs_by_stable_id[_1.stable_id].destroy_all }
|
||||
|
||||
# update champ
|
||||
changes_by_op[:update].each { champs_by_stable_id[_1.stable_id].update_all(rebased_at: Time.zone.now) }
|
||||
.transform_values { _1.map(&:stable_id) }
|
||||
updated_stable_ids = changed_stable_ids_by_op.fetch(:update, [])
|
||||
added_stable_ids = changed_stable_ids_by_op.fetch(:add, [])
|
||||
|
||||
# update dossier revision
|
||||
update_column(:revision_id, target_revision.id)
|
||||
|
||||
# add champ (after changing dossier revision to avoid errors)
|
||||
changes_by_op[:add]
|
||||
.map { target_coordinates_by_stable_id[_1.stable_id] }
|
||||
.each { add_new_champs_for_revision(_1) }
|
||||
end
|
||||
# mark updated champs as rebased
|
||||
champs.where(stable_id: updated_stable_ids).update_all(rebased_at: Time.zone.now)
|
||||
|
||||
def add_new_champs_for_revision(target_coordinate)
|
||||
if target_coordinate.child?
|
||||
row_ids = repetition_row_ids(target_coordinate.parent.type_de_champ)
|
||||
|
||||
if row_ids.present?
|
||||
row_ids.each do |row_id|
|
||||
create_champ(target_coordinate, row_id:)
|
||||
end
|
||||
elsif target_coordinate.parent.mandatory?
|
||||
create_champ(target_coordinate, row_id: ULID.generate)
|
||||
# add rows for new repetitions
|
||||
repetition_types_de_champ = target_revision
|
||||
.types_de_champ
|
||||
.repetition
|
||||
.where(stable_id: added_stable_ids)
|
||||
repetition_types_de_champ.mandatory
|
||||
.or(repetition_types_de_champ.private_only)
|
||||
.find_each do |type_de_champ|
|
||||
self.champs << type_de_champ.build_champ(row_id: ULID.generate, rebased_at: Time.zone.now)
|
||||
end
|
||||
else
|
||||
create_champ(target_coordinate)
|
||||
end
|
||||
end
|
||||
|
||||
def create_champ(target_coordinate, row_id: nil)
|
||||
self.champs << target_coordinate
|
||||
.type_de_champ
|
||||
.build_champ(rebased_at: Time.zone.now, row_id:)
|
||||
end
|
||||
|
||||
def purge_piece_justificative_file(champ)
|
||||
ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all
|
||||
end
|
||||
end
|
||||
|
|
|
@ -171,6 +171,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
scope :not_condition, -> { where(condition: nil) }
|
||||
scope :fillable, -> { where.not(type_champ: [type_champs.fetch(:header_section), type_champs.fetch(:explication)]) }
|
||||
scope :with_header_section, -> { where.not(type_champ: TypeDeChamp.type_champs[:explication]) }
|
||||
scope :mandatory, -> { where(mandatory: true) }
|
||||
|
||||
scope :dubious, -> {
|
||||
where("unaccent(types_de_champ.libelle) ~* unaccent(?)", DubiousProcedure.forbidden_regexp)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue