From c7259995829312e3658ec9aa19bd1b98524fea7d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 22 Oct 2021 15:17:25 +0200 Subject: [PATCH] move merge method to user --- app/controllers/manager/users_controller.rb | 14 +----- app/models/user.rb | 16 +++++++ .../manager/users_controller_spec.rb | 39 ++-------------- spec/models/user_spec.rb | 46 +++++++++++++++++++ 4 files changed, 67 insertions(+), 48 deletions(-) diff --git a/app/controllers/manager/users_controller.rb b/app/controllers/manager/users_controller.rb index 5fb9e4e6d..7b3a376fe 100644 --- a/app/controllers/manager/users_controller.rb +++ b/app/controllers/manager/users_controller.rb @@ -14,19 +14,7 @@ module Manager flash[:error] = user.errors.full_messages.to_sentence end else - user.dossiers.update_all(user_id: targeted_user.id) - - [ - [user.instructeur, targeted_user.instructeur], - [user.expert, targeted_user.expert], - [user.administrateur, targeted_user.administrateur] - ].each do |old_role, targeted_role| - if targeted_role.nil? - old_role&.update(user: targeted_user) - else - targeted_role.merge(old_role) - end - end + targeted_user.merge(user) flash[:notice] = "Le compte « #{targeted_email} » a absorbé le compte « #{user.email} »." end diff --git a/app/models/user.rb b/app/models/user.rb index f2143203a..6bde56fe1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -194,6 +194,22 @@ class User < ApplicationRecord end end + def merge(old_user) + old_user.dossiers.update_all(user_id: id) + + [ + [old_user.instructeur, instructeur], + [old_user.expert, expert], + [old_user.administrateur, administrateur] + ].each do |old_role, targeted_role| + if targeted_role.nil? + old_role&.update(user: self) + else + targeted_role.merge(old_role) + end + end + end + private def link_invites! diff --git a/spec/controllers/manager/users_controller_spec.rb b/spec/controllers/manager/users_controller_spec.rb index 241e59fa0..602ad70eb 100644 --- a/spec/controllers/manager/users_controller_spec.rb +++ b/spec/controllers/manager/users_controller_spec.rb @@ -48,43 +48,12 @@ describe Manager::UsersController, type: :controller do let(:targeted_user) { create(:user, email: 'email.existant@domaine.fr', password: '{My-$3cure-p4ssWord}') } let(:nouvel_email) { targeted_user.email } - context 'and the old account has a dossier' do - let!(:dossier) { create(:dossier, user: user) } + it 'launches the merge process' do + expect_any_instance_of(User).to receive(:merge).with(user) - it 'transfers the dossier' do - subject + subject - expect(targeted_user.dossiers).to match([dossier]) - end - end - - context 'and the old account belongs to an instructeur, expert and administrateur' do - let!(:instructeur) { create(:instructeur, user: user) } - let!(:expert) { create(:expert, user: user) } - let!(:administrateur) { create(:administrateur, user: user, instructeur: instructeur) } - - it 'transfers instructeur account' do - subject - targeted_user.reload - - expect(targeted_user.instructeur).to match(instructeur) - expect(targeted_user.expert).to match(expert) - expect(targeted_user.administrateur).to match(administrateur) - expect(flash[:notice]).to match("Le compte « email.existant@domaine.fr » a absorbé le compte « ancien.email@domaine.fr ».") - end - - context 'and the targeted account owns an instructeur and expert as well' do - let!(:targeted_instructeur) { create(:instructeur, user: targeted_user) } - let!(:targeted_expert) { create(:expert, user: targeted_user) } - let!(:targeted_administrateur) { create(:administrateur, user: targeted_user) } - - it 'merge the account' do - expect_any_instance_of(Instructeur).to receive(:merge) - expect_any_instance_of(Expert).to receive(:merge) - expect_any_instance_of(Administrateur).to receive(:merge) - subject - end - end + expect(flash[:notice]).to match("Le compte « email.existant@domaine.fr » a absorbé le compte « ancien.email@domaine.fr ».") end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6989089d7..e4055cb22 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -417,4 +417,50 @@ describe User, type: :model do end end end + + describe '#merge' do + let(:old_user) { create(:user) } + let(:targeted_user) { create(:user) } + + subject { targeted_user.merge(old_user) } + + context 'and the old account has a dossier' do + let!(:dossier) { create(:dossier, user: old_user) } + + it 'transfers the dossier' do + subject + + expect(targeted_user.dossiers).to match([dossier]) + end + end + + context 'and the old account belongs to an instructeur, expert and administrateur' do + let!(:expert) { create(:expert, user: old_user) } + let!(:administrateur) { create(:administrateur, user: old_user) } + let!(:instructeur) { old_user.instructeur } + + it 'transfers instructeur account' do + subject + targeted_user.reload + + expect(targeted_user.instructeur).to match(instructeur) + expect(targeted_user.expert).to match(expert) + expect(targeted_user.administrateur).to match(administrateur) + end + + context 'and the targeted account owns an instructeur and expert as well' do + let!(:targeted_administrateur) { create(:administrateur, user: targeted_user) } + let!(:targeted_instructeur) { targeted_user.instructeur } + let!(:targeted_expert) { create(:expert, user: targeted_user) } + + it 'merge the account' do + expect(targeted_instructeur).to receive(:merge).with(instructeur) + expect(targeted_expert).to receive(:merge).with(expert) + expect(targeted_administrateur).to receive(:merge).with(administrateur) + + subject + end + end + end + end end