demarches-normaliennes/app/models/columns/champ_column.rb

119 lines
2.9 KiB
Ruby
Raw Normal View History

2024-10-31 18:29:23 +01:00
# frozen_string_literal: true
class Columns::ChampColumn < Column
attr_reader :stable_id
2024-10-31 18:29:23 +01:00
def initialize(procedure_id:, label:, stable_id:, tdc_type:, displayable: true, filterable: true, type: :text, options_for_select: [])
2024-10-31 18:29:23 +01:00
@stable_id = stable_id
2024-11-04 16:34:00 +01:00
@tdc_type = tdc_type
2024-10-31 18:29:23 +01:00
super(
procedure_id:,
table: 'type_de_champ',
column: stable_id.to_s,
label:,
type:,
displayable:,
filterable:,
options_for_select:
2024-10-31 18:29:23 +01:00
)
end
def value(champ)
return if champ.nil?
2024-11-04 16:34:29 +01:00
# nominal case
if @tdc_type == champ.last_write_type_champ
typed_value(champ)
2024-10-31 18:29:23 +01:00
else
2024-11-04 16:34:29 +01:00
cast_value(champ)
2024-10-31 18:29:23 +01:00
end
end
def filtered_ids(dossiers, search_terms)
if type == :enum
dossiers.with_type_de_champ(stable_id)
.filter_enum(:champs, value_column, search_terms).ids
elsif type == :enums
dossiers.with_type_de_champ(stable_id)
.filter_array_enum(:champs, value_column, search_terms).ids
else
dossiers.with_type_de_champ(stable_id)
.filter_ilike(:champs, value_column, search_terms).ids
end
end
2024-10-31 18:29:23 +01:00
private
def column_id = "type_de_champ/#{stable_id}"
def value_column = @tdc_type.in?(['departements', 'regions']) ? :external_id : :value
2024-11-04 16:34:29 +01:00
def string_value(champ) = champ.public_send(value_column)
2024-10-31 18:29:23 +01:00
def typed_value(champ)
value = string_value(champ)
return if value.blank?
case type
when :boolean
parse_boolean(value)
when :integer
value.to_i
when :decimal
value.to_f
when :datetime
parse_datetime(value)
when :date
parse_datetime(value)&.to_date
when :enums
parse_enums(value)
else
value
end
end
2024-11-04 16:34:29 +01:00
def cast_value(champ)
value = string_value(champ)
2024-10-31 18:29:23 +01:00
return if value.blank?
2024-11-04 16:34:29 +01:00
case [champ.last_write_type_champ, @tdc_type]
when ['integer_number', 'decimal_number'] # recast numbers automatically
2024-10-31 18:29:23 +01:00
value.to_f
2024-11-04 16:34:29 +01:00
when ['decimal_number', 'integer_number'] # may lose some data, but who cares ?
2024-10-31 18:29:23 +01:00
value.to_i
2024-11-04 16:34:29 +01:00
when ['integer_number', 'text'], ['decimal_number', 'text'] # number to text
value
when ['drop_down_list', 'multiple_drop_down_list'] # single list can become multi
2024-10-31 18:29:23 +01:00
[value]
2024-11-04 16:34:29 +01:00
when ['drop_down_list', 'text'] # single list can become text
2024-10-31 18:29:23 +01:00
value
2024-11-04 16:34:29 +01:00
when ['multiple_drop_down_list', 'drop_down_list'] # multi list can become single
parse_enums(value).first
when ['multiple_drop_down_list', 'text'] # single list can become text
parse_enums(value).join(', ')
when ['date', 'datetime'] # date <=> datetime
parse_datetime(value)&.to_datetime
when ['datetime', 'date'] # may lose some data, but who cares ?
parse_datetime(value)&.to_date
2024-10-31 18:29:23 +01:00
else
nil
end
end
def parse_boolean(value)
case value
when 'true', 'on', '1'
true
when 'false'
false
end
end
def parse_enums(value) = JSON.parse(value) rescue nil
def parse_datetime(value) = Time.zone.parse(value) rescue nil
end