Class: CustomerFilter::QueryBuilder
Instance Attribute Summary
#query
Class Method Summary
collapse
Instance Method Summary
collapse
#array_check, #habtm_check, #reflect
Constructor Details
#initialize(query = nil) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
2
3
4
5
|
# File 'app/queries/customer_filter/query_builder.rb', line 2
def initialize(query = nil)
@table_prefix = 'customer_filters'
super(query || CustomerFilter.unscoped)
end
|
Class Method Details
.filter_ids_for_customer(customer) ⇒ Object
7
8
9
10
11
|
# File 'app/queries/customer_filter/query_builder.rb', line 7
def self.filter_ids_for_customer(customer)
Customer::Cache::CustomerFilter.fetch(customer) do
CustomerFilter::QueryBuilder.new.for_customer(customer).query.pluck(:id)
end
end
|
Instance Method Details
#for_customer(customer) ⇒ Object
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
|
# File 'app/queries/customer_filter/query_builder.rb', line 13
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
100
101
102
|
# File 'app/queries/customer_filter/query_builder.rb', line 100
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
96
97
98
|
# File 'app/queries/customer_filter/query_builder.rb', line 96
def with_catalog_id(catalog_id)
habtm_check(catalog_id.to_i, :catalogs_customer_filters, :catalog_id)
end
|
#with_city(city) ⇒ Object
66
67
68
|
# File 'app/queries/customer_filter/query_builder.rb', line 66
def with_city(city)
array_check(:cities, city)
end
|
44
45
46
47
48
|
# File 'app/queries/customer_filter/query_builder.rb', line 44
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
92
93
94
|
# File 'app/queries/customer_filter/query_builder.rb', line 92
def with_customer_state(state)
array_check(:customer_states, state)
end
|
#with_party_id(party_id) ⇒ Object
108
109
110
|
# File 'app/queries/customer_filter/query_builder.rb', line 108
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
80
81
82
83
84
85
86
|
# File 'app/queries/customer_filter/query_builder.rb', line 80
def with_phone_number_starts_with(phone_numbers)
return reflect(query) unless phone_numbers.present?
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
74
75
76
77
78
|
# File 'app/queries/customer_filter/query_builder.rb', line 74
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 array_check(:postal_codes, postal_values)
end
|
#with_profile_id(profile_id) ⇒ Object
54
55
56
|
# File 'app/queries/customer_filter/query_builder.rb', line 54
def with_profile_id(profile_id)
habtm_check(profile_id.to_i, :customer_filters_profiles, :profile_id)
end
|
#with_report_grouping(report_grouping) ⇒ Object
88
89
90
|
# File 'app/queries/customer_filter/query_builder.rb', line 88
def with_report_grouping(report_grouping)
array_check(:report_groupings, report_grouping)
end
|
#with_sales_order(sales_order_exists) ⇒ Object
#with_source_id(source_id) ⇒ Object
104
105
106
|
# File 'app/queries/customer_filter/query_builder.rb', line 104
def with_source_id(source_id)
habtm_check(source_id.to_i, :customer_filters_sources, :source_id)
end
|
#with_state_code(state_code) ⇒ Object
62
63
64
|
# File 'app/queries/customer_filter/query_builder.rb', line 62
def with_state_code(state_code)
array_check(:state_codes, state_code)
end
|
#with_store_id(store_id) ⇒ Object
58
59
60
|
# File 'app/queries/customer_filter/query_builder.rb', line 58
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
70
71
72
|
# File 'app/queries/customer_filter/query_builder.rb', line 70
def with_tier2_program_pricing(tier2_program_pricing_id)
habtm_check(tier2_program_pricing_id, :customer_filters_program_pricings, :tier2_program_pricing_id)
end
|