From 973570332f8917573eb325792e3a162f895f1b5e Mon Sep 17 00:00:00 2001 From: JC Date: Wed, 9 Nov 2016 09:19:57 +0100 Subject: [PATCH] Implemented custom method to catch procedure_id while rendering data on each request format --- .../backoffice/dossiers_controller.rb | 13 ++++++++----- app/models/dossier.rb | 19 +++++++++++++++++-- app/views/backoffice/dossiers/index.html.haml | 6 +++--- spec/models/dossier_spec.rb | 6 +++--- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 33d2297c6..3dc04f42f 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -20,11 +20,14 @@ class Backoffice::DossiersController < Backoffice::DossiersListController def download_dossiers_tps dossiers = current_gestionnaire.dossiers.where.not(state: :draft) - - respond_to do |format| - format.xlsx { render xlsx: dossiers } - format.ods { render ods: dossiers } - format.csv { render csv: dossiers } + if procedure = Procedure.find_by(id: params[:procedure_id]) + respond_with Dossier.export_columns_and_procedure(dossiers, request.format, procedure) + else + respond_to do |format| + format.xlsx { render xlsx: dossiers } + format.ods { render ods: dossiers } + format.csv { render csv: dossiers } + end end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fa388fe88..21b6629e4 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -303,7 +303,7 @@ class Dossier < ActiveRecord::Base return hash end - def as_csv(options={}) + def export_default_columns dossier_attr = DossierSerializer.new(self).attributes dossier_attr = convert_specific_values_to_string(dossier_attr) unless entreprise.nil? @@ -319,7 +319,22 @@ class Dossier < ActiveRecord::Base end def spreadsheet_columns - self.as_csv.to_a + self.export_default_columns.to_a + end + + def self.export_columns_and_procedure(dossiers, format, procedure) + data = [] + headers = dossiers.first.export_default_columns.keys + dossiers.each do |dossier| + data << dossier.export_default_columns.values + end + if ["csv"].include?(format) + return SpreadsheetArchitect.to_csv(data: data, headers: headers) + elsif ["xlsx"].include?(format) + return SpreadsheetArchitect.to_xlsx(data: data, headers: headers) + elsif ["ods"].include?(format) + return SpreadsheetArchitect.to_ods(data: data, headers: headers) + end end def reset! diff --git a/app/views/backoffice/dossiers/index.html.haml b/app/views/backoffice/dossiers/index.html.haml index 67257a9c2..8629c2a79 100644 --- a/app/views/backoffice/dossiers/index.html.haml +++ b/app/views/backoffice/dossiers/index.html.haml @@ -12,13 +12,13 @@ %span.caret %ul.dropdown-menu.dropdown-menu-right %li - = link_to backoffice_download_dossiers_tps_path(format: :csv), { class: 'btn btn-sm' } do + = link_to backoffice_download_dossiers_tps_path(format: :csv, procedure_id: params[:id]), { class: 'btn btn-sm' } do = t('dynamics.backoffice.format_csv') %li - = link_to backoffice_download_dossiers_tps_path(format: :xlsx), { class: 'btn btn-sm' } do + = link_to backoffice_download_dossiers_tps_path(format: :xlsx, procedure_id: params[:id]), { class: 'btn btn-sm' } do = t('dynamics.backoffice.format_xlsx') %li - = link_to backoffice_download_dossiers_tps_path(format: :ods), { class: 'btn btn-sm' } do + = link_to backoffice_download_dossiers_tps_path(format: :ods, procedure_id: params[:id]), { class: 'btn btn-sm' } do = t('dynamics.backoffice.format_ods') = render partial: 'backoffice/dossiers/onglets' diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 9ee562670..7630c20e3 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -659,10 +659,10 @@ describe Dossier do end end - describe '#as_csv?' do + describe '#export_default_columns?' do let(:procedure) { create(:procedure) } let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } - subject { dossier.as_csv } + subject { dossier.export_default_columns } it { expect(subject[:archived]).to eq('false') } it { expect(subject[:etablissement_siret]).to eq('44011762001530') } @@ -692,7 +692,7 @@ describe Dossier do context 'when dossier does not have enterprise' do let(:dossier) { create(:dossier, user: user, procedure: procedure) } - subject { dossier.as_csv } + subject { dossier.export_default_columns } it { expect(subject[:archived]).to eq('false') } end