Implemented custom method to catch procedure_id while rendering data on each request format

This commit is contained in:
JC 2016-11-09 09:19:57 +01:00
parent 23ee1cb21c
commit 973570332f
4 changed files with 31 additions and 13 deletions

View file

@ -20,13 +20,16 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
def download_dossiers_tps
dossiers = current_gestionnaire.dossiers.where.not(state: :draft)
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
def search
@search_terms = params[:q]

View file

@ -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!

View file

@ -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'

View file

@ -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