From 9d13ebb3ffaf95fc834a2f7e147d344e551fde62 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 4 Nov 2024 17:01:00 +0100 Subject: [PATCH] feat(pj): add specialized column for attachments --- app/models/columns/attached_many_column.rb | 9 +++ app/models/columns/titre_identite_column.rb | 9 --- app/models/type_de_champ.rb | 4 +- .../piece_justificative_type_de_champ.rb | 13 +++- .../titre_identite_type_de_champ.rb | 2 +- spec/models/column_spec.rb | 4 -- spec/models/columns/champ_column_spec.rb | 69 +++++++++---------- 7 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 app/models/columns/attached_many_column.rb delete mode 100644 app/models/columns/titre_identite_column.rb delete mode 100644 spec/models/column_spec.rb diff --git a/app/models/columns/attached_many_column.rb b/app/models/columns/attached_many_column.rb new file mode 100644 index 000000000..439f7adc5 --- /dev/null +++ b/app/models/columns/attached_many_column.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class Columns::AttachedManyColumn < Columns::ChampColumn + private + + def typed_value(champ) + champ.piece_justificative_file.to_a + end +end diff --git a/app/models/columns/titre_identite_column.rb b/app/models/columns/titre_identite_column.rb deleted file mode 100644 index 80ddcaf63..000000000 --- a/app/models/columns/titre_identite_column.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -class Columns::TitreIdentiteColumn < Columns::ChampColumn - private - - def typed_value(champ) - champ.piece_justificative_file.attached? - end -end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 420b4a2cf..6364d3d24 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -540,8 +540,10 @@ class TypeDeChamp < ApplicationRecord :enums when type_champs.fetch(:drop_down_list), type_champs.fetch(:departements), type_champs.fetch(:regions) :enum - when type_champs.fetch(:checkbox), type_champs.fetch(:yes_no), type_champs.fetch(:titre_identite) + when type_champs.fetch(:checkbox), type_champs.fetch(:yes_no) :boolean + when type_champs.fetch(:titre_identite), type_champs.fetch(:piece_justificative) + :attachements else :text end diff --git a/app/models/types_de_champ/piece_justificative_type_de_champ.rb b/app/models/types_de_champ/piece_justificative_type_de_champ.rb index 5ddd1bef6..cb65fb132 100644 --- a/app/models/types_de_champ/piece_justificative_type_de_champ.rb +++ b/app/models/types_de_champ/piece_justificative_type_de_champ.rb @@ -26,6 +26,17 @@ class TypesDeChamp::PieceJustificativeTypeDeChamp < TypesDeChamp::TypeDeChampBas def champ_blank?(champ) = champ.piece_justificative_file.blank? def columns(procedure_id:, displayable: true, prefix: nil) - [] + [ + Columns::AttachedManyColumn.new( + procedure_id:, + stable_id:, + tdc_type: type_champ, + label: libelle_with_prefix(prefix), + type: TypeDeChamp.column_type(type_champ), + value_column: TypeDeChamp.value_column(type_champ), + displayable: false, + filterable: false + ) + ] end end diff --git a/app/models/types_de_champ/titre_identite_type_de_champ.rb b/app/models/types_de_champ/titre_identite_type_de_champ.rb index 02cd74fb6..9938808de 100644 --- a/app/models/types_de_champ/titre_identite_type_de_champ.rb +++ b/app/models/types_de_champ/titre_identite_type_de_champ.rb @@ -26,7 +26,7 @@ class TypesDeChamp::TitreIdentiteTypeDeChamp < TypesDeChamp::TypeDeChampBase def columns(procedure_id:, displayable: nil, prefix: nil) [ - Columns::TitreIdentiteColumn.new( + Columns::AttachedManyColumn.new( procedure_id:, stable_id:, tdc_type: type_champ, diff --git a/spec/models/column_spec.rb b/spec/models/column_spec.rb deleted file mode 100644 index 7b1f4b3d9..000000000 --- a/spec/models/column_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true - -describe Column do -end diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index 8318cf2c9..0cbf1da91 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -9,39 +9,38 @@ describe Columns::ChampColumn do let(:types_de_champ) { procedure.all_revisions_types_de_champ } it 'extracts values for columns and type de champ' do - expect_type_de_champ_values('civilite', ["M."]) - expect_type_de_champ_values('email', ['yoda@beta.gouv.fr']) - expect_type_de_champ_values('phone', ['0666666666']) - expect_type_de_champ_values('address', ["2 rue des Démarches"]) - expect_type_de_champ_values('communes', ["Coye-la-Forêt"]) - expect_type_de_champ_values('departements', ['01']) - expect_type_de_champ_values('regions', ['01']) - expect_type_de_champ_values('pays', ['France']) - expect_type_de_champ_values('epci', [nil]) - expect_type_de_champ_values('iban', [nil]) - expect_type_de_champ_values('siret', ["44011762001530", "postal_code", "city_name", "departement_code", "region_name"]) - expect_type_de_champ_values('text', ['text']) - expect_type_de_champ_values('textarea', ['textarea']) - expect_type_de_champ_values('number', ['42']) - expect_type_de_champ_values('decimal_number', [42.1]) - expect_type_de_champ_values('integer_number', [42]) - expect_type_de_champ_values('date', [Time.zone.parse('2019-07-10').to_date]) - expect_type_de_champ_values('datetime', [Time.zone.parse("1962-09-15T15:35:00+01:00")]) - expect_type_de_champ_values('checkbox', [true]) - expect_type_de_champ_values('drop_down_list', ['val1']) - expect_type_de_champ_values('multiple_drop_down_list', [["val1", "val2"]]) - expect_type_de_champ_values('linked_drop_down_list', [nil, "categorie 1", "choix 1"]) - expect_type_de_champ_values('yes_no', [true]) - expect_type_de_champ_values('annuaire_education', [nil]) - expect_type_de_champ_values('carte', []) - expect_type_de_champ_values('piece_justificative', []) - expect_type_de_champ_values('titre_identite', [true]) - expect_type_de_champ_values('cnaf', [nil]) - expect_type_de_champ_values('dgfip', [nil]) - expect_type_de_champ_values('pole_emploi', [nil]) - expect_type_de_champ_values('mesri', [nil]) - expect_type_de_champ_values('cojo', [nil]) - expect_type_de_champ_values('expression_reguliere', [nil]) + expect_type_de_champ_values('civilite', eq(["M."])) + expect_type_de_champ_values('email', eq(['yoda@beta.gouv.fr'])) + expect_type_de_champ_values('phone', eq(['0666666666'])) + expect_type_de_champ_values('address', eq(["2 rue des Démarches"])) + expect_type_de_champ_values('communes', eq(["Coye-la-Forêt"])) + expect_type_de_champ_values('departements', eq(['01'])) + expect_type_de_champ_values('regions', eq(['01'])) + expect_type_de_champ_values('pays', eq(['France'])) + expect_type_de_champ_values('epci', eq([nil])) + expect_type_de_champ_values('iban', eq([nil])) + expect_type_de_champ_values('siret', eq(["44011762001530", "postal_code", "city_name", "departement_code", "region_name"])) + expect_type_de_champ_values('text', eq(['text'])) + expect_type_de_champ_values('textarea', eq(['textarea'])) + expect_type_de_champ_values('number', eq(['42'])) + expect_type_de_champ_values('decimal_number', eq([42.1])) + expect_type_de_champ_values('integer_number', eq([42])) + expect_type_de_champ_values('date', eq([Time.zone.parse('2019-07-10').to_date])) + expect_type_de_champ_values('datetime', eq([Time.zone.parse("1962-09-15T15:35:00+01:00")])) + expect_type_de_champ_values('checkbox', eq([true])) + expect_type_de_champ_values('drop_down_list', eq(['val1'])) + expect_type_de_champ_values('multiple_drop_down_list', eq([["val1", "val2"]])) + expect_type_de_champ_values('linked_drop_down_list', eq([nil, "categorie 1", "choix 1"])) + expect_type_de_champ_values('yes_no', eq([true])) + expect_type_de_champ_values('annuaire_education', eq([nil])) + expect_type_de_champ_values('piece_justificative', be_an_instance_of(Array)) + expect_type_de_champ_values('titre_identite', be_an_instance_of(Array)) + expect_type_de_champ_values('cnaf', eq([nil])) + expect_type_de_champ_values('dgfip', eq([nil])) + expect_type_de_champ_values('pole_emploi', eq([nil])) + expect_type_de_champ_values('mesri', eq([nil])) + expect_type_de_champ_values('cojo', eq([nil])) + expect_type_de_champ_values('expression_reguliere', eq([nil])) end end @@ -120,11 +119,11 @@ describe Columns::ChampColumn do private - def expect_type_de_champ_values(type, values) + def expect_type_de_champ_values(type, assertion) type_de_champ = types_de_champ.find { _1.type_champ == type } champ = dossier.send(:filled_champ, type_de_champ, nil) columns = type_de_champ.columns(procedure_id: procedure.id) - expect(columns.map { _1.value(champ) }).to eq(values) + expect(columns.map { _1.value(champ) }).to assertion end def retrieve_champ(type)