[Fix #3192] Clone service if procedure is transferred

This commit is contained in:
Mathieu Magnin 2018-12-19 15:35:07 +01:00
parent f40a150ee3
commit b40c706d98
4 changed files with 32 additions and 4 deletions

View file

@ -213,6 +213,8 @@ class Procedure < ApplicationRecord
if from_library
procedure.service = nil
elsif self.service.present? && (self.administrateur_id != admin.id)
procedure.service = self.service.clone_and_assign_to_administrateur(admin)
end
procedure

View file

@ -25,4 +25,10 @@ class Service < ApplicationRecord
validates :horaires, presence: { message: 'doivent être renseignés' }, allow_nil: false
validates :adresse, presence: { message: 'doit être renseignée' }, allow_nil: false
validates :administrateur, presence: { message: 'doit être renseigné' }, allow_nil: false
def clone_and_assign_to_administrateur(administrateur)
service_cloned = self.dup
service_cloned.administrateur = administrateur
service_cloned
end
end

View file

@ -580,7 +580,7 @@ describe Admin::ProceduresController, type: :controller do
end
describe 'POST #transfer' do
let!(:procedure) { create :procedure, administrateur: admin }
let!(:procedure) { create :procedure, :with_service, administrateur: admin }
subject { post :transfer, params: { email_admin: email_admin, procedure_id: procedure.id } }
@ -597,7 +597,12 @@ describe Admin::ProceduresController, type: :controller do
let(:email_admin) { 'new_admin@admin.com' }
it { expect(subject.status).to eq 200 }
it { expect { subject }.to change(Procedure, :count).by(1) }
it { expect { subject }.to change(new_admin.procedures, :count).by(1) }
it "should create a new service" do
subject
expect(new_admin.procedures.last.service_id).not_to eq(procedure.service_id)
end
end
context 'when admin is know but its email was not downcased' do

View file

@ -337,12 +337,13 @@ describe Procedure do
let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) }
let(:received_mail) { create(:received_mail) }
let(:from_library) { false }
let(:administrateur) { procedure.administrateur }
before do
@logo = File.open('spec/fixtures/files/white.png')
@signature = File.open('spec/fixtures/files/black.png')
@attestation_template = create(:attestation_template, procedure: procedure, logo: @logo, signature: @signature)
@procedure = procedure.clone(procedure.administrateur, from_library)
@procedure = procedure.clone(administrateur, from_library)
@procedure.save
end
@ -389,12 +390,26 @@ describe Procedure do
let(:from_library) { true }
it { expect(subject.cloned_from_library).to be(true) }
it 'should set service_id to nil' do
expect(subject.service).to eq(nil)
end
end
it 'should keep service_id' do
expect(subject.service).to eq(service)
end
context 'when the procedure is cloned to another administrateur' do
let(:administrateur) { create(:administrateur) }
it 'should clone service' do
expect(subject.service.id).not_to eq(service.id)
expect(subject.service.administrateur_id).not_to eq(service.administrateur_id)
expect(subject.service.attributes.except("id", "administrateur_id", "created_at", "updated_at")).to eq(service.attributes.except("id", "administrateur_id", "created_at", "updated_at"))
end
end
it 'should duplicate existing mail_templates' do
expect(subject.received_mail.attributes.except("id", "procedure_id", "created_at", "updated_at")).to eq procedure.received_mail.attributes.except("id", "procedure_id", "created_at", "updated_at")
expect(subject.received_mail.id).not_to eq procedure.received_mail.id