From 9fb575ef8c1e4be2bc64a4ba862ae977ebee849f Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 24 Dec 2015 15:57:03 +0100 Subject: [PATCH] Refactor procedure locked and retrieve management --- .../admin/pieces_justificatives_controller.rb | 1 + .../admin/procedures_controller.rb | 16 +++---------- .../admin/types_de_champ_controller.rb | 3 ++- app/controllers/admin_controller.rb | 16 ++++++++----- app/views/admin/procedures/show.html.haml | 24 +++++++++---------- .../pieces_justificatives_controller_spec.rb | 2 +- .../admin/procedures_controller_spec.rb | 2 +- .../admin/types_de_champ_controller_spec.rb | 2 +- .../admin/procedures/show.html.haml_spec.rb | 3 +-- 9 files changed, 32 insertions(+), 37 deletions(-) diff --git a/app/controllers/admin/pieces_justificatives_controller.rb b/app/controllers/admin/pieces_justificatives_controller.rb index 193cc6657..638596511 100644 --- a/app/controllers/admin/pieces_justificatives_controller.rb +++ b/app/controllers/admin/pieces_justificatives_controller.rb @@ -1,5 +1,6 @@ class Admin::PiecesJustificativesController < AdminController before_action :retrieve_procedure + before_action :procedure_locked? def show end diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 4443549fa..eabcb1569 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -1,6 +1,7 @@ class Admin::ProceduresController < AdminController - before_action :retrieve_procedure, only: :edit + before_action :retrieve_procedure, only: [:show, :edit] + before_action :procedure_locked?, only: [:edit] def index @procedures = current_administrateur.procedures.where(archived: false) @@ -15,13 +16,10 @@ class Admin::ProceduresController < AdminController end def show - informations - - @facade = AdminProceduresShowFacades.new @procedure + @facade = AdminProceduresShowFacades.new @procedure.decorate end def edit - informations end def new @@ -83,12 +81,4 @@ class Admin::ProceduresController < AdminController def create_module_api_carto_params params.require(:procedure).require(:module_api_carto_attributes).permit(:id, :use_api_carto, :quartiers_prioritaires, :cadastre) end - - def informations - @procedure = current_administrateur.procedures.find(params[:id]).decorate - - rescue ActiveRecord::RecordNotFound - flash.alert = 'Procédure inéxistante' - redirect_to admin_procedures_path - end end diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index 85daf3666..66349aef3 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -1,6 +1,7 @@ class Admin::TypesDeChampController < AdminController before_action :retrieve_procedure - + before_action :procedure_locked? + def destroy @procedure.types_de_champ.destroy(params[:id]) render 'show', format: :js diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index cddb113ea..48598e2c4 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -6,15 +6,19 @@ class AdminController < ApplicationController end def retrieve_procedure - id = params[:procedure_id] || params[:id ] + id = params[:procedure_id] || params[:id] @procedure = current_administrateur.procedures.find(id) - if @procedure.locked? - render json: {message: 'Procedure locked'}, status: 403 - end - rescue ActiveRecord::RecordNotFound - render json: {message: 'Procedure not found'}, status: 404 + flash.alert = 'Procédure inéxistante' + redirect_to admin_procedures_path, status: 404 + end + + def procedure_locked? + if @procedure.locked? + flash.alert = 'Procédure verrouillée' + redirect_to admin_procedure_path(id: @procedure.id) + end end end diff --git a/app/views/admin/procedures/show.html.haml b/app/views/admin/procedures/show.html.haml index f633f2392..3cffe702b 100644 --- a/app/views/admin/procedures/show.html.haml +++ b/app/views/admin/procedures/show.html.haml @@ -1,30 +1,30 @@ #procedure_show %h2.text-info - =@procedure.libelle + =@facade.procedure.libelle %br %ul.nav.nav-tabs %li.active - = link_to('Informations', admin_procedure_path(@procedure)) + = link_to('Informations', admin_procedure_path(@facade.procedure)) - %li{ class: ('disabled' if @procedure.locked?) } - = link_to_unless(@procedure.locked?, 'Description', edit_admin_procedure_path(@procedure)) do + %li{ class: ('disabled' if @facade.procedure.locked?) } + = link_to_unless(@facade.procedure.locked?, 'Description', edit_admin_procedure_path(@facade.procedure)) do = link_to('Description', '#') - %li{ class: ('disabled' if @procedure.locked?) } - = link_to_unless(@procedure.locked?, 'Champs', admin_procedure_types_de_champ_path(@procedure)) do + %li{ class: ('disabled' if @facade.procedure.locked?) } + = link_to_unless(@facade.procedure.locked?, 'Champs', admin_procedure_types_de_champ_path(@facade.procedure)) do = link_to('Champs', '#') - %li{ class: ('disabled' if @procedure.locked?) } - = link_to_unless(@procedure.locked?, 'Pièces justificatives', admin_procedure_pieces_justificatives_path(@procedure)) do + %li{ class: ('disabled' if @facade.procedure.locked?) } + = link_to_unless(@facade.procedure.locked?, 'Pièces justificatives', admin_procedure_pieces_justificatives_path(@facade.procedure)) do = link_to('Pièces justificatives', '#') %li{style:'float:right'} - = form_tag admin_procedure_archive_path(procedure_id: @procedure.id, archive: !@procedure.archived?), method: :put do + = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put do %button#archive.btn.btn-nav.text-info{type: :button} %i.fa.fa-eraser - - if @procedure.archived + - if @facade.procedure.archived = 'Réactiver' - else = 'Archiver' @@ -36,7 +36,7 @@ %i.fa.fa-remove Annuler - - if @procedure.locked? + - if @facade.procedure.locked? #procedure_locked.center %h5 .label.label-info La procédure ne peut plus être modifiée car un usagé a déjà déposé un dossier @@ -44,7 +44,7 @@ %div %h3 Lien procédure %div{style:'margin-left:3%'} - = @procedure.lien + = @facade.procedure.lien %br %h3 Dossiers diff --git a/spec/controllers/admin/pieces_justificatives_controller_spec.rb b/spec/controllers/admin/pieces_justificatives_controller_spec.rb index d90860116..b52e019ce 100644 --- a/spec/controllers/admin/pieces_justificatives_controller_spec.rb +++ b/spec/controllers/admin/pieces_justificatives_controller_spec.rb @@ -19,7 +19,7 @@ describe Admin::PiecesJustificativesController, type: :controller do context 'when procedure have at least a file' do let!(:dossier) { create(:dossier, :with_user, procedure: procedure, state: :initiated) } - it { expect(subject.status).to eq(403) } + it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end context 'when procedure does not belong to admin' do diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 534f17b78..627b53330 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -68,7 +68,7 @@ describe Admin::ProceduresController, type: :controller do context 'when procedure have at least a file' do let!(:dossier) { create(:dossier, :with_user, procedure: procedure, state: :initiated) } - it { expect(subject.status).to eq(403) } + it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end context "when procedure doesn't exist" do diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 9d660884c..c99924d29 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -21,7 +21,7 @@ describe Admin::TypesDeChampController, type: :controller do context 'when procedure have at least a file' do let!(:dossier) { create(:dossier, :with_user, procedure: procedure, state: :initiated) } - it { expect(subject.status).to eq(403) } + it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end context 'when procedure does not belong to admin' do diff --git a/spec/views/admin/procedures/show.html.haml_spec.rb b/spec/views/admin/procedures/show.html.haml_spec.rb index d6d199501..c40f96b88 100644 --- a/spec/views/admin/procedures/show.html.haml_spec.rb +++ b/spec/views/admin/procedures/show.html.haml_spec.rb @@ -4,8 +4,7 @@ describe 'admin/procedures/show.html.haml', type: :view do let(:procedure) { create(:procedure) } before do - assign(:procedure, procedure.decorate) - assign(:facade, AdminProceduresShowFacades.new(procedure)) + assign(:facade, AdminProceduresShowFacades.new(procedure.decorate)) render end