From f1595d1f50b698903454609f44fe436aeb89c893 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Fri, 16 Dec 2022 15:24:05 +0100 Subject: [PATCH] wrap resultset tuples in ProcedureDetail --- .../administrateurs/procedures_controller.rb | 24 ++--------- app/models/procedure_detail.rb | 9 +++++ .../administrateurs/procedures/all.html.haml | 10 ++--- .../procedures_controller_spec.rb | 40 ++++++++----------- 4 files changed, 34 insertions(+), 49 deletions(-) create mode 100644 app/models/procedure_detail.rb diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index f8b389eb9..35aa38bbd 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -342,7 +342,7 @@ module Administrateurs def all @filter = ProceduresFilter.new(current_administrateur, params) - all_procedures = filter_procedures(@filter) + all_procedures = filter_procedures(@filter).map { |p| ProcedureDetail.new(p) } respond_to do |format| format.html do @@ -350,11 +350,8 @@ module Administrateurs @procedures = all_procedures.page(params[:page]).per(25) end format.xlsx do - render xlsx: SpreadsheetArchitect.to_xlsx( - headers: export_procedures_headers(all_procedures), - data: export_procedures_values(all_procedures) - ), - filename: "demarches-#{@filter}" + render xlsx: ProcedureDetail.to_xlsx(instances: all_procedures), + filename: "demarches-#{@filter}" end end end @@ -369,21 +366,6 @@ module Administrateurs private - def export_procedures_headers(all_procedures) - all_procedures.to_a.first.keys.map do |key| - I18n.t(key, scope: 'activerecord.attributes.procedure_export') - end - end - - def export_procedures_values(all_procedures) - aasm_state_index = all_procedures.to_a.first.keys.index("aasm_state") - all_procedures.to_a.map(&:values).each do |procedure| - procedure.tap do |p| - p[aasm_state_index] = I18n.t(p[aasm_state_index], scope: 'activerecord.attributes.procedure.aasm_state') - end - end - end - def filter_procedures(filter) procedures_result = Procedure.select(:id).joins(:procedures_zones).distinct.publiees_ou_closes procedures_result = procedures_result.where(procedures_zones: { zone_id: filter.zone_ids }) if filter.zone_ids.present? diff --git a/app/models/procedure_detail.rb b/app/models/procedure_detail.rb new file mode 100644 index 000000000..c4ff435a2 --- /dev/null +++ b/app/models/procedure_detail.rb @@ -0,0 +1,9 @@ +class ProcedureDetail < OpenStruct + include SpreadsheetArchitect + + def spreadsheet_columns + [:id, :libelle, :published_at, :aasm_state, :admin_count].map do |attribute| + [I18n.t(attribute, scope: 'activerecord.attributes.procedure_export'), attribute] + end + end +end diff --git a/app/views/administrateurs/procedures/all.html.haml b/app/views/administrateurs/procedures/all.html.haml index 69be1186e..7e9f0e29e 100644 --- a/app/views/administrateurs/procedures/all.html.haml +++ b/app/views/administrateurs/procedures/all.html.haml @@ -46,9 +46,9 @@ %td = button_to detail_admin_procedure_path(procedure["id"]), params: { show_detail: true}, method: :get, title: 'Afficher details', class: "fr-icon-add-line fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline" do Afficher details procedure - %td= procedure["libelle"] - %td= procedure["id"] - %td= procedure["admin_count"] - %td= t procedure["aasm_state"], scope: 'activerecord.values.procedure.aasm_state' - %td= l(procedure["published_at"], format: :message_date_without_time) + %td= procedure.libelle + %td= procedure.id + %td= procedure.admin_count + %td= t procedure.aasm_state, scope: 'activerecord.values.procedure.aasm_state' + %td= l(procedure.published_at, format: :message_date_without_time) .fr-mt-2w= paginate @procedures, views_prefix: 'administrateurs' diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index b5cce4ebf..fadbbcf1d 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -99,21 +99,21 @@ describe Administrateurs::ProceduresController, type: :controller do subject { get :all, format: :xlsx } it 'exports result in xlsx' do - allow(SpreadsheetArchitect).to receive(:to_xlsx) + allow(ProcedureDetail).to receive(:to_xlsx) subject - expect(SpreadsheetArchitect).to have_received(:to_xlsx) + expect(ProcedureDetail).to have_received(:to_xlsx) end end it 'display published or closed procedures' do subject - expect(values_for_field(assigns(:procedures), "id")).to include(published_procedure.id) - expect(values_for_field(assigns(:procedures), "id")).to include(closed_procedure.id) + expect(assigns(:procedures).any? { |p| p.id == published_procedure.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == closed_procedure.id }).to be_truthy end it 'doesn’t display draft procedures' do subject - expect(values_for_field(assigns(:procedures), "id")).not_to include(draft_procedure.id) + expect(assigns(:procedures).any? { |p| p.id == draft_procedure.id }).to be_falsey end context "for specific zones" do @@ -126,8 +126,8 @@ describe Administrateurs::ProceduresController, type: :controller do it 'display only procedures for specified zones' do subject - expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id) - expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id) + expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey end end @@ -137,14 +137,14 @@ describe Administrateurs::ProceduresController, type: :controller do it 'display only published procedures' do get :all, params: { statuses: ['publiee'] } - expect(values_for_field(assigns(:procedures), "id")).to include(procedure1.id) - expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure2.id) + expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_falsey end it 'display only closed procedures' do get :all, params: { statuses: ['close'] } - expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id) - expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id) + expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey end end @@ -156,9 +156,9 @@ describe Administrateurs::ProceduresController, type: :controller do it 'display only procedures published after specific date' do get :all, params: { from_publication_date: after } - expect(values_for_field(assigns(:procedures), "id")).to include(procedure1.id) - expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id) - expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure3.id) + expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure3.id }).to be_falsey end end @@ -169,9 +169,9 @@ describe Administrateurs::ProceduresController, type: :controller do it 'returns procedures with specific terms in libelle' do get :all, params: { libelle: 'entrepreneur' } - expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id) - expect(values_for_field(assigns(:procedures), "id")).to include(procedure3.id) - expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id) + expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure3.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey end end end @@ -954,9 +954,3 @@ describe Administrateurs::ProceduresController, type: :controller do end end end - -def values_for_field(array, field) - array.map do |procedure| - procedure[field] - end -end