app: move new_from_existing to NewAdministrateur::ProceduresController

This commit is contained in:
Pierre de La Morinerie 2021-08-31 16:44:23 -05:00
parent 7729385d89
commit 3e83ad454f
6 changed files with 65 additions and 66 deletions

View file

@ -33,24 +33,6 @@ class Admin::ProceduresController < AdminController
redirect_to admin_procedures_path redirect_to admin_procedures_path
end 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 private
def cloned_from_library? def cloned_from_library?

View file

@ -52,6 +52,23 @@ module NewAdministrateur
@procedure ||= Procedure.new(for_individual: true) @procedure ||= Procedure.new(for_individual: true)
end 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 def show
@procedure = current_administrateur.procedures.find(params[:id]) @procedure = current_administrateur.procedures.find(params[:id])
@current_administrateur = current_administrateur @current_administrateur = current_administrateur

View file

@ -194,10 +194,6 @@ Rails.application.routes.draw do
get 'procedures/draft', to: redirect('/admin/procedures?statut=brouillons') get 'procedures/draft', to: redirect('/admin/procedures?statut=brouillons')
resources :procedures, only: [] do 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 'archive' => 'procedures#archive', as: :archive
put 'clone' => 'procedures#clone', as: :clone put 'clone' => 'procedures#clone', as: :clone
end end
@ -392,6 +388,10 @@ Rails.application.routes.draw do
namespace :admin, module: 'new_administrateur' do namespace :admin, module: 'new_administrateur' do
resources :procedures, except: [:destroy] do resources :procedures, except: [:destroy] do
collection do
get 'new_from_existing'
end
member do member do
get 'apercu' get 'apercu'
get 'champs' get 'champs'

View file

@ -177,48 +177,4 @@ describe Admin::ProceduresController, type: :controller do
end end
end 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 'doesnt display procedures without a significant number of dossiers' do
expect(response_procedures).not_to include(small_closed_procedure)
end
it 'doesnt 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 end

View file

@ -64,6 +64,50 @@ describe NewAdministrateur::ProceduresController, type: :controller do
it { expect(subject.status).to eq(200) } it { expect(subject.status).to eq(200) }
end 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 'doesnt display procedures without a significant number of dossiers' do
expect(response_procedures).not_to include(small_closed_procedure)
end
it 'doesnt 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 describe 'GET #edit' do
let(:published_at) { nil } let(:published_at) { nil }
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) } let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }