Class: CustomerFilter::QueryBuilder

Inherits:
BaseQueryBuilder show all
Defined in:
app/queries/customer_filter/query_builder.rb

Overview

Query object: query builder.

Instance Attribute Summary

Attributes inherited from BaseQueryBuilder

#query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseQueryBuilder

#array_check, #habtm_check, #reflect

Constructor Details

#initialize(query = nil) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



4
5
6
7
# File 'app/queries/customer_filter/query_builder.rb', line 4

def initialize(query = nil)
  @table_prefix = 'customer_filters'
  super(query || CustomerFilter.unscoped)
end

Class Method Details

.filter_ids_for_customer(customer) ⇒ Object



9
10
11
12
13
# File 'app/queries/customer_filter/query_builder.rb', line 9

def self.filter_ids_for_customer(customer)
  Customer::Cache::CustomerFilter.fetch(customer) do
    CustomerFilter::QueryBuilder.new.for_customer(customer).query.ids
  end
end

Instance Method Details

#for_customer(customer) ⇒ Object



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
# File 'app/queries/customer_filter/query_builder.rb', line 15

def for_customer(customer)
  return reflect(query.none) if customer.nil?

  customer_address = customer.main_address
  contact_points = customer.contact_points
  has_phone = contact_points.any?(&:callable?)
  has_fax   = contact_points.any?(&:fax?)
  has_email = contact_points.any?(&:email?)

  reflect(
    with_profile_id(customer.profile_id)
    .with_store_id(customer.store.try(:id))
    .with_state_code(customer_address&.state_code)
    .with_city(customer_address&.city)
    .with_postal_code(customer_address&.zip)
    .with_report_grouping(customer.effective_report_grouping)
    .with_customer_state(customer.state)
    .with_catalog_id(customer.catalog_id)
    .with_buying_group_id(customer.buying_group_id)
    .with_source_id(customer.source_id)
    .with_party_id(customer.id)
    .with_tier2_program_pricing(customer.tier2_program_pricing_id)
    .with_contact_point(:has_phone, has_phone)
    .with_contact_point(:has_fax, has_fax)
    .with_contact_point(:has_email, has_email)
    .with_sales_order(customer.orders.so_only.active.exists?)
    .with_phone_number_starts_with(customer.all_contact_points.dialable.map(&:detail))
    .query
  )
end

#with_buying_group_id(buying_group_id) ⇒ Object



102
103
104
# File 'app/queries/customer_filter/query_builder.rb', line 102

def with_buying_group_id(buying_group_id)
  habtm_check(buying_group_id.to_i, :buying_groups_customer_filters, :buying_group_id)
end

#with_catalog_id(catalog_id) ⇒ Object



98
99
100
# File 'app/queries/customer_filter/query_builder.rb', line 98

def with_catalog_id(catalog_id)
  habtm_check(catalog_id.to_i, :catalogs_customer_filters, :catalog_id)
end

#with_city(city) ⇒ Object



68
69
70
# File 'app/queries/customer_filter/query_builder.rb', line 68

def with_city(city)
  array_check(:cities, city)
end

#with_contact_point(cp_category, has_contact_point) ⇒ Object



46
47
48
49
50
# File 'app/queries/customer_filter/query_builder.rb', line 46

def with_contact_point(cp_category, has_contact_point)
  raise 'Invalid contact point category' unless %i[has_phone has_fax has_email].include? cp_category

  reflect(has_contact_point ? query.where(cp_category => [CustomerFilter::YES, CustomerFilter::NA]) : query.where(cp_category => [CustomerFilter::NO, CustomerFilter::NA]))
end

#with_customer_state(state) ⇒ Object



94
95
96
# File 'app/queries/customer_filter/query_builder.rb', line 94

def with_customer_state(state)
  array_check(:customer_states, state)
end

#with_party_id(party_id) ⇒ Object



110
111
112
# File 'app/queries/customer_filter/query_builder.rb', line 110

def with_party_id(party_id)
  habtm_check(party_id.to_i, :customer_filters_parties, :party_id, :exclude_parties)
end

#with_phone_number_starts_with(phone_numbers) ⇒ Object



82
83
84
85
86
87
88
# File 'app/queries/customer_filter/query_builder.rb', line 82

def with_phone_number_starts_with(phone_numbers)
  return reflect(query) if phone_numbers.blank?

  sql_filters = phone_numbers.map { |pn| "'#{pn}' ~ customer_filters.phone_number_starts_with" }
  sql_filters << 'customer_filters.phone_number_starts_with IS NULL'
  reflect(query.where(sql_filters.join(' OR ')))
end

#with_postal_code(postal_code) ⇒ Object



76
77
78
79
80
# File 'app/queries/customer_filter/query_builder.rb', line 76

def with_postal_code(postal_code)
  postal_values = [postal_code]
  postal_values << postal_code.first(3) if postal_code && postal_code =~ /^([a-z]\d[a-z])/i # Canadians get a partial match query
  array_check(:postal_codes, postal_values)
end

#with_profile_id(profile_id) ⇒ Object



56
57
58
# File 'app/queries/customer_filter/query_builder.rb', line 56

def with_profile_id(profile_id)
  habtm_check(profile_id.to_i, :customer_filters_profiles, :profile_id)
end

#with_report_grouping(report_grouping) ⇒ Object



90
91
92
# File 'app/queries/customer_filter/query_builder.rb', line 90

def with_report_grouping(report_grouping)
  array_check(:report_groupings, report_grouping)
end

#with_sales_order(sales_order_exists) ⇒ Object



52
53
54
# File 'app/queries/customer_filter/query_builder.rb', line 52

def with_sales_order(sales_order_exists)
  reflect(sales_order_exists ? query.where(has_sales_order: [CustomerFilter::YES, CustomerFilter::NA]) : query.where(has_sales_order: [CustomerFilter::NO, CustomerFilter::NA]))
end

#with_source_id(source_id) ⇒ Object



106
107
108
# File 'app/queries/customer_filter/query_builder.rb', line 106

def with_source_id(source_id)
  habtm_check(source_id.to_i, :customer_filters_sources, :source_id)
end

#with_state_code(state_code) ⇒ Object



64
65
66
# File 'app/queries/customer_filter/query_builder.rb', line 64

def with_state_code(state_code)
  array_check(:state_codes, state_code)
end

#with_store_id(store_id) ⇒ Object



60
61
62
# File 'app/queries/customer_filter/query_builder.rb', line 60

def with_store_id(store_id)
  reflect(store_id.present? ? query.where(store_id: [store_id, nil]) : query)
end

#with_tier2_program_pricing(tier2_program_pricing_id) ⇒ Object



72
73
74
# File 'app/queries/customer_filter/query_builder.rb', line 72

def with_tier2_program_pricing(tier2_program_pricing_id)
  habtm_check(tier2_program_pricing_id, :customer_filters_program_pricings, :tier2_program_pricing_id)
end