2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-04 10:18:07 +01:00
|
|
|
class Columns::JSONPathColumn < Columns::ChampColumn
|
|
|
|
attr_reader :jsonpath
|
|
|
|
|
2024-11-06 11:28:51 +01:00
|
|
|
def initialize(procedure_id:, label:, stable_id:, tdc_type:, jsonpath:, options_for_select: [], displayable:, type: :text)
|
2024-11-04 10:18:07 +01:00
|
|
|
@jsonpath = quote_string(jsonpath)
|
|
|
|
|
|
|
|
super(
|
|
|
|
procedure_id:,
|
|
|
|
label:,
|
|
|
|
stable_id:,
|
2024-11-04 16:34:00 +01:00
|
|
|
tdc_type:,
|
2024-11-04 10:18:07 +01:00
|
|
|
displayable:,
|
2024-11-06 11:28:51 +01:00
|
|
|
type:,
|
|
|
|
options_for_select:
|
2024-11-04 10:18:07 +01:00
|
|
|
)
|
2024-07-22 14:58:16 +02:00
|
|
|
end
|
|
|
|
|
2024-11-04 10:18:07 +01:00
|
|
|
def filtered_ids(dossiers, search_terms)
|
|
|
|
value = quote_string(search_terms.join('|'))
|
|
|
|
|
|
|
|
condition = %{champs.value_json @? '#{jsonpath} ? (@ like_regex "#{value}" flag "i")'}
|
|
|
|
|
2024-07-22 14:58:16 +02:00
|
|
|
dossiers.with_type_de_champ(stable_id)
|
2024-11-04 10:18:07 +01:00
|
|
|
.where(condition)
|
2024-07-22 14:58:16 +02:00
|
|
|
.ids
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-11-04 10:18:07 +01:00
|
|
|
def column_id = "type_de_champ/#{stable_id}-#{jsonpath}"
|
2024-07-22 14:58:16 +02:00
|
|
|
|
2024-11-04 10:18:07 +01:00
|
|
|
def typed_value(champ)
|
|
|
|
champ.value_json&.dig(*jsonpath.split('.')[1..])
|
2024-07-22 14:58:16 +02:00
|
|
|
end
|
|
|
|
|
2024-11-04 10:18:07 +01:00
|
|
|
def quote_string(string) = ActiveRecord::Base.connection.quote_string(string)
|
2024-07-22 14:58:16 +02:00
|
|
|
end
|