From b0a9e49549abb9f55112a359fc10b871393297d8 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 3 Jul 2018 20:18:49 +0200 Subject: [PATCH] procedures: group new_from_existing list by organization As procedures are not grouped by Service (which are unique to an administrator) but by organization, this allows procedures with the same organization name to be displayed together. --- app/controllers/admin/procedures_controller.rb | 4 ++-- app/models/procedure.rb | 4 ++++ .../admin/procedures/new_from_existing.html.haml | 2 +- .../admin/procedures_controller_spec.rb | 14 ++++++++++++++ spec/models/procedure_spec.rb | 13 +++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index ac5cc6a9a..a4431cbd7 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -186,8 +186,8 @@ class Admin::ProceduresController < AdminController @grouped_procedures = Procedure .where(id: significant_procedure_ids) - .group_by(&:administrateur) - .sort_by { |a, _| a.created_at } + .group_by(&:organisation_name) + .sort_by { |_, procedures| procedures.first.created_at } end def active_class diff --git a/app/models/procedure.rb b/app/models/procedure.rb index f7d7b8a6f..769742d66 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -152,6 +152,10 @@ class Procedure < ApplicationRecord libelle.parameterize.first(50) end + def organisation_name + service&.nom || organisation + end + def types_de_champ_ordered types_de_champ.order(:order_place) end diff --git a/app/views/admin/procedures/new_from_existing.html.haml b/app/views/admin/procedures/new_from_existing.html.haml index 3935e0722..a7996800a 100644 --- a/app/views/admin/procedures/new_from_existing.html.haml +++ b/app/views/admin/procedures/new_from_existing.html.haml @@ -12,7 +12,7 @@ %br - @grouped_procedures.each do |_, procedures| %b - = procedures.first.organisation + = procedures.first.organisation_name %table{ style: 'margin-bottom: 40px;' } - procedures.sort_by(&:id).each do |procedure| %tr{ style: 'height: 36px;' } diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 6fe5ebcf2..3a1f16831 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -553,6 +553,20 @@ describe Admin::ProceduresController, type: :controller 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 #path_list' do diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 7ff08b45c..010c57760 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -735,6 +735,19 @@ describe Procedure do it { expect(Champ.count).to eq(0) } end + describe "#organisation_name" do + subject { procedure.organisation_name } + context 'when the procedure has a service (and no organization)' do + let(:procedure) { create(:procedure, :with_service, organisation: nil) } + it { is_expected.to eq procedure.service.nom } + end + + context 'when the procedure has an organization (and no service)' do + let(:procedure) { create(:procedure, organisation: 'DDT des Vosges', service: nil) } + it { is_expected.to eq procedure.organisation } + end + end + describe '#juridique_required' do it 'automatically jumps to true once cadre_juridique or deliberation have been set' do p = create(