Class: LcpRuby::Search::QueryBuilder

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

Overview

Reusable search pipeline extracted from ApplicationController#apply_advanced_search. Used by the export handler and other non-controller contexts that need to reconstruct the same query from filter params.

Handles: default scope, predefined filters, parameterized scopes, Ransack filters, quick search, and custom field filters. Does NOT handle saved filters (those require additional visibility checks and controller-level side effects).

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, params, model_class, model_definition, presenter, evaluator) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



24
25
26
27
28
29
30
31
32
# File 'lib/lcp_ruby/search/query_builder.rb', line 24

def initialize(scope, params, model_class, model_definition, presenter, evaluator)
  @scope = scope
  @params = params.transform_keys(&:to_s)
  @model_class = model_class
  @model_definition = model_definition
  @presenter = presenter
  @evaluator = evaluator
  @ransack_search = nil
end

Class Method Details

.apply(scope, params:, model_class:, model_definition:, presenter:, evaluator:) ⇒ Result

Returns with :scope and :ransack_search.

Parameters:

Returns:

  • (Result)

    with :scope and :ransack_search



20
21
22
# File 'lib/lcp_ruby/search/query_builder.rb', line 20

def self.apply(scope, params:, model_class:, model_definition:, presenter:, evaluator:)
  new(scope, params, model_class, model_definition, presenter, evaluator).apply
end

Instance Method Details

#applyObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lcp_ruby/search/query_builder.rb', line 34

def apply
  search_config = @presenter.search_config
  return Result.new(scope: @scope, ransack_search: nil) unless search_config["enabled"]

  apply_default_scope(search_config)
  apply_predefined_filter(search_config)
  apply_parameterized_scopes
  apply_ransack_filters(search_config)
  apply_quick_search(search_config)
  apply_custom_field_filters

  Result.new(scope: @scope, ransack_search: @ransack_search)
end