demarches-normaliennes/app/models/columns/linked_drop_down_column.rb
2024-11-05 09:38:27 +01:00

46 lines
886 B
Ruby

# frozen_string_literal: true
class Columns::LinkedDropDownColumn < Columns::ChampColumn
attr_reader :path
def initialize(procedure_id:, label:, stable_id:, tdc_type:, path:, displayable:, type: :text)
@path = path
super(
procedure_id:,
label:,
stable_id:,
tdc_type:,
displayable:,
type:
)
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
primary_value, secondary_value = unpack_values(champ.value)
case path
when :primary
primary_value
when :secondary
secondary_value
end
end
def unpack_values(value)
JSON.parse(value)
rescue JSON::ParserError
[]
end
end