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

121 lines
3 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
column = tdc_type.in?(['departements', 'regions']) ? :external_id : :value
2024-10-31 18:29:23 +01:00
super(
procedure_id:,
table: 'type_de_champ',
column:,
2024-10-31 18:29:23 +01:00
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
2024-11-06 22:29:59 +01:00
if champ.is_type?(@tdc_type)
2024-11-04 16:34:29 +01:00
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)
2024-11-12 17:01:38 +01:00
relation = dossiers.with_type_de_champ(stable_id)
if type == :enum
2024-11-12 17:01:38 +01:00
relation.where(champs: { column => search_terms }).ids
elsif type == :enums
# in a multiple drop down list, the value are stored as '["v1", "v2"]'
quoted_search_terms = search_terms.map { %{"#{_1}"} }
relation.filter_ilike(:champs, column, quoted_search_terms).ids
else
2024-11-12 17:01:38 +01:00
relation.filter_ilike(:champs, column, search_terms).ids
end
end
def champ_column? = true
2024-10-31 18:29:23 +01:00
private
def column_id = "type_de_champ/#{stable_id}"
def string_value(champ) = champ.public_send(column)
2024-11-04 16:34:29 +01:00
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