Split methods for export, need to write 2 tests for headers and raw data (export_headers, data_with_champs)

This commit is contained in:
JC 2016-11-14 10:41:56 +01:00
parent a89a40df34
commit e49e0b02ef
3 changed files with 39 additions and 12 deletions

View file

@ -21,7 +21,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
def download_dossiers_tps def download_dossiers_tps
if procedure = Procedure.find_by(id: params[:procedure_id]) if procedure = Procedure.find_by(id: params[:procedure_id])
dossiers = DossiersListGestionnaireService.new(current_gestionnaire, nil, procedure).filter_dossiers dossiers = DossiersListGestionnaireService.new(current_gestionnaire, nil, procedure).filter_dossiers
respond_with Dossier.export_columns_and_procedure(dossiers, request.format) respond_with Dossier.export_full_generation(dossiers, request.format)
else else
dossiers = DossiersListGestionnaireService.new(current_gestionnaire, nil, nil).filter_dossiers dossiers = DossiersListGestionnaireService.new(current_gestionnaire, nil, nil).filter_dossiers
respond_to do |format| respond_to do |format|

View file

@ -325,19 +325,34 @@ class Dossier < ActiveRecord::Base
self.export_default_columns.to_a self.export_default_columns.to_a
end end
def self.export_columns_and_procedure(dossiers, format) def export_headers
serialized_dossier = DossierProcedureSerializer.new(self).as_json[:dossier_procedure]
champs = {}
serialized_dossier[:champs].each do |champ_hash|
champs[champ_hash[:type_de_champ]["libelle"].parameterize.underscore.to_sym] = champ_hash[:value]
end
dossier_data = serialized_dossier.except(:champs).merge(champs)
dossier_data = self.convert_specific_values_to_string(dossier_data)
return (dossier_data.keys << self.export_entreprise_data.keys).flatten
end
def data_with_champs
serialized_dossier = DossierProcedureSerializer.new(self).as_json[:dossier_procedure]
champs = {}
serialized_dossier[:champs].each do |champ_hash|
champs[champ_hash[:type_de_champ]["libelle"].parameterize.underscore.to_sym] = champ_hash[:value]
end
dossier_data = serialized_dossier.except(:champs).merge(champs)
dossier_data = self.convert_specific_values_to_string(dossier_data)
return (dossier_data.values << self.export_entreprise_data.values).flatten
end
def self.export_full_generation(dossiers, format)
data = [] data = []
headers = nil headers = []
dossiers.each do |dossier| dossiers.each do |dossier|
serialized_dossier = DossierProcedureSerializer.new(dossier).as_json[:dossier_procedure] headers = dossier.export_headers if headers.empty?
champs = {} data << dossier.data_with_champs
serialized_dossier[:champs].each do |champ_hash|
champs[champ_hash[:type_de_champ]["libelle"].parameterize.underscore.to_sym] = champ_hash[:value]
end
dossier_data = serialized_dossier.except(:champs).merge(champs)
dossier_data = dossier.convert_specific_values_to_string(dossier_data)
headers ||= (dossier_data.keys << dossier.export_entreprise_data.keys).flatten
data << (dossier_data.values << dossier.export_entreprise_data.values).flatten
end end
if ["csv"].include?(format) if ["csv"].include?(format)
return SpreadsheetArchitect.to_csv(data: data, headers: headers) return SpreadsheetArchitect.to_csv(data: data, headers: headers)

View file

@ -754,6 +754,18 @@ describe Dossier do
end end
end end
describe '#export_headers' do
let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) }
subject { dossier.export_headers }
it do
end
end
describe '#data_with_champs' do
end
describe '#Dossier.to_csv' do describe '#Dossier.to_csv' do
let!(:procedure) { create(:procedure) } let!(:procedure) { create(:procedure) }
let!(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } let!(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) }