From 3f25624a5a8490e9c6b1451a118c834941506533 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 6 Apr 2021 13:18:51 +0200 Subject: [PATCH 1/4] routes: nest ExpertsProcedures routes in a resource --- .../new_administrateur/experts_procedures_controller.rb | 2 +- config/routes.rb | 7 +++++-- .../experts_procedures_controller_spec.rb | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/new_administrateur/experts_procedures_controller.rb b/app/controllers/new_administrateur/experts_procedures_controller.rb index 1b965f0dc..0a246ed12 100644 --- a/app/controllers/new_administrateur/experts_procedures_controller.rb +++ b/app/controllers/new_administrateur/experts_procedures_controller.rb @@ -33,7 +33,7 @@ module NewAdministrateur end def revoke_expert_from_procedure - expert_procedure = ExpertsProcedure.find_by!(procedure: @procedure, id: params[:expert_procedure][:id]) + expert_procedure = ExpertsProcedure.find_by!(procedure: @procedure, id: params[:id]) expert_email = expert_procedure.expert.email expert_procedure.update!(revoked_at: Time.zone.now) flash[:notice] = "#{expert_email} a été révoqué de la démarche et ne pourra plus déposer d'avis." diff --git a/config/routes.rb b/config/routes.rb index 81156cf26..9ae110e28 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -408,8 +408,6 @@ Rails.application.routes.draw do post 'transfer' => 'procedures#transfer', as: :transfer get 'invited_expert_list' put 'update_allow_decision_access' => 'procedures#update_allow_decision_access', as: :update_allow_decision_access - post 'add_expert_to_procedure' => 'experts_procedures#add_expert_to_procedure', as: :add_expert_to_procedure - put 'revoke_expert_from_procedure' => 'experts_procedures#revoke_expert_from_procedure', as: :revoke_expert_from_procedure resources :mail_templates, only: [:edit, :update] resources :groupe_instructeurs, only: [:index, :show, :create, :update, :destroy] do @@ -427,6 +425,11 @@ Rails.application.routes.draw do resources :administrateurs, controller: 'procedure_administrateurs', only: [:index, :create, :destroy] + resources :experts, controller: 'experts_procedures', only: [] do + post :add_expert_to_procedure, on: :collection + delete :revoke_expert_from_procedure, on: :member + end + resources :types_de_champ, only: [:create, :update, :destroy] do member do patch :move diff --git a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb index 39e28e3f9..41a0b637e 100644 --- a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb @@ -32,7 +32,7 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do let(:expert_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } subject do - put :revoke_expert_from_procedure, params: { procedure_id: procedure.id, expert_procedure: { id: expert_procedure.id } } + delete :revoke_expert_from_procedure, params: { procedure_id: procedure.id, id: expert_procedure.id } end before do From 7acb4cee837f24fd7e08e96cdc6ba305d5e23a9f Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 6 Apr 2021 11:42:21 +0000 Subject: [PATCH 2/4] controllers: use standard action names in ExpertsProceduresController --- .../experts_procedures_controller.rb | 6 +++--- .../procedures/invited_expert_list.html.haml | 12 +++++------- config/routes.rb | 5 +---- .../experts_procedures_controller_spec.rb | 13 ++++++++----- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/controllers/new_administrateur/experts_procedures_controller.rb b/app/controllers/new_administrateur/experts_procedures_controller.rb index 0a246ed12..89645ec5f 100644 --- a/app/controllers/new_administrateur/experts_procedures_controller.rb +++ b/app/controllers/new_administrateur/experts_procedures_controller.rb @@ -1,8 +1,8 @@ module NewAdministrateur class ExpertsProceduresController < AdministrateurController - before_action :retrieve_procedure, only: [:add_expert_to_procedure, :revoke_expert_from_procedure] + before_action :retrieve_procedure - def add_expert_to_procedure + def create emails = params['emails'].presence || [].to_json emails = JSON.parse(emails).map(&:strip).map(&:downcase) @@ -32,7 +32,7 @@ module NewAdministrateur redirect_to admin_procedure_invited_expert_list_path(@procedure) end - def revoke_expert_from_procedure + def destroy expert_procedure = ExpertsProcedure.find_by!(procedure: @procedure, id: params[:id]) expert_email = expert_procedure.expert.email expert_procedure.update!(revoked_at: Time.zone.now) diff --git a/app/views/new_administrateur/procedures/invited_expert_list.html.haml b/app/views/new_administrateur/procedures/invited_expert_list.html.haml index 3272948d4..5e5e60f4a 100644 --- a/app/views/new_administrateur/procedures/invited_expert_list.html.haml +++ b/app/views/new_administrateur/procedures/invited_expert_list.html.haml @@ -11,7 +11,7 @@ .card .card-title Affecter des experts à la démarche = form_for :experts_procedure, - url: admin_procedure_add_expert_to_procedure_path(@procedure), + url: admin_procedure_experts_path(@procedure), html: { class: 'form' } do |f| .instructeur-wrapper @@ -57,13 +57,11 @@ %span.toggle-switch-label.on %span.toggle-switch-label.off %td.actions= button_to 'retirer', - { action: "revoke_expert_from_procedure", :controller=>"new_administrateur/experts_procedures" }, - { method: :put, - data: { confirm: "Êtes-vous sûr de vouloir révoquer l'expert « #{expert_procedure.expert.email} » de la démarche #{expert_procedure.procedure.libelle} ? Les instructeurs ne pourront plus lui demander d'avis" }, - params: { expert_procedure: { id: expert_procedure.id }}, - class: 'button' } + admin_procedure_expert_path(id: expert_procedure, procedure: @procedure), + method: :delete, + data: { confirm: "Êtes-vous sûr de vouloir révoquer l'expert « #{expert_procedure.expert.email} » de la démarche #{expert_procedure.procedure.libelle} ? Les instructeurs ne pourront plus lui demander d'avis" }, + class: 'button' - else .blank-tab %h2.empty-text Aucun expert invité pour le moment. %p.empty-text-details Les instructeurs de cette démarche n'ont pas encore fait appel aux experts. - diff --git a/config/routes.rb b/config/routes.rb index 9ae110e28..41180668f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -425,10 +425,7 @@ Rails.application.routes.draw do resources :administrateurs, controller: 'procedure_administrateurs', only: [:index, :create, :destroy] - resources :experts, controller: 'experts_procedures', only: [] do - post :add_expert_to_procedure, on: :collection - delete :revoke_expert_from_procedure, on: :member - end + resources :experts, controller: 'experts_procedures', only: [:create, :destroy] resources :types_de_champ, only: [:create, :update, :destroy] do member do diff --git a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb index 41a0b637e..88a8d38fc 100644 --- a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb @@ -4,14 +4,17 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do before do sign_in(admin.user) end - describe '#add_expert_to_procedure' do + + describe '#create' do let(:procedure) { create :procedure, administrateur: admin } let(:expert) { create(:expert) } let(:expert2) { create(:expert) } subject do - post :add_expert_to_procedure, - params: { procedure_id: procedure.id, emails: "[\"#{expert.email}\",\"#{expert2.email}\"]" } + post :create, params: { + procedure_id: procedure.id, + emails: "[\"#{expert.email}\",\"#{expert2.email}\"]" + } end before do @@ -26,13 +29,13 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do end end - describe '#revoke_expert_from_procedure' do + describe '#delete' do let(:procedure) { create :procedure, administrateur: admin } let(:expert) { create(:expert) } let(:expert_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } subject do - delete :revoke_expert_from_procedure, params: { procedure_id: procedure.id, id: expert_procedure.id } + delete :destroy, params: { procedure_id: procedure.id, id: expert_procedure.id } end before do From c82c72402d517011865d31cb4ddfbca3c0ba2d51 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 6 Apr 2021 11:55:43 +0000 Subject: [PATCH 3/4] app: move the experts list to ExpertsProceduresController --- .../experts_procedures_controller.rb | 18 ++++++++++++++++-- .../procedures_controller.rb | 11 +---------- .../index.html.haml} | 0 .../procedures/show.html.haml | 2 +- config/routes.rb | 3 +-- .../experts_procedures_controller_spec.rb | 19 +++++++++++++++---- .../index.html.haml_spec.rb} | 2 +- 7 files changed, 35 insertions(+), 20 deletions(-) rename app/views/new_administrateur/{procedures/invited_expert_list.html.haml => experts_procedures/index.html.haml} (100%) rename spec/views/new_administrateur/{procedures/invited_expert_list.html.haml_spec.rb => experts_procedures/index.html.haml_spec.rb} (93%) diff --git a/app/controllers/new_administrateur/experts_procedures_controller.rb b/app/controllers/new_administrateur/experts_procedures_controller.rb index 89645ec5f..dada4d873 100644 --- a/app/controllers/new_administrateur/experts_procedures_controller.rb +++ b/app/controllers/new_administrateur/experts_procedures_controller.rb @@ -2,6 +2,14 @@ module NewAdministrateur class ExpertsProceduresController < AdministrateurController before_action :retrieve_procedure + def index + @experts_procedure = @procedure + .experts_procedures + .where(revoked_at: nil) + .sort_by { |expert_procedure| expert_procedure.expert.email } + @experts_emails = experts_procedure_emails + end + def create emails = params['emails'].presence || [].to_json emails = JSON.parse(emails).map(&:strip).map(&:downcase) @@ -29,7 +37,7 @@ module NewAdministrateur value: valid_users.map(&:email).join(', '), procedure: @procedure.id) end - redirect_to admin_procedure_invited_expert_list_path(@procedure) + redirect_to admin_procedure_experts_path(@procedure) end def destroy @@ -37,7 +45,13 @@ module NewAdministrateur expert_email = expert_procedure.expert.email expert_procedure.update!(revoked_at: Time.zone.now) flash[:notice] = "#{expert_email} a été révoqué de la démarche et ne pourra plus déposer d'avis." - redirect_to admin_procedure_invited_expert_list_path(@procedure) + redirect_to admin_procedure_experts_path(@procedure) + end + + private + + def experts_procedure_emails + @procedure.experts.map(&:email).sort end end end diff --git a/app/controllers/new_administrateur/procedures_controller.rb b/app/controllers/new_administrateur/procedures_controller.rb index 212878085..637b6d3ad 100644 --- a/app/controllers/new_administrateur/procedures_controller.rb +++ b/app/controllers/new_administrateur/procedures_controller.rb @@ -1,6 +1,6 @@ module NewAdministrateur class ProceduresController < AdministrateurController - before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :allow_expert_review, :invited_expert_list, :update_allow_decision_access] + before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :allow_expert_review, :update_allow_decision_access] before_action :procedure_locked?, only: [:champs, :annotations] ITEMS_PER_PAGE = 25 @@ -185,11 +185,6 @@ module NewAdministrateur end end - def invited_expert_list - @experts_procedure = @procedure.experts_procedures.where(revoked_at: nil).sort_by { |expert_procedure| expert_procedure.expert.email } - @experts_emails = experts_procedure_emails - end - def update_allow_decision_access @procedure .experts_procedures @@ -199,10 +194,6 @@ module NewAdministrateur private - def experts_procedure_emails - @procedure.experts.map(&:email).sort - end - def apercu_tab params[:tab] || 'dossier' end diff --git a/app/views/new_administrateur/procedures/invited_expert_list.html.haml b/app/views/new_administrateur/experts_procedures/index.html.haml similarity index 100% rename from app/views/new_administrateur/procedures/invited_expert_list.html.haml rename to app/views/new_administrateur/experts_procedures/index.html.haml diff --git a/app/views/new_administrateur/procedures/show.html.haml b/app/views/new_administrateur/procedures/show.html.haml index 83a5ca28f..6f362e623 100644 --- a/app/views/new_administrateur/procedures/show.html.haml +++ b/app/views/new_administrateur/procedures/show.html.haml @@ -167,7 +167,7 @@ %p.card-admin-subtitle Liste des experts invités par les instructeurs .card-admin-action - = link_to "Voir", admin_procedure_invited_expert_list_path(@procedure), class: 'button' + = link_to "Voir", admin_procedure_experts_path(@procedure), class: 'button' .card-admin diff --git a/config/routes.rb b/config/routes.rb index 41180668f..ee2b37e7b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -406,7 +406,6 @@ Rails.application.routes.draw do put 'publish' => 'procedures#publish', as: :publish get 'transfert' => 'procedures#transfert', as: :transfert post 'transfer' => 'procedures#transfer', as: :transfer - get 'invited_expert_list' put 'update_allow_decision_access' => 'procedures#update_allow_decision_access', as: :update_allow_decision_access resources :mail_templates, only: [:edit, :update] @@ -425,7 +424,7 @@ Rails.application.routes.draw do resources :administrateurs, controller: 'procedure_administrateurs', only: [:index, :create, :destroy] - resources :experts, controller: 'experts_procedures', only: [:create, :destroy] + resources :experts, controller: 'experts_procedures', only: [:index, :create, :destroy] resources :types_de_champ, only: [:create, :update, :destroy] do member do diff --git a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb index 88a8d38fc..48c85f043 100644 --- a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb @@ -1,12 +1,24 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do let(:admin) { create(:administrateur) } + let(:procedure) { create :procedure, administrateur: admin } before do sign_in(admin.user) end + describe '#index' do + subject do + get :index, params: { procedure_id: procedure.id } + end + + before do + subject + end + + it { expect(response.status).to eq 200 } + end + describe '#create' do - let(:procedure) { create :procedure, administrateur: admin } let(:expert) { create(:expert) } let(:expert2) { create(:expert) } @@ -25,12 +37,11 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do it { expect(procedure.experts.include?(expert)).to be_truthy } it { expect(procedure.experts.include?(expert2)).to be_truthy } it { expect(flash.notice).to be_present } - it { expect(response).to redirect_to(admin_procedure_invited_expert_list_path(procedure)) } + it { expect(response).to redirect_to(admin_procedure_experts_path(procedure)) } end end describe '#delete' do - let(:procedure) { create :procedure, administrateur: admin } let(:expert) { create(:expert) } let(:expert_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } @@ -46,7 +57,7 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do context 'of multiple experts' do it { expect(expert_procedure.revoked_at).to be_present } it { expect(flash.notice).to be_present } - it { expect(response).to redirect_to(admin_procedure_invited_expert_list_path(procedure)) } + it { expect(response).to redirect_to(admin_procedure_experts_path(procedure)) } end end end diff --git a/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb b/spec/views/new_administrateur/experts_procedures/index.html.haml_spec.rb similarity index 93% rename from spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb rename to spec/views/new_administrateur/experts_procedures/index.html.haml_spec.rb index c21d87f40..b4a34df12 100644 --- a/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb +++ b/spec/views/new_administrateur/experts_procedures/index.html.haml_spec.rb @@ -1,4 +1,4 @@ -describe 'new_administrateur/procedures/invited_expert_list.html.haml', type: :view do +describe 'new_administrateur/experts_procedures/index.html.haml', type: :view do let!(:procedure) { create(:procedure, :published) } before do From 62051c926aa444cc45465cbe24e5c6766641ad27 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 6 Apr 2021 14:25:41 +0200 Subject: [PATCH 4/4] app: move update_allow_decision_access to ExpertsController --- .../experts_procedures_controller.rb | 11 +++++++ .../procedures_controller.rb | 9 +----- .../experts_procedures/index.html.haml | 2 +- config/routes.rb | 3 +- .../experts_procedures_controller_spec.rb | 25 ++++++++++++++++ .../procedures_controller_spec.rb | 30 ------------------- 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/app/controllers/new_administrateur/experts_procedures_controller.rb b/app/controllers/new_administrateur/experts_procedures_controller.rb index dada4d873..3aae682d3 100644 --- a/app/controllers/new_administrateur/experts_procedures_controller.rb +++ b/app/controllers/new_administrateur/experts_procedures_controller.rb @@ -40,6 +40,13 @@ module NewAdministrateur redirect_to admin_procedure_experts_path(@procedure) end + def update + @procedure + .experts_procedures + .find(params[:id]) + .update!(expert_procedure_params) + end + def destroy expert_procedure = ExpertsProcedure.find_by!(procedure: @procedure, id: params[:id]) expert_email = expert_procedure.expert.email @@ -53,5 +60,9 @@ module NewAdministrateur def experts_procedure_emails @procedure.experts.map(&:email).sort end + + def expert_procedure_params + params.require(:experts_procedure).permit(:allow_decision_access) + end end end diff --git a/app/controllers/new_administrateur/procedures_controller.rb b/app/controllers/new_administrateur/procedures_controller.rb index 637b6d3ad..4211fdcd7 100644 --- a/app/controllers/new_administrateur/procedures_controller.rb +++ b/app/controllers/new_administrateur/procedures_controller.rb @@ -1,6 +1,6 @@ module NewAdministrateur class ProceduresController < AdministrateurController - before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :allow_expert_review, :update_allow_decision_access] + before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :allow_expert_review] before_action :procedure_locked?, only: [:champs, :annotations] ITEMS_PER_PAGE = 25 @@ -185,13 +185,6 @@ module NewAdministrateur end end - def update_allow_decision_access - @procedure - .experts_procedures - .find(params[:expert_procedure]) - .update!(allow_decision_access_params) - end - private def apercu_tab diff --git a/app/views/new_administrateur/experts_procedures/index.html.haml b/app/views/new_administrateur/experts_procedures/index.html.haml index 5e5e60f4a..bed3d724b 100644 --- a/app/views/new_administrateur/experts_procedures/index.html.haml +++ b/app/views/new_administrateur/experts_procedures/index.html.haml @@ -46,7 +46,7 @@ - if @procedure.feature_enabled?(:admin_affect_experts_to_avis) %td.text-center = form_for expert_procedure, - url: admin_procedure_update_allow_decision_access_path(expert_procedure: expert_procedure), + url: admin_procedure_expert_path(id: expert_procedure), remote: true, method: :put, authenticity_token: true, diff --git a/config/routes.rb b/config/routes.rb index ee2b37e7b..21499cd0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -406,7 +406,6 @@ Rails.application.routes.draw do put 'publish' => 'procedures#publish', as: :publish get 'transfert' => 'procedures#transfert', as: :transfert post 'transfer' => 'procedures#transfer', as: :transfer - put 'update_allow_decision_access' => 'procedures#update_allow_decision_access', as: :update_allow_decision_access resources :mail_templates, only: [:edit, :update] resources :groupe_instructeurs, only: [:index, :show, :create, :update, :destroy] do @@ -424,7 +423,7 @@ Rails.application.routes.draw do resources :administrateurs, controller: 'procedure_administrateurs', only: [:index, :create, :destroy] - resources :experts, controller: 'experts_procedures', only: [:index, :create, :destroy] + resources :experts, controller: 'experts_procedures', only: [:index, :create, :update, :destroy] resources :types_de_champ, only: [:create, :update, :destroy] do member do diff --git a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb index 48c85f043..8f5162ea0 100644 --- a/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/experts_procedures_controller_spec.rb @@ -41,6 +41,31 @@ describe NewAdministrateur::ExpertsProceduresController, type: :controller do end end + describe '#update' do + let(:expert) { create(:expert) } + let(:expert_procedure) { create(:experts_procedure, procedure: procedure, expert: expert) } + + subject do + put :update, params: { + id: expert_procedure.id, + procedure_id: procedure.id, + experts_procedure: { + allow_decision_access: true + } + }, format: :js + end + + before do + subject + end + + it 'updates the record' do + expect(expert_procedure.allow_decision_access).to be false + subject + expect(expert_procedure.reload.allow_decision_access).to be true + end + end + describe '#delete' do let(:expert) { create(:expert) } let(:expert_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } diff --git a/spec/controllers/new_administrateur/procedures_controller_spec.rb b/spec/controllers/new_administrateur/procedures_controller_spec.rb index 9dfaf588a..e508faba7 100644 --- a/spec/controllers/new_administrateur/procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/procedures_controller_spec.rb @@ -532,34 +532,4 @@ describe NewAdministrateur::ProceduresController, type: :controller do it { expect(procedure.allow_expert_review).to be_truthy } end end - - describe 'PUT #update_allow_decision_access' do - let!(:procedure) { create :procedure, :with_service, administrateur: admin } - let(:expert) { create(:expert) } - let(:expert_procedure) { create(:experts_procedure, procedure: procedure, expert: expert) } - - subject do - put :update_allow_decision_access, params: { procedure_id: procedure.id, experts_procedure: { allow_decision_access: !expert_procedure.allow_decision_access }, expert_procedure: expert_procedure }, format: :js - end - - context 'when the experts_procedure is true' do - let(:expert_procedure) { create(:experts_procedure, procedure: procedure, expert: expert, allow_decision_access: true) } - - before do - subject - expert_procedure.reload - end - - it { expect(expert_procedure.allow_decision_access).to be_falsy } - end - - context 'when the experts_procedure is false' do - before do - subject - expert_procedure.reload - end - - it { expect(expert_procedure.allow_decision_access).to be_truthy } - end - end end