Module: Models::CustomerSearchCriteria

Extended by:
ActiveSupport::Concern
Included in:
CustomerSearch
Defined in:
app/concerns/models/customer_search_criteria.rb

Overview

Non-Ransack advanced-search criteria for CustomerSearch.

Search#apply_criteria runs the Ransack-expressible +query_params+ through
Ransack, then hands the resulting relation to #apply_custom_criteria for the
filters Ransack cannot express: correlated has-ordered / has-quoted +EXISTS+
subqueries, ltree product containment, campaign membership, drop-event joins,
profile predicates, and the certified-installer join.

Extracted from CustomerSearch as a pure structural move (no behaviour
change) to bring the former god-method under the complexity budget. The
+CustomerSearch::CUSTOM_CRITERIA_KEYS+ constant lists exactly the +query_params+
keys these methods consume; it stays on the model because it is referenced
externally (e.g. +Audience+) as +CustomerSearch::CUSTOM_CRITERIA_KEYS+.

Instance Method Summary collapse

Instance Method Details

#apply_custom_criteria(results) ⇒ ActiveRecord::Relation

Applies every non-Ransack criterion to the Ransack result relation, in the
historical order (distance, has-ordered, campaign, drop-event, profile,
certified-installer). Each helper is a no-op when its +query_params+ keys
are absent, so the relation is returned unchanged for an empty criteria set.

Parameters:

  • results (ActiveRecord::Relation)

    the post-Ransack +view_customers+ relation

Returns:

  • (ActiveRecord::Relation)

    the relation with the custom criteria applied



29
30
31
32
33
34
35
36
37
38
# File 'app/concerns/models/customer_search_criteria.rb', line 29

def apply_custom_criteria(results)
  results = apply_distance_search(results, custom_table_join: 'INNER JOIN addresses on addresses.party_id = view_customers.id', custom_table_alias: 'addresses')
  results = apply_has_ordered_criteria(results)
  results = apply_has_not_ordered_criteria(results)
  results = apply_campaign_criteria(results)
  results = apply_drop_event_criteria(results)
  results = apply_profile_criteria(results)
  results = results.joins(:certifications) if query_params[:is_certified_installer].present? && query_params[:is_certified_installer].to_b
  results
end