raise if trying to del a user who can't be deleted

This commit is contained in:
Christophe Robillard 2020-01-08 17:00:00 +01:00 committed by clemkeirua
parent 696a058280
commit 0f881f942b
3 changed files with 6 additions and 9 deletions

View file

@ -101,6 +101,10 @@ class User < ApplicationRecord
end
def delete_and_keep_track_dossiers(administration)
if !can_be_deleted?
raise "Cannot delete this user because instruction has started for some dossiers"
end
if can_be_deleted?
dossiers.each do |dossier|
dossier.delete_and_keep_track(administration)

View file

@ -15,10 +15,6 @@ namespace :support do
user = User.find_by!(email: user_email)
administration = Administration.find_by!(email: administration_email)
if !user.can_be_deleted?
fail "Cannot delete this user because instruction has started for some dossiers"
end
user.delete_and_keep_track_dossiers(administration)
user.destroy
end

View file

@ -232,11 +232,8 @@ describe User, type: :model do
context 'avec un dossier en instruction' do
let!(:dossier_en_instruction) { create(:dossier, :en_instruction, user: user) }
it 'ne supprime rien si dossier en instruction' do
user.delete_and_keep_track_dossiers(administration)
expect(Dossier.find_by(id: dossier_en_instruction.id)).to be_present
expect(User.find_by(id: user.id)).to be_present
it 'raises' do
expect { user.delete_and_keep_track_dossiers(administration) }.to raise_error
end
end