[Fix #3192] Clone service if procedure is transferred
This commit is contained in:
parent
f40a150ee3
commit
b40c706d98
4 changed files with 32 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -335,14 +335,15 @@ describe Procedure do
|
|||
let!(:type_de_champ_private_2) { create(:type_de_champ_drop_down_list, :private, procedure: procedure, order_place: 2) }
|
||||
let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) }
|
||||
let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) }
|
||||
let(:received_mail){ create(:received_mail) }
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue