app: move clone to NewAdministrateur::ProceduresController

This commit is contained in:
Pierre de La Morinerie 2021-08-31 17:20:32 -05:00
parent 56fa7e7cd6
commit e7c8a9fff5
5 changed files with 76 additions and 74 deletions

View file

@ -10,32 +10,4 @@ class Admin::ProceduresController < AdminController
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
def clone
procedure = Procedure.find(params[:procedure_id])
new_procedure = procedure.clone(current_administrateur, cloned_from_library?)
if new_procedure.valid?
flash.notice = 'Démarche clonée'
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
if cloned_from_library?
flash.alert = new_procedure.errors.full_messages
redirect_to new_from_existing_admin_procedures_path
else
flash.alert = new_procedure.errors.full_messages
redirect_to admin_procedures_path
end
end
rescue ActiveRecord::RecordNotFound
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
private
def cloned_from_library?
params[:from_new_from_existing].present?
end
end

View file

@ -110,6 +110,28 @@ module NewAdministrateur
end
end
def clone
procedure = Procedure.find(params[:procedure_id])
new_procedure = procedure.clone(current_administrateur, cloned_from_library?)
if new_procedure.valid?
flash.notice = 'Démarche clonée'
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
if cloned_from_library?
flash.alert = new_procedure.errors.full_messages
redirect_to new_from_existing_admin_procedures_path
else
flash.alert = new_procedure.errors.full_messages
redirect_to admin_procedures_path
end
end
rescue ActiveRecord::RecordNotFound
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
def destroy
procedure = current_administrateur.procedures.find(params[:id])
@ -236,5 +258,9 @@ module NewAdministrateur
def allow_decision_access_params
params.require(:experts_procedure).permit(:allow_decision_access)
end
def cloned_from_library?
params[:from_new_from_existing].present?
end
end
end

View file

@ -195,7 +195,6 @@ Rails.application.routes.draw do
resources :procedures, only: [] do
put 'archive' => 'procedures#archive', as: :archive
put 'clone' => 'procedures#clone', as: :clone
end
namespace :assigns do
@ -404,6 +403,7 @@ Rails.application.routes.draw do
put :experts_require_administrateur_invitation
end
put 'clone'
get 'publication' => 'procedures#publication', as: :publication
put 'publish' => 'procedures#publish', as: :publish
get 'transfert' => 'procedures#transfert', as: :transfert

View file

@ -67,49 +67,4 @@ describe Admin::ProceduresController, type: :controller do
end
end
describe 'PUT #clone' do
let!(:procedure) { create(:procedure, :with_notice, :with_deliberation, administrateur: admin) }
let(:params) { { procedure_id: procedure.id } }
subject { put :clone, params: params }
before do
response = Typhoeus::Response.new(code: 200, body: 'Hello world')
Typhoeus.stub(/active_storage\/disk/).and_return(response)
end
it { expect { subject }.to change(Procedure, :count).by(1) }
context 'when admin is the owner of the procedure' do
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_falsey
expect(Procedure.last.notice.attached?).to be_truthy
expect(Procedure.last.deliberation.attached?).to be_truthy
expect(flash[:notice]).to have_content 'Démarche 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
let(:admin_2) { create(:administrateur) }
before do
sign_out(admin.user)
sign_in(admin_2.user)
subject
end
it 'creates a new procedure and redirect to it' do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
expect(flash[:notice]).to have_content 'Démarche clonée'
end
end
end
end

View file

@ -294,6 +294,55 @@ describe NewAdministrateur::ProceduresController, type: :controller do
end
end
describe 'PUT #clone' do
let(:procedure) { create(:procedure, :with_notice, :with_deliberation, administrateur: admin) }
let(:params) { { procedure_id: procedure.id } }
subject { put :clone, params: params }
before do
procedure
response = Typhoeus::Response.new(code: 200, body: 'Hello world')
Typhoeus.stub(/active_storage\/disk/).and_return(response)
end
it { expect { subject }.to change(Procedure, :count).by(1) }
context 'when admin is the owner of the procedure' do
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_falsey
expect(Procedure.last.notice.attached?).to be_truthy
expect(Procedure.last.deliberation.attached?).to be_truthy
expect(flash[:notice]).to have_content 'Démarche 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
let(:admin_2) { create(:administrateur) }
before do
sign_out(admin.user)
sign_in(admin_2.user)
subject
end
it 'creates a new procedure and redirect to it' do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
expect(flash[:notice]).to have_content 'Démarche clonée'
end
end
end
describe 'DELETE #destroy' do
let(:procedure_draft) { create(:procedure, administrateurs: [admin]) }
let(:procedure_published) { create(:procedure, :published, administrateurs: [admin]) }