Merge pull request #67 from sgmap/move_export_full_generation_to_procedure

Move export_full_generation method from Dossier to Procedure
This commit is contained in:
gregoirenovel 2017-04-05 13:40:23 +02:00 committed by GitHub
commit 65ac1da73a
5 changed files with 40 additions and 40 deletions

View file

@ -40,7 +40,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
def download_dossiers_tps
procedure = Procedure.find_by(id: params[:procedure_id])
export = Dossier.export_full_generation(procedure.dossiers.downloadable)
export = procedure.generate_export
respond_to do |format|
format.csv { send_data(SpreadsheetArchitect.to_csv(data: export[:data], headers: export[:headers]), filename: 'dossiers.csv') }

View file

@ -263,23 +263,6 @@ class Dossier < ActiveRecord::Base
return headers
end
def self.export_full_generation(dossiers)
data = []
headers = []
if dossiers && dossiers.any?
headers = dossiers.first.export_headers
dossiers.each do |dossier|
data << dossier.convert_specific_array_values_to_string(dossier.data_with_champs)
end
end
return {
data: data,
headers: headers
}
end
def followers_gestionnaires_emails
follows.includes(:gestionnaire).map { |f| f.gestionnaire }.pluck(:email).join(' ')
end

View file

@ -130,4 +130,18 @@ class Procedure < ActiveRecord::Base
self.dossiers.where.not(state: :draft).size
end
def generate_export
exportable_dossiers = dossiers.downloadable
headers = exportable_dossiers.any? ? exportable_dossiers.first.export_headers : []
data = exportable_dossiers.map do |dossier|
dossier.convert_specific_array_values_to_string(dossier.data_with_champs)
end
{
headers: headers,
data: data
}
end
end

View file

@ -603,7 +603,6 @@ describe Dossier do
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, follows: [follow], initiated_at: date1, received_at: date2, processed_at: date3) }
describe '#export_headers' do
subject { dossier.export_headers }
it { expect(subject).to include(:description) }
@ -625,27 +624,6 @@ describe Dossier do
it { expect(subject[9]).to eq(dossier.followers_gestionnaires_emails) }
it { expect(subject.count).to eq(DossierProcedureSerializer.new(dossier).attributes.count + dossier.procedure.types_de_champ.count + dossier.export_entreprise_data.count) }
end
describe '.export_full_generation' do
context 'when there are no dossiers' do
subject { Dossier.export_full_generation(Dossier.none) }
it { expect(subject[:data]).to eq([]) }
it { expect(subject[:headers]).to eq([]) }
end
context 'when there are some dossiers' do
let!(:dossier){ create(:dossier) }
let!(:dossier2){ create(:dossier) }
subject { Dossier.export_full_generation(Dossier.all) }
it { expect(subject[:data].size).to eq(2) }
it { expect(subject[:headers]).to eq(dossier.export_headers) }
end
end
end
describe '#Dossier.to_csv' do

View file

@ -256,4 +256,29 @@ describe Procedure do
it { is_expected.to eq 2 }
end
describe '#generate_export' do
let(:procedure) { create :procedure }
subject { procedure.generate_export }
context 'when there are no dossiers' do
it { expect(subject[:data]).to eq([]) }
it { expect(subject[:headers]).to eq([]) }
end
context 'when there are some dossiers' do
let!(:dossier){ create(:dossier, procedure: procedure, state: 'initiated') }
let!(:dossier2){ create(:dossier, procedure: procedure, state: 'closed') }
it { expect(subject[:data].size).to eq(2) }
it { expect(subject[:headers]).to eq(dossier.export_headers) }
end
context 'when there is a draft dossier' do
let!(:dossier_not_exportable){ create(:dossier, procedure: procedure, state: 'draft') }
it { expect(subject[:data]).to eq([]) }
it { expect(subject[:headers]).to eq([]) }
end
end
end