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

52 lines
942 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:, path:, displayable:, type: :text)
@path = path
super(
procedure_id:,
label:,
stable_id:,
displayable:,
type:
)
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
if path == :value
"type_de_champ/#{stable_id}"
else
"type_de_champ/#{stable_id}->#{path}"
end
end
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