beef up the merge methods

This commit is contained in:
simon lehericey 2021-10-07 14:34:01 +02:00
parent 77d14d4a60
commit d7e621d167
4 changed files with 191 additions and 4 deletions

View file

@ -9,6 +9,7 @@
class Expert < ApplicationRecord
has_one :user
has_many :experts_procedures
has_many :procedures, through: :experts_procedures
has_many :avis, through: :experts_procedures
has_many :dossiers, through: :avis
has_many :commentaires
@ -37,7 +38,22 @@ class Expert < ApplicationRecord
end
def merge(old_expert)
old_expert.experts_procedures.update_all(expert_id: id)
procedure_with_new, procedure_without_new = old_expert
.procedures
.partition { |p| p.experts.exists?(id) }
ExpertsProcedure
.where(expert_id: old_expert.id, procedure: procedure_without_new)
.update_all(expert_id: id)
ExpertsProcedure
.where(expert_id: old_expert.id, procedure: procedure_with_new)
.destroy_all
old_expert.commentaires.update_all(expert_id: id)
Avis
.where(claimant_id: old_expert.id, claimant_type: Expert.name)
.update_all(claimant_id: id)
end
end

View file

@ -252,9 +252,29 @@ class Instructeur < ApplicationRecord
end
def merge(old_instructeur)
old_instructeur.assign_to.update_all(instructeur_id: id)
old_instructeur.follows.update_all(instructeur_id: id)
old_instructeur.administrateurs_instructeurs.update_all(instructeur_id: id)
old_instructeur
.assign_to
.where.not(groupe_instructeur_id: assign_to.pluck(:groupe_instructeur_id))
.update_all(instructeur_id: id)
old_instructeur
.follows
.where.not(dossier_id: follows.pluck(:dossier_id))
.update_all(instructeur_id: id)
admin_with_new_instructeur, admin_without_new_instructeur = old_instructeur
.administrateurs
.partition { |admin| admin.instructeurs.exists?(id) }
admin_without_new_instructeur.each do |admin|
admin.instructeurs << self
admin.instructeurs.delete(old_instructeur)
end
admin_with_new_instructeur.each do |admin|
admin.instructeurs.delete(old_instructeur)
end
old_instructeur.commentaires.update_all(instructeur_id: id)
old_instructeur.bulk_messages.update_all(instructeur_id: id)
end