From b12ac7c9aa3bdfb93af462fc3a028ba1dd86f05a Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 17 Dec 2024 16:03:36 +0100 Subject: [PATCH] wip make column work with referentiel headers --- .../types_de_champ_controller.rb | 7 +++- .../drop_down_list_type_de_champ.rb | 34 +++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index f018f59980b..868a9e2694a 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -153,7 +153,12 @@ def import_referentiel ActiveRecord::Base.transaction do # Create referentiel - referentiel = type_de_champ.create_referentiel!(name: referentiel_file.original_filename) + headers = ACSV::CSV.new_for_ruby3(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true) + .map { |row| row.to_h } + .first + .keys + + referentiel = type_de_champ.create_referentiel!(name: referentiel_file.original_filename, headers:) csv_to_code = ACSV::CSV.new_for_ruby3(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true, header_converters: [:downcase, :symbol]) .map { |row| row.to_h } diff --git a/app/models/types_de_champ/drop_down_list_type_de_champ.rb b/app/models/types_de_champ/drop_down_list_type_de_champ.rb index acc7abf7fd6..b5ab8aed11d 100644 --- a/app/models/types_de_champ/drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/drop_down_list_type_de_champ.rb @@ -1,31 +1,31 @@ # frozen_string_literal: true class TypesDeChamp::DropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase - def columns(procedure:, displayable: true, prefix: nil) if referentiel? - referentiel.data_header_columns.map do |header| - Columns::JSONPathColumn.new( - procedure_id: procedure.id, - stable_id:, - tdc_type: type_champ, - label: "#{libelle_with_prefix(prefix)} – Référentiel #{header.humanize}", - type: :text, - jsonpath: "$.referentiel.data.#{header}", - displayable: - ) - end - .concat( - [Columns::JSONPathColumn.new( + referentiel.headers.map do |header| + Columns::JSONPathColumn.new( procedure_id: procedure.id, stable_id:, tdc_type: type_champ, - label: "#{libelle_with_prefix(prefix)} – Référentiel #{referentiel.option_header_column.humanize}", + label: "#{libelle_with_prefix(prefix)} – Référentiel #{header}", type: :text, - jsonpath: "$.referentiel.option.#{referentiel.option_header_column}", + jsonpath: "$.referentiel.data.#{header.parameterize.underscore}", displayable: ) - ] + end + .concat( + [ + Columns::JSONPathColumn.new( + procedure_id: procedure.id, + stable_id:, + tdc_type: type_champ, + label: "#{libelle_with_prefix(prefix)} – Référentiel #{referentiel.option_header_column.humanize}", + type: :text, + jsonpath: "$.referentiel.option.#{referentiel.option_header_column}", + displayable: + ) + ] ) else super