fix(revision): fix migrate_parent!

This commit is contained in:
Paul Chavard 2022-05-12 09:26:43 +02:00
parent 3d7356c2d0
commit 6adb3f7a3a
3 changed files with 18 additions and 9 deletions

View file

@ -732,6 +732,13 @@ class Procedure < ApplicationRecord
new_draft.revision_types_de_champ.reload
# Some revisions do not have links to children types de champ
new_draft
.types_de_champ
.filter(&:repetition?)
.flat_map(&:types_de_champ)
.each(&:migrate_parent!)
new_draft
end

View file

@ -381,13 +381,12 @@ class ProcedureRevision < ApplicationRecord
end
def revise_type_de_champ(type_de_champ)
types_de_champ_association = type_de_champ.private? ? :revision_types_de_champ_private : :revision_types_de_champ_public
association = send(types_de_champ_association).find_by!(type_de_champ: type_de_champ)
revision_type_de_champ = revision_types_de_champ.find_by!(type_de_champ: type_de_champ)
cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
end
cloned_type_de_champ.revision = self
association.update!(type_de_champ: cloned_type_de_champ)
revision_type_de_champ.update!(type_de_champ: cloned_type_de_champ)
cloned_type_de_champ.types_de_champ.each(&:migrate_parent!)
cloned_type_de_champ
end

View file

@ -18,6 +18,8 @@
# stable_id :bigint
#
class TypeDeChamp < ApplicationRecord
self.ignored_columns = [:migrated_parent]
enum type_champs: {
text: 'text',
textarea: 'textarea',
@ -394,12 +396,13 @@ class TypeDeChamp < ApplicationRecord
end
def migrate_parent!
if parent_id.present? && migrated_parent.nil?
ProcedureRevisionTypeDeChamp.create(parent: parent.revision_type_de_champ,
if parent_id.present? && revision_types_de_champ.empty?
parent.revision_types_de_champ.each do |revision_type_de_champ|
ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ,
type_de_champ: self,
revision_id: parent.revision_type_de_champ.revision_id,
revision_id: revision_type_de_champ.revision_id,
position: order_place)
update_column(:migrated_parent, true)
end
end
self
end