feat(export.type): ensure right cast type on export. ods expecting boolean value a 0||1

This commit is contained in:
mfo 2024-11-12 17:34:22 +01:00
parent 8792cb3cfd
commit e563d81b7e
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
6 changed files with 50 additions and 21 deletions

View file

@ -87,13 +87,13 @@ module ColumnsConcern
def followers_instructeurs_email_column = dossier_col(table: 'followers_instructeurs', column: 'email')
def dossier_archived_column = dossier_col(table: 'self', column: 'archived', type: :text, displayable: false, filterable: false);
def dossier_archived_column = dossier_col(table: 'self', column: 'archived', type: :boolean, displayable: false, filterable: false);
def dossier_motivation_column = dossier_col(table: 'self', column: 'motivation', type: :text, displayable: false, filterable: false);
def user_email_for_display_column = dossier_col(table: 'self', column: 'user_email_for_display', filterable: false, displayable: false)
def user_france_connected_column = dossier_col(table: 'self', column: 'user_from_france_connect?', filterable: false, displayable: false)
def user_france_connected_column = dossier_col(table: 'self', column: 'user_from_france_connect?', type: :boolean, filterable: false, displayable: false)
def dossier_labels_column = dossier_col(table: 'dossier_labels', column: 'label_id', type: :enum, options_for_select: labels.map { [_1.name, _1.id] })
@ -140,7 +140,8 @@ module ColumnsConcern
def individual_columns
['gender', 'nom', 'prenom'].map { |column| dossier_col(table: 'individual', column:) }
.concat ['for_tiers', 'mandataire_last_name', 'mandataire_first_name'].map { |column| dossier_col(table: 'self', column:) }
.concat ['mandataire_last_name', 'mandataire_first_name'].map { |column| dossier_col(table: 'self', column:) }
.concat ['for_tiers'].map { |column| dossier_col(table: 'self', column:, type: :boolean) }
end
def moral_columns

View file

@ -4,24 +4,24 @@ module DossierExportConcern
extend ActiveSupport::Concern
def spreadsheet_columns_csv(types_de_champ:, export_template: nil)
spreadsheet_columns(with_etablissement: true, types_de_champ:, export_template:)
spreadsheet_columns(with_etablissement: true, types_de_champ:, export_template:, format: :csv)
end
def spreadsheet_columns_xlsx(types_de_champ:, export_template: nil)
spreadsheet_columns(types_de_champ:, export_template:)
spreadsheet_columns(types_de_champ:, export_template:, format: :xlsx)
end
def spreadsheet_columns_ods(types_de_champ:, export_template: nil)
spreadsheet_columns(types_de_champ:, export_template:)
spreadsheet_columns(types_de_champ:, export_template:, format: :ods)
end
def champ_values_for_export(types_de_champ, row_id: nil, export_template: nil)
def champ_values_for_export(types_de_champ, row_id: nil, export_template: nil, format:)
types_de_champ.flat_map do |type_de_champ|
champ = filled_champ(type_de_champ, row_id)
if export_template.present?
export_template
.columns_for_stable_id(type_de_champ.stable_id)
.map { |exported_column| exported_column.libelle_with_value(champ) }
.map { |exported_column| exported_column.libelle_with_value(champ, format:) }
else
type_de_champ.libelles_for_export.map do |(libelle, path)|
[libelle, type_de_champ.champ_value_for_export(champ, path)]
@ -30,15 +30,15 @@ module DossierExportConcern
end
end
def spreadsheet_columns(types_de_champ:, with_etablissement: false, export_template: nil)
dossier_values_for_export(with_etablissement:, export_template:) + champ_values_for_export(types_de_champ, export_template:)
def spreadsheet_columns(types_de_champ:, with_etablissement: false, export_template: nil, format: nil)
dossier_values_for_export(with_etablissement:, export_template:, format:) + champ_values_for_export(types_de_champ, export_template:, format:)
end
private
def dossier_values_for_export(with_etablissement: false, export_template: nil)
def dossier_values_for_export(with_etablissement: false, export_template: nil, format:)
if export_template.present?
return export_template.dossier_exported_columns.map { _1.libelle_with_value(self) }
return export_template.dossier_exported_columns.map { _1.libelle_with_value(self, format:) }
end
columns = [

View file

@ -10,7 +10,25 @@ class ExportedColumn
def id = { id: column.id, libelle: }.to_json
def libelle_with_value(champ_or_dossier)
[libelle, ExportedColumnFormatter.format(column:, champ_or_dossier:)]
def libelle_with_value(champ_or_dossier, format:)
[libelle, ExportedColumnFormatter.format(column:, champ_or_dossier:, format:), spreadsheet_architect_type]
end
# see: https://github.com/westonganger/spreadsheet_architect/blob/771e2e5558fbf6e0cb830e881a7214fa710e49c3/lib/spreadsheet_architect.rb#L39
def spreadsheet_architect_type
case @column.type
when :boolean
:boolean
when :decimal
:float
when :number
:integer
when :datetime
:time
when :date
:date
else
:string
end
end
end

View file

@ -1,12 +1,14 @@
# frozen_string_literal: true
class ExportedColumnFormatter
def self.format(column:, champ_or_dossier:)
def self.format(column:, champ_or_dossier:, format:)
return if champ_or_dossier.nil?
raw_value = column.value(champ_or_dossier)
case column.type
when :boolean
format_boolean(column:, raw_value:, format:)
when :attachements
format_attachments(column:, raw_value:)
when :enum
@ -20,6 +22,14 @@ class ExportedColumnFormatter
private
def self.format_boolean(column:, raw_value:, format:)
if format == :ods
raw_value ? 1 : 0
else
raw_value
end
end
def self.format_attachments(column:, raw_value:)
case column.tdc_type
when TypeDeChamp.type_champs[:titre_identite]

View file

@ -184,7 +184,7 @@ RSpec.describe DossierChampsConcern do
end
describe "#champ_values_for_export" do
subject { dossier.champ_values_for_export(dossier.revision.types_de_champ_public) }
subject { dossier.champ_values_for_export(dossier.revision.types_de_champ_public, format: :xlsx) }
it { expect(subject.size).to eq(4) }
it { expect(subject.first).to eq(["Un champ text", nil]) }

View file

@ -1993,7 +1993,7 @@ describe Dossier, type: :model do
expect {
integer_number_type_de_champ.update(type_champ: :decimal_number)
procedure.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision)
}.to change { dossier.reload.champ_values_for_export(procedure.all_revisions_types_de_champ.not_repetition.to_a) }
}.to change { dossier.reload.champ_values_for_export(procedure.all_revisions_types_de_champ.not_repetition.to_a, format: :xlsx) }
.from([["c1", 42]]).to([["c1", 42.0]])
end
end
@ -2020,8 +2020,8 @@ describe Dossier, type: :model do
let(:repetition_second_revision_champ) { dossier_second_revision.project_champs_public.find(&:repetition?) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier_second_revision) { create(:dossier, procedure: procedure) }
let(:dossier_champ_values_for_export) { dossier.champ_values_for_export(procedure.types_de_champ_for_procedure_export) }
let(:dossier_second_revision_champ_values_for_export) { dossier_second_revision.champ_values_for_export(procedure.types_de_champ_for_procedure_export) }
let(:dossier_champ_values_for_export) { dossier.champ_values_for_export(procedure.types_de_champ_for_procedure_export, format: :xlsx) }
let(:dossier_second_revision_champ_values_for_export) { dossier_second_revision.champ_values_for_export(procedure.types_de_champ_for_procedure_export, format: :xlsx) }
context "when procedure published" do
before do
@ -2056,7 +2056,7 @@ describe Dossier, type: :model do
dossier_test = create(:dossier, procedure: proc_test)
type_champs = proc_test.all_revisions_types_de_champ(parent: tdc_repetition).to_a
expect(type_champs.size).to eq(1)
expect(dossier.champ_values_for_export(type_champs).size).to eq(3)
expect(dossier.champ_values_for_export(type_champs, format: :xlsx).size).to eq(3)
end
end
end
@ -2079,7 +2079,7 @@ describe Dossier, type: :model do
let(:text_tdc) { procedure.active_revision.types_de_champ_public.second }
let(:tdcs) { dossier.project_champs_public.map(&:type_de_champ) }
subject { dossier.champ_values_for_export(tdcs) }
subject { dossier.champ_values_for_export(tdcs, format: :xlsx) }
before do
text_tdc.update(condition: ds_eq(champ_value(yes_no_tdc.stable_id), constant(true)))