Add commencer/test/:procedure_path route

This commit is contained in:
Paul Chavard 2018-05-17 15:34:51 +02:00
parent ffef1a644c
commit 94d253c6dc
6 changed files with 45 additions and 30 deletions

View file

@ -63,11 +63,6 @@ class Admin::ProceduresController < AdminController
def hide
procedure = current_administrateur.procedures.find(params[:id])
procedure.hide!
# procedure should no longer be reachable so we delete its procedure_path
# that way it is also available for another procedure
# however, sometimes the path has already been deleted (ex: stolen by another procedure),
# so we're not certain the procedure has a procedure_path anymore
procedure.procedure_path.try(:destroy)
flash.notice = "Procédure supprimée, en cas d'erreur contactez nous : contact@demarches-simplifiees.fr"
redirect_to admin_procedures_draft_path

View file

@ -41,28 +41,34 @@ class Users::DossiersController < UsersController
array: true
end
def commencer_test
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
procedure = procedure_path.test_procedure
if procedure.present?
redirect_to new_users_dossier_path(procedure_id: procedure.id)
else
flash.alert = "Procédure inconnue"
redirect_to root_path
end
end
def commencer
if params[:procedure_path].present?
procedure_path = ProcedurePath.where(path: params[:procedure_path]).last
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
procedure = procedure_path.procedure
if procedure_path.nil? || procedure_path.procedure.nil?
flash.alert = "Procédure inconnue"
return redirect_to root_path
if procedure.present?
if procedure.archivee?
@dossier = Dossier.new(procedure: procedure)
render 'commencer/archived'
else
procedure = procedure_path.procedure
redirect_to new_users_dossier_path(procedure_id: procedure.id)
end
else
flash.alert = "Procédure inconnue"
redirect_to root_path
end
if procedure.archivee?
@dossier = Dossier.new(procedure: procedure)
return render 'commencer/archived'
end
redirect_to new_users_dossier_path(procedure_id: procedure.id)
rescue ActiveRecord::RecordNotFound
error_procedure
end
def new

View file

@ -65,8 +65,9 @@ class Procedure < ApplicationRecord
def hide!
now = DateTime.now
self.update(hidden_at: now, aasm_state: :hidden)
self.dossiers.update_all(hidden_at: now)
update(hidden_at: now, aasm_state: :hidden)
procedure_path&.hide!(self)
dossiers.update_all(hidden_at: now)
end
def path

View file

@ -10,4 +10,16 @@ class ProcedurePath < ApplicationRecord
def self.find_with_procedure(procedure)
where(procedure: procedure).or(where(test_procedure: procedure)).last
end
def hide!(procedure)
if self.procedure == procedure
update(procedure: nil)
end
if self.test_procedure == procedure
update(test_procedure: nil)
end
if procedure.nil? && test_procedure.nil?
destroy
end
end
end

View file

@ -211,6 +211,7 @@ Rails.application.routes.draw do
end
namespace :commencer do
get '/test/:procedure_path' => '/users/dossiers#commencer_test', as: :test
get '/:procedure_path' => '/users/dossiers#commencer'
end

View file

@ -547,7 +547,7 @@ describe Admin::ProceduresController, type: :controller do
end
end
describe 'POST transfer' do
describe 'POST #transfer' do
let!(:procedure) { create :procedure, administrateur: admin }
subject { post :transfer, params: { email_admin: email_admin, procedure_id: procedure.id } }
@ -587,17 +587,17 @@ describe Admin::ProceduresController, type: :controller do
end
end
describe "POST hide" do
describe "POST #hide" do
subject { post :hide, params: { id: procedure.id } }
context "when procedure is not owned by administrateur" do
let!(:procedure) { create :procedure, administrateur: create(:administrateur) }
let(:procedure) { create :procedure, administrateur: create(:administrateur) }
it { expect{ subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context "when procedure is owned by administrateur" do
let!(:procedure) { create :procedure, :published, administrateur: admin }
let(:procedure) { create :procedure, :published, administrateur: admin }
before do
subject
@ -605,11 +605,11 @@ describe Admin::ProceduresController, type: :controller do
end
it { expect(procedure.hidden_at).not_to be_nil }
it { expect(procedure.procedure_path).to be_nil }
it { expect(procedure.procedure_path.procedure).to be_nil }
end
context "when procedure has no path" do
let!(:procedure) { create :procedure, administrateur: admin }
let(:procedure) { create :procedure, administrateur: admin }
it { expect{ subject }.not_to raise_error }
it do