diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index d761f78ec..f7535fa7c 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -160,7 +160,7 @@ class Admin::ProceduresController < AdminController render '/admin/procedures/transfer', formats: 'js', status: 404 else procedure = current_administrateur.procedures.find(params[:procedure_id]) - clone_procedure = procedure.clone(admin) + clone_procedure = procedure.clone(admin, false) clone_procedure.save @@ -184,13 +184,13 @@ class Admin::ProceduresController < AdminController def clone procedure = Procedure.find(params[:procedure_id]) - new_procedure = procedure.clone(current_administrateur) + new_procedure = procedure.clone(current_administrateur, cloned_from_library?) if new_procedure.save flash.notice = 'Procédure clonée' redirect_to edit_admin_procedure_path(id: new_procedure.id) else - if params[:from_new_from_existing].present? + if cloned_from_library? flash.alert = new_procedure.errors.full_messages redirect_to new_from_existing_admin_procedures_path else @@ -249,6 +249,10 @@ class Admin::ProceduresController < AdminController private + def cloned_from_library? + params[:from_new_from_existing].present? + end + def procedure_params editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on] if @procedure.try(:locked?) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index b76eb3393..630f0feb9 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -104,7 +104,7 @@ class Procedure < ApplicationRecord publiee_ou_archivee? end - def clone(admin) + def clone(admin, from_library) procedure = self.deep_clone(include: { types_de_piece_justificative: nil, @@ -125,6 +125,8 @@ class Procedure < ApplicationRecord procedure.refused_mail = refused_mail.try(:dup) procedure.without_continuation_mail = without_continuation_mail.try(:dup) + procedure.cloned_from_library = from_library + procedure end diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 0931d12d9..ac665c5ab 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -456,19 +456,25 @@ describe Admin::ProceduresController, type: :controller do describe 'PUT #clone' do let!(:procedure) { create(:procedure, administrateur: admin) } - subject { put :clone, params: { procedure_id: procedure.id } } + let(:params) { { procedure_id: procedure.id } } + subject { put :clone, params: params } it { expect { subject }.to change(Procedure, :count).by(1) } context 'when admin is the owner of the procedure' do - before do - subject - end + before { subject } it 'creates a new procedure and redirect to it' do expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id) + expect(Procedure.last.cloned_from_library).to be(false) expect(flash[:notice]).to have_content 'Procédure clonée' end + + context 'when the procedure is cloned from the library' do + let(:params) { { procedure_id: procedure.id, from_new_from_existing: true } } + + it { expect(Procedure.last.cloned_from_library).to be(true) } + end end context 'when admin is not the owner of the procedure' do diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 0ed1d296e..fc7029bf4 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -272,12 +272,13 @@ describe Procedure do 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(:from_library) { false } before do @logo = File.open('spec/fixtures/white.png') @signature = File.open('spec/fixtures/black.png') @attestation_template = create(:attestation_template, procedure: procedure, logo: @logo, signature: @signature) - @procedure = procedure.clone(procedure.administrateur) + @procedure = procedure.clone(procedure.administrateur, from_library) @procedure.save end @@ -312,6 +313,14 @@ describe Procedure do end expect(subject.attestation_template.title).to eq(procedure.attestation_template.title) + + expect(subject.cloned_from_library).to be(false) + end + + context 'when the procedure is clone from the library' do + let(:from_library) { true } + + it { expect(subject.cloned_from_library).to be(true) } end it 'should duplicate existing mail_templates' do