From 17531b73b7cdae8c9e9cbec4efc6d42cccc491c7 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 6 Nov 2024 22:01:21 +0100 Subject: [PATCH] internalize value_column inside champ_column --- app/models/column.rb | 7 +- app/models/columns/champ_column.rb | 5 +- app/models/type_de_champ.rb | 8 -- .../piece_justificative_type_de_champ.rb | 1 - .../titre_identite_type_de_champ.rb | 1 - .../types_de_champ/type_de_champ_base.rb | 1 - spec/models/concerns/columns_concern_spec.rb | 86 +++++++++---------- 7 files changed, 49 insertions(+), 60 deletions(-) diff --git a/app/models/column.rb b/app/models/column.rb index 178b7860f..7f8cdc9b8 100644 --- a/app/models/column.rb +++ b/app/models/column.rb @@ -8,15 +8,14 @@ class Column TYPE_DE_CHAMP_TABLE = 'type_de_champ' - attr_reader :table, :column, :label, :type, :value_column, :filterable, :displayable, :options_for_select + attr_reader :table, :column, :label, :type, :filterable, :displayable, :options_for_select - def initialize(procedure_id:, table:, column:, label: nil, type: :text, value_column: :value, filterable: true, displayable: true, options_for_select: []) + def initialize(procedure_id:, table:, column:, label: nil, type: :text, filterable: true, displayable: true, options_for_select: []) @procedure_id = procedure_id @table = table @column = column @label = label || I18n.t(column, scope: [:activerecord, :attributes, :procedure_presentation, :fields, table]) @type = type - @value_column = value_column @filterable = filterable @displayable = displayable @options_for_select = options_for_select @@ -33,7 +32,7 @@ class Column def to_json { - table:, column:, label:, type:, value_column:, filterable:, displayable: + table:, column:, label:, type:, filterable:, displayable: } end diff --git a/app/models/columns/champ_column.rb b/app/models/columns/champ_column.rb index 6ff99ff13..aa7cb16d4 100644 --- a/app/models/columns/champ_column.rb +++ b/app/models/columns/champ_column.rb @@ -3,7 +3,7 @@ class Columns::ChampColumn < Column attr_reader :stable_id - def initialize(procedure_id:, label:, stable_id:, tdc_type:, displayable: true, filterable: true, type: :text, value_column: :value, options_for_select: []) + def initialize(procedure_id:, label:, stable_id:, tdc_type:, displayable: true, filterable: true, type: :text, options_for_select: []) @stable_id = stable_id @tdc_type = tdc_type @@ -13,7 +13,6 @@ class Columns::ChampColumn < Column column: stable_id.to_s, label:, type:, - value_column:, displayable:, filterable:, options_for_select: @@ -48,6 +47,8 @@ class Columns::ChampColumn < Column def column_id = "type_de_champ/#{stable_id}" + def value_column = @tdc_type.in?(['departements', 'regions']) ? :external_id : :value + def string_value(champ) = champ.public_send(value_column) def typed_value(champ) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index f6ba0ff7d..e0e3319b3 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -545,14 +545,6 @@ class TypeDeChamp < ApplicationRecord end end - def self.value_column(type_champ) - if type_champ.in?([type_champs.fetch(:departements), type_champs.fetch(:regions)]) - :external_id - else - :value - end - end - def options_for_select if departement? APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }.sort 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 dbebe11f9..3fef517d0 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 @@ -33,7 +33,6 @@ class TypesDeChamp::PieceJustificativeTypeDeChamp < TypesDeChamp::TypeDeChampBas 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 ) 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 e72944c15..ed929dec4 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 @@ -32,7 +32,6 @@ class TypesDeChamp::TitreIdentiteTypeDeChamp < TypesDeChamp::TypeDeChampBase 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 ) diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index 885d1be49..1cf5e66e4 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -104,7 +104,6 @@ class TypesDeChamp::TypeDeChampBase tdc_type: type_champ, label: libelle_with_prefix(prefix), type: TypeDeChamp.column_type(type_champ), - value_column: TypeDeChamp.value_column(type_champ), displayable:, options_for_select: ) diff --git a/spec/models/concerns/columns_concern_spec.rb b/spec/models/concerns/columns_concern_spec.rb index 32515dc10..0f046e23d 100644 --- a/spec/models/concerns/columns_concern_spec.rb +++ b/spec/models/concerns/columns_concern_spec.rb @@ -30,42 +30,42 @@ describe ColumnsConcern do let(:tdc_private_2) { procedure.active_revision.types_de_champ_private[1] } let(:expected) { [ - { label: 'Dossier ID', table: 'self', column: 'id', displayable: true, type: :number, value_column: :value, filterable: true }, - { label: 'notifications', table: 'notifications', column: 'notifications', displayable: true, type: :text, value_column: :value, filterable: false }, - { label: 'Date de création', table: 'self', column: 'created_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'Mis à jour le', table: 'self', column: 'updated_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'Date de dépot', table: 'self', column: 'depose_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'En construction le', table: 'self', column: 'en_construction_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'En instruction le', table: 'self', column: 'en_instruction_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'Terminé le', table: 'self', column: 'processed_at', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: "Dernier évènement depuis", table: "self", column: "updated_since", displayable: false, type: :date, value_column: :value, filterable: true }, - { label: "Déposé depuis", table: "self", column: "depose_since", displayable: false, type: :date, value_column: :value, filterable: true }, - { label: "En construction depuis", table: "self", column: "en_construction_since", displayable: false, type: :date, value_column: :value, filterable: true }, - { label: "En instruction depuis", table: "self", column: "en_instruction_since", displayable: false, type: :date, value_column: :value, filterable: true }, - { label: "Traité depuis", table: "self", column: "processed_since", displayable: false, type: :date, value_column: :value, filterable: true }, - { label: "Statut", table: "self", column: "state", displayable: false, type: :enum, value_column: :value, filterable: true }, - { label: "Archivé", table: "self", column: "archived", displayable: false, type: :text, value_column: :value, filterable: false }, - { label: "Motivation de la décision", table: "self", column: "motivation", displayable: false, type: :text, value_column: :value, filterable: false }, - { label: "Date de dernière modification (usager)", table: "self", column: "last_champ_updated_at", displayable: false, type: :text, value_column: :value, filterable: false }, - { label: 'Demandeur', table: 'user', column: 'email', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Email instructeur', table: 'followers_instructeurs', column: 'email', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Groupe instructeur', table: 'groupe_instructeur', column: 'id', displayable: true, type: :enum, value_column: :value, filterable: true }, - { label: 'Avis oui/non', table: 'avis', column: 'question_answer', displayable: true, type: :text, value_column: :value, filterable: false }, - { label: 'France connecté ?', table: 'self', column: 'user_from_france_connect?', displayable: false, type: :text, value_column: :value, filterable: false }, - { label: "Labels", table: "dossier_labels", column: "label_id", displayable: true, value_column: :value, filterable: true }, - { label: 'SIREN', table: 'etablissement', column: 'entreprise_siren', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Forme juridique', table: 'etablissement', column: 'entreprise_forme_juridique', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Nom commercial', table: 'etablissement', column: 'entreprise_nom_commercial', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Raison sociale', table: 'etablissement', column: 'entreprise_raison_sociale', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'SIRET siège social', table: 'etablissement', column: 'entreprise_siret_siege_social', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Date de création', table: 'etablissement', column: 'entreprise_date_creation', displayable: true, type: :date, value_column: :value, filterable: true }, - { label: 'SIRET', table: 'etablissement', column: 'siret', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Libellé NAF', table: 'etablissement', column: 'libelle_naf', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: 'Code postal', table: 'etablissement', column: 'code_postal', displayable: true, type: :text, value_column: :value, filterable: true }, - { label: tdc_1.libelle, table: 'type_de_champ', column: tdc_1.stable_id.to_s, displayable: true, type: :text, value_column: :value, filterable: true }, - { label: tdc_2.libelle, table: 'type_de_champ', column: tdc_2.stable_id.to_s, displayable: true, type: :text, value_column: :value, filterable: true }, - { label: tdc_private_1.libelle, table: 'type_de_champ', column: tdc_private_1.stable_id.to_s, displayable: true, type: :text, value_column: :value, filterable: true }, - { label: tdc_private_2.libelle, table: 'type_de_champ', column: tdc_private_2.stable_id.to_s, displayable: true, type: :text, value_column: :value, filterable: true } + { label: 'Dossier ID', table: 'self', column: 'id', displayable: true, type: :number, filterable: true }, + { label: 'notifications', table: 'notifications', column: 'notifications', displayable: true, type: :text, filterable: false }, + { label: 'Date de création', table: 'self', column: 'created_at', displayable: true, type: :date, filterable: true }, + { label: 'Mis à jour le', table: 'self', column: 'updated_at', displayable: true, type: :date, filterable: true }, + { label: 'Date de dépot', table: 'self', column: 'depose_at', displayable: true, type: :date, filterable: true }, + { label: 'En construction le', table: 'self', column: 'en_construction_at', displayable: true, type: :date, filterable: true }, + { label: 'En instruction le', table: 'self', column: 'en_instruction_at', displayable: true, type: :date, filterable: true }, + { label: 'Terminé le', table: 'self', column: 'processed_at', displayable: true, type: :date, filterable: true }, + { label: "Dernier évènement depuis", table: "self", column: "updated_since", displayable: false, type: :date, filterable: true }, + { label: "Déposé depuis", table: "self", column: "depose_since", displayable: false, type: :date, filterable: true }, + { label: "En construction depuis", table: "self", column: "en_construction_since", displayable: false, type: :date, filterable: true }, + { label: "En instruction depuis", table: "self", column: "en_instruction_since", displayable: false, type: :date, filterable: true }, + { label: "Traité depuis", table: "self", column: "processed_since", displayable: false, type: :date, filterable: true }, + { label: "Statut", table: "self", column: "state", displayable: false, type: :enum, filterable: true }, + { label: "Archivé", table: "self", column: "archived", displayable: false, type: :text, filterable: false }, + { label: "Motivation de la décision", table: "self", column: "motivation", displayable: false, type: :text, filterable: false }, + { label: "Date de dernière modification (usager)", table: "self", column: "last_champ_updated_at", displayable: false, type: :text, filterable: false }, + { label: 'Demandeur', table: 'user', column: 'email', displayable: true, type: :text, filterable: true }, + { label: 'Email instructeur', table: 'followers_instructeurs', column: 'email', displayable: true, type: :text, filterable: true }, + { label: 'Groupe instructeur', table: 'groupe_instructeur', column: 'id', displayable: true, type: :enum, filterable: true }, + { label: 'Avis oui/non', table: 'avis', column: 'question_answer', displayable: true, type: :text, filterable: false }, + { label: 'France connecté ?', table: 'self', column: 'user_from_france_connect?', displayable: false, type: :text, filterable: false }, + { label: "Labels", table: "dossier_labels", column: "label_id", displayable: true, filterable: true }, + { label: 'SIREN', table: 'etablissement', column: 'entreprise_siren', displayable: true, type: :text, filterable: true }, + { label: 'Forme juridique', table: 'etablissement', column: 'entreprise_forme_juridique', displayable: true, type: :text, filterable: true }, + { label: 'Nom commercial', table: 'etablissement', column: 'entreprise_nom_commercial', displayable: true, type: :text, filterable: true }, + { label: 'Raison sociale', table: 'etablissement', column: 'entreprise_raison_sociale', displayable: true, type: :text, filterable: true }, + { label: 'SIRET siège social', table: 'etablissement', column: 'entreprise_siret_siege_social', displayable: true, type: :text, filterable: true }, + { label: 'Date de création', table: 'etablissement', column: 'entreprise_date_creation', displayable: true, type: :date, filterable: true }, + { label: 'SIRET', table: 'etablissement', column: 'siret', displayable: true, type: :text, filterable: true }, + { label: 'Libellé NAF', table: 'etablissement', column: 'libelle_naf', displayable: true, type: :text, filterable: true }, + { label: 'Code postal', table: 'etablissement', column: 'code_postal', displayable: true, type: :text, filterable: true }, + { label: tdc_1.libelle, table: 'type_de_champ', column: tdc_1.stable_id.to_s, displayable: true, type: :text, filterable: true }, + { label: tdc_2.libelle, table: 'type_de_champ', column: tdc_2.stable_id.to_s, displayable: true, type: :text, filterable: true }, + { label: tdc_private_1.libelle, table: 'type_de_champ', column: tdc_private_1.stable_id.to_s, displayable: true, type: :text, filterable: true }, + { label: tdc_private_2.libelle, table: 'type_de_champ', column: tdc_private_2.stable_id.to_s, displayable: true, type: :text, filterable: true } ].map { Column.new(**_1.merge(procedure_id:)) } } @@ -103,9 +103,9 @@ describe ColumnsConcern do end context 'when the procedure is for individuals' do - let(:name_field) { Column.new(procedure_id:, label: "Prénom", table: "individual", column: "prenom", displayable: true, type: :text, value_column: :value, filterable: true) } - let(:surname_field) { Column.new(procedure_id:, label: "Nom", table: "individual", column: "nom", displayable: true, type: :text, value_column: :value, filterable: true) } - let(:gender_field) { Column.new(procedure_id:, label: "Civilité", table: "individual", column: "gender", displayable: true, type: :text, value_column: :value, filterable: true) } + let(:name_field) { Column.new(procedure_id:, label: "Prénom", table: "individual", column: "prenom", displayable: true, type: :text, filterable: true) } + let(:surname_field) { Column.new(procedure_id:, label: "Nom", table: "individual", column: "nom", displayable: true, type: :text, filterable: true) } + let(:gender_field) { Column.new(procedure_id:, label: "Civilité", table: "individual", column: "gender", displayable: true, type: :text, filterable: true) } let(:procedure) { create(:procedure, :for_individual) } it { is_expected.to include(name_field, surname_field, gender_field) } @@ -114,8 +114,8 @@ describe ColumnsConcern do context 'when the procedure is sva' do let(:procedure) { create(:procedure, :sva) } - let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVA", table: "self", column: "sva_svr_decision_on", displayable: true, type: :date, value_column: :value, filterable: true) } - let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVA avant", table: "self", column: "sva_svr_decision_before", displayable: false, type: :date, value_column: :value, filterable: true) } + let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVA", table: "self", column: "sva_svr_decision_on", displayable: true, type: :date, filterable: true) } + let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVA avant", table: "self", column: "sva_svr_decision_before", displayable: false, type: :date, filterable: true) } it { is_expected.to include(decision_on, decision_before_field) } end @@ -123,8 +123,8 @@ describe ColumnsConcern do context 'when the procedure is svr' do let(:procedure) { create(:procedure, :svr) } - let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVR", table: "self", column: "sva_svr_decision_on", displayable: true, type: :date, value_column: :value, filterable: true) } - let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVR avant", table: "self", column: "sva_svr_decision_before", displayable: false, type: :date, value_column: :value, filterable: true) } + let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVR", table: "self", column: "sva_svr_decision_on", displayable: true, type: :date, filterable: true) } + let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVR avant", table: "self", column: "sva_svr_decision_before", displayable: false, type: :date, filterable: true) } it { is_expected.to include(decision_on, decision_before_field) } end