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

48 lines
937 B
Ruby
Raw Normal View History

2024-10-29 16:38:17 +01:00
# frozen_string_literal: true
class Columns::LinkedDropDownColumn < Columns::ChampColumn
attr_reader :path
def initialize(procedure_id:, label:, stable_id:, tdc_type:, path:, options_for_select: [], displayable:, type: :text)
@path = path
super(
procedure_id:,
label:,
stable_id:,
2024-11-04 16:34:00 +01:00
tdc_type:,
displayable:,
type:,
options_for_select:
)
2024-10-29 16:38:17 +01:00
end
def filtered_ids(dossiers, values)
dossiers.with_type_de_champ(@column)
.filter_ilike(:champs, :value, values)
.ids
end
private
def column_id = "type_de_champ/#{stable_id}->#{path}"
def typed_value(champ)
return nil if path == :value
2024-10-29 16:38:17 +01:00
primary_value, secondary_value = unpack_values(champ.value)
case path
2024-10-29 16:38:17 +01:00
when :primary
primary_value
when :secondary
secondary_value
end
end
def unpack_values(value)
JSON.parse(value)
rescue JSON::ParserError
[]
end
end