From db2e4cf802429b8b6d62f095b3e557ab660e4e1d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 6 Nov 2024 11:28:51 +0100 Subject: [PATCH] inject options_for_select for json_column --- app/models/columns/json_path_column.rb | 16 +++----------- .../concerns/addressable_column_concern.rb | 21 ++++++++++++------- app/models/type_de_champ.rb | 4 +--- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/models/columns/json_path_column.rb b/app/models/columns/json_path_column.rb index 89e4e15f3..a60d5d872 100644 --- a/app/models/columns/json_path_column.rb +++ b/app/models/columns/json_path_column.rb @@ -3,7 +3,7 @@ class Columns::JSONPathColumn < Columns::ChampColumn attr_reader :jsonpath - def initialize(procedure_id:, label:, stable_id:, tdc_type:, jsonpath:, displayable:, type: :text) + def initialize(procedure_id:, label:, stable_id:, tdc_type:, jsonpath:, options_for_select: [], displayable:, type: :text) @jsonpath = quote_string(jsonpath) super( @@ -12,7 +12,8 @@ class Columns::JSONPathColumn < Columns::ChampColumn stable_id:, tdc_type:, displayable:, - type: + type:, + options_for_select: ) end @@ -26,17 +27,6 @@ class Columns::JSONPathColumn < Columns::ChampColumn .ids end - def options_for_select - case jsonpath.split('.').last - when 'departement_code' - APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } - when 'region_name' - APIGeoService.regions.map { [_1[:name], _1[:name]] } - else - [] - end - end - private def column_id = "type_de_champ/#{stable_id}-#{jsonpath}" diff --git a/app/models/concerns/addressable_column_concern.rb b/app/models/concerns/addressable_column_concern.rb index e7ba4d11e..76af699cb 100644 --- a/app/models/concerns/addressable_column_concern.rb +++ b/app/models/concerns/addressable_column_concern.rb @@ -5,12 +5,16 @@ module AddressableColumnConcern included do def columns(procedure:, displayable: true, prefix: nil) - super.concat([ - ["code postal (5 chiffres)", '$.postal_code', :text], - ["commune", '$.city_name', :text], - ["département", '$.departement_code', :enum], - ["region", '$.region_name', :enum] - ].map do |(label, jsonpath, type)| + departement_options = APIGeoService.departements + .map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + region_options = APIGeoService.regions.map { [_1[:name], _1[:name]] } + + addressable_columns = [ + ["code postal (5 chiffres)", '$.postal_code', :text, []], + ["commune", '$.city_name', :text, []], + ["département", '$.departement_code', :enum, departement_options], + ["region", '$.region_name', :enum, region_options] + ].map do |(label, jsonpath, type, options_for_select)| Columns::JSONPathColumn.new( procedure_id: procedure.id, stable_id:, @@ -18,9 +22,12 @@ module AddressableColumnConcern label: "#{libelle_with_prefix(prefix)} – #{label}", jsonpath:, displayable:, + options_for_select:, type: ) - end) + end + + super.concat(addressable_columns) end end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index d87836187..c857ecdfe 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -553,7 +553,7 @@ class TypeDeChamp < ApplicationRecord end end - def options_for_select(column) + def options_for_select if departement? APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } elsif region? @@ -566,8 +566,6 @@ class TypeDeChamp < ApplicationRecord elsif checkbox? Champs::CheckboxChamp.options end - elsif siret? || rna? || rnf? - column.options_for_select end end