2024-10-29 16:38:17 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-10-31 18:34:30 +01:00
|
|
|
class Columns::LinkedDropDownColumn < Columns::ChampColumn
|
2024-11-03 22:07:38 +01:00
|
|
|
attr_reader :path
|
|
|
|
|
2024-11-06 12:02:53 +01:00
|
|
|
def initialize(procedure_id:, label:, stable_id:, tdc_type:, path:, options_for_select: [], displayable:, type: :text)
|
2024-11-03 22:07:38 +01:00
|
|
|
@path = path
|
|
|
|
|
2024-10-31 18:34:30 +01:00
|
|
|
super(
|
|
|
|
procedure_id:,
|
|
|
|
label:,
|
|
|
|
stable_id:,
|
2024-11-04 16:34:00 +01:00
|
|
|
tdc_type:,
|
2024-10-31 18:34:30 +01:00
|
|
|
displayable:,
|
2024-11-06 12:02:53 +01:00
|
|
|
type:,
|
|
|
|
options_for_select:
|
2024-10-31 18:34:30 +01:00
|
|
|
)
|
2024-10-29 16:38:17 +01:00
|
|
|
end
|
|
|
|
|
2024-11-13 10:03:24 +01:00
|
|
|
def filtered_ids(dossiers, search_terms)
|
|
|
|
relation = dossiers.with_type_de_champ(@stable_id)
|
|
|
|
|
|
|
|
case path
|
|
|
|
when :value
|
|
|
|
search_terms.flat_map do |search_term|
|
|
|
|
# when looking for "section 1 / option A",
|
|
|
|
# the value must contain both "section 1" and "option A"
|
|
|
|
primary, *secondary = search_term.split(%r{[[:space:]]*/[[:space:]]*})
|
|
|
|
safe_terms = [primary, *secondary].map { "%#{safe_like(_1)}%" }
|
|
|
|
|
|
|
|
relation.where("champs.value ILIKE ALL (ARRAY[?])", safe_terms).ids
|
|
|
|
end.uniq
|
|
|
|
when :primary
|
|
|
|
primary_terms = search_terms.map { |term| %{["#{safe_like(term)}","%"]} }
|
|
|
|
|
|
|
|
relation.where("champs.value ILIKE ANY (array[?])", primary_terms).ids
|
|
|
|
when :secondary
|
|
|
|
secondary_terms = search_terms.map { |term| %{["%","#{safe_like(term)}"]} }
|
|
|
|
|
|
|
|
relation.where("champs.value ILIKE ANY (array[?])", secondary_terms).ids
|
|
|
|
end
|
2024-10-29 16:38:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-11-04 08:26:57 +01:00
|
|
|
def column_id = "type_de_champ/#{stable_id}->#{path}"
|
2024-10-31 18:34:30 +01:00
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
def typed_value(champ)
|
2024-10-29 16:38:17 +01:00
|
|
|
primary_value, secondary_value = unpack_values(champ.value)
|
2024-11-03 22:07:38 +01:00
|
|
|
case path
|
2024-11-14 09:55:10 +01:00
|
|
|
when :value
|
|
|
|
"#{primary_value} / #{secondary_value}"
|
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
|
2024-11-13 10:03:24 +01:00
|
|
|
|
|
|
|
def safe_like(q) = ActiveRecord::Base.sanitize_sql_like(q)
|
2024-10-29 16:38:17 +01:00
|
|
|
end
|