From 3e83ad454f2153ac71959b96ae9f1aa0cb350deb Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 31 Aug 2021 16:44:23 -0500 Subject: [PATCH] app: move new_from_existing to NewAdministrateur::ProceduresController --- .../admin/procedures_controller.rb | 18 -------- .../procedures_controller.rb | 17 +++++++ .../procedures/new_from_existing.html.haml | 0 config/routes.rb | 8 ++-- .../admin/procedures_controller_spec.rb | 44 ------------------- .../procedures_controller_spec.rb | 44 +++++++++++++++++++ 6 files changed, 65 insertions(+), 66 deletions(-) rename app/views/{admin => new_administrateur}/procedures/new_from_existing.html.haml (100%) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index b5f4b0497..0c0f5b477 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -33,24 +33,6 @@ class Admin::ProceduresController < AdminController redirect_to admin_procedures_path end - SIGNIFICANT_DOSSIERS_THRESHOLD = 30 - - def new_from_existing - significant_procedure_ids = Procedure - .publiees_ou_closes - .joins(:dossiers) - .group("procedures.id") - .having("count(dossiers.id) >= ?", SIGNIFICANT_DOSSIERS_THRESHOLD) - .pluck('procedures.id') - - @grouped_procedures = Procedure - .includes(:administrateurs, :service) - .where(id: significant_procedure_ids) - .group_by(&:organisation_name) - .sort_by { |_, procedures| procedures.first.created_at } - render layout: 'application' - end - private def cloned_from_library? diff --git a/app/controllers/new_administrateur/procedures_controller.rb b/app/controllers/new_administrateur/procedures_controller.rb index 1e5a07877..14f7f3ee1 100644 --- a/app/controllers/new_administrateur/procedures_controller.rb +++ b/app/controllers/new_administrateur/procedures_controller.rb @@ -52,6 +52,23 @@ module NewAdministrateur @procedure ||= Procedure.new(for_individual: true) end + SIGNIFICANT_DOSSIERS_THRESHOLD = 30 + + def new_from_existing + significant_procedure_ids = Procedure + .publiees_ou_closes + .joins(:dossiers) + .group("procedures.id") + .having("count(dossiers.id) >= ?", SIGNIFICANT_DOSSIERS_THRESHOLD) + .pluck('procedures.id') + + @grouped_procedures = Procedure + .includes(:administrateurs, :service) + .where(id: significant_procedure_ids) + .group_by(&:organisation_name) + .sort_by { |_, procedures| procedures.first.created_at } + end + def show @procedure = current_administrateur.procedures.find(params[:id]) @current_administrateur = current_administrateur diff --git a/app/views/admin/procedures/new_from_existing.html.haml b/app/views/new_administrateur/procedures/new_from_existing.html.haml similarity index 100% rename from app/views/admin/procedures/new_from_existing.html.haml rename to app/views/new_administrateur/procedures/new_from_existing.html.haml diff --git a/config/routes.rb b/config/routes.rb index 8a2f86424..bbbca2581 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -194,10 +194,6 @@ Rails.application.routes.draw do get 'procedures/draft', to: redirect('/admin/procedures?statut=brouillons') resources :procedures, only: [] do - collection do - get 'new_from_existing' => 'procedures#new_from_existing', as: :new_from_existing - end - put 'archive' => 'procedures#archive', as: :archive put 'clone' => 'procedures#clone', as: :clone end @@ -392,6 +388,10 @@ Rails.application.routes.draw do namespace :admin, module: 'new_administrateur' do resources :procedures, except: [:destroy] do + collection do + get 'new_from_existing' + end + member do get 'apercu' get 'champs' diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index b27f0c583..7958f1787 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -177,48 +177,4 @@ describe Admin::ProceduresController, type: :controller do end end end - - describe 'GET #new_from_existing' do - before do - stub_const("Admin::ProceduresController::SIGNIFICANT_DOSSIERS_THRESHOLD", 2) - end - - subject { get :new_from_existing } - let(:grouped_procedures) { subject; assigns(:grouped_procedures) } - let(:response_procedures) { grouped_procedures.map { |_o, procedures| procedures }.flatten } - - describe 'selecting' do - let!(:large_draft_procedure) { create(:procedure_with_dossiers, dossiers_count: 2) } - let!(:large_published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } - let!(:large_closed_procedure) { create(:procedure_with_dossiers, :closed, dossiers_count: 2) } - let!(:small_closed_procedure) { create(:procedure_with_dossiers, :closed, dossiers_count: 1) } - - it 'displays published and closed procedures' do - expect(response_procedures).to include(large_published_procedure) - expect(response_procedures).to include(large_closed_procedure) - end - - it 'doesn’t display procedures without a significant number of dossiers' do - expect(response_procedures).not_to include(small_closed_procedure) - end - - it 'doesn’t display draft procedures' do - expect(response_procedures).not_to include(large_draft_procedure) - end - end - - describe 'grouping' do - let(:service_1) { create(:service, nom: 'DDT des Vosges') } - let(:service_2) { create(:service, nom: 'DDT du Loiret') } - let!(:procedure_with_service_1) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_1, dossiers_count: 2) } - let!(:procedure_with_service_2) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_2, dossiers_count: 2) } - let!(:procedure_without_service) { create(:procedure_with_dossiers, :published, organisation: 'DDT du Loiret', dossiers_count: 2) } - - it 'groups procedures with services as well as procedures with organisations' do - expect(grouped_procedures.length).to eq 2 - expect(grouped_procedures.find { |o, _p| o == 'DDT des Vosges' }.last).to contain_exactly(procedure_with_service_1) - expect(grouped_procedures.find { |o, _p| o == 'DDT du Loiret' }.last).to contain_exactly(procedure_with_service_2, procedure_without_service) - end - end - end end diff --git a/spec/controllers/new_administrateur/procedures_controller_spec.rb b/spec/controllers/new_administrateur/procedures_controller_spec.rb index 6dfe388fb..eec73170c 100644 --- a/spec/controllers/new_administrateur/procedures_controller_spec.rb +++ b/spec/controllers/new_administrateur/procedures_controller_spec.rb @@ -64,6 +64,50 @@ describe NewAdministrateur::ProceduresController, type: :controller do it { expect(subject.status).to eq(200) } end + describe 'GET #new_from_existing' do + before do + stub_const("NewAdministrateur::ProceduresController::SIGNIFICANT_DOSSIERS_THRESHOLD", 2) + end + + subject { get :new_from_existing } + let(:grouped_procedures) { subject; assigns(:grouped_procedures) } + let(:response_procedures) { grouped_procedures.map { |_o, procedures| procedures }.flatten } + + describe 'selecting' do + let!(:large_draft_procedure) { create(:procedure_with_dossiers, dossiers_count: 2) } + let!(:large_published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } + let!(:large_closed_procedure) { create(:procedure_with_dossiers, :closed, dossiers_count: 2) } + let!(:small_closed_procedure) { create(:procedure_with_dossiers, :closed, dossiers_count: 1) } + + it 'displays published and closed procedures' do + expect(response_procedures).to include(large_published_procedure) + expect(response_procedures).to include(large_closed_procedure) + end + + it 'doesn’t display procedures without a significant number of dossiers' do + expect(response_procedures).not_to include(small_closed_procedure) + end + + it 'doesn’t display draft procedures' do + expect(response_procedures).not_to include(large_draft_procedure) + end + end + + describe 'grouping' do + let(:service_1) { create(:service, nom: 'DDT des Vosges') } + let(:service_2) { create(:service, nom: 'DDT du Loiret') } + let!(:procedure_with_service_1) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_1, dossiers_count: 2) } + let!(:procedure_with_service_2) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_2, dossiers_count: 2) } + let!(:procedure_without_service) { create(:procedure_with_dossiers, :published, organisation: 'DDT du Loiret', dossiers_count: 2) } + + it 'groups procedures with services as well as procedures with organisations' do + expect(grouped_procedures.length).to eq 2 + expect(grouped_procedures.find { |o, _p| o == 'DDT des Vosges' }.last).to contain_exactly(procedure_with_service_1) + expect(grouped_procedures.find { |o, _p| o == 'DDT du Loiret' }.last).to contain_exactly(procedure_with_service_2, procedure_without_service) + end + end + end + describe 'GET #edit' do let(:published_at) { nil } let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }