inject options_for_select for json_column

This commit is contained in:
simon lehericey 2024-11-06 11:28:51 +01:00
parent 49d3661441
commit db2e4cf802
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 18 additions and 23 deletions

View file

@ -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}"

View file

@ -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

View file

@ -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