Class: LcpRuby::Search::FilterParamBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/search/filter_param_builder.rb

Class Method Summary collapse

Class Method Details

.build(condition_tree) ⇒ Object

Converts a condition tree into Ransack-compatible params hash.

Supports both recursive format:

{ "combinator" => "and", "children" => [leaf_or_group, ...] }

and legacy format:

{ "combinator" => "and", "conditions" => [...], "groups" => [...] }

Output: { ransack: { … }, custom_fields: { … }, scopes: { … } }



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lcp_ruby/search/filter_param_builder.rb', line 13

def self.build(condition_tree)
  return {} if condition_tree.blank?

  # Handle bare leaf condition (promoted from single-child group)
  if condition_tree.key?("field") && !condition_tree.key?("children")
    condition_tree = { "combinator" => "and", "children" => [ condition_tree ] }
  end

  # Detect legacy format and delegate
  if condition_tree.key?("conditions") && !condition_tree.key?("children")
    return build_legacy(condition_tree)
  end

  result = {}
  custom_field_params = {}
  scope_params = {}
  group_counter = { value: 0 }

  # Set root combinator when it's not the default "and"
  root_combinator = condition_tree["combinator"] || "and"
  result["m"] = root_combinator if root_combinator != "and"

  children = condition_tree["children"] || []
  children.each do |child|
    if child.key?("field")
      # Leaf condition at top level
      extract_condition(child, result, custom_field_params, scope_params)
    else
      # Nested group — emit as Ransack group
      result["g"] ||= {}
      idx = group_counter[:value].to_s
      group_counter[:value] += 1
      group_params = build_ransack_group(child, group_counter, custom_field_params, scope_params)
      result["g"][idx] = group_params if group_params.present?
    end
  end

  { ransack: result, custom_fields: custom_field_params, scopes: scope_params }
end

.dot_path_to_ransack(path) ⇒ Object

Converts association dot-path to Ransack underscore format. e.g., “company.name” -> “company_name”



55
56
57
# File 'lib/lcp_ruby/search/filter_param_builder.rb', line 55

def self.dot_path_to_ransack(path)
  path.tr(".", "_")
end