Class: Coupon::QueryBuilder
Overview
Service object: query builder.
Instance Attribute Summary
#query
Instance Method Summary
collapse
#array_check, #habtm_check, #reflect
Constructor Details
#initialize(query = nil, options = {}) ⇒ QueryBuilder
When initializing this class make sure to always pass a nil if you
run into a wrong number of arguments (0 for 1) error, for
reasons i cannot explain the default = nil is ignored and it tried
to initialize the superclass instead
11
12
13
14
15
16
17
|
# File 'app/services/coupon/query_builder.rb', line 11
def initialize(query = nil, options = {})
@options = options
@auto_apply_coupon = @options[:auto_apply_coupon]
query ||= Coupon.unscoped.active
query = query.where(auto_apply: true) if @auto_apply_coupon
super(query)
end
|
Instance Method Details
#for_customer(customer) ⇒ Object
Returns all the coupons applicable for a given customer
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'app/services/coupon/query_builder.rb', line 32
def for_customer(customer)
qb = CustomerFilter::QueryBuilder.new(nil)
matching_customer_filters_ids = qb.for_customer(customer).query.ids
matching_customer_filters_ids << nil
reflect(
if @auto_apply_coupon
query.where(customer_filter_id: matching_customer_filters_ids, auto_apply_customer_filter_id: matching_customer_filters_ids)
else
query.where(customer_filter_id: matching_customer_filters_ids)
end
)
end
|
#for_itemizable(itemizable) ⇒ Object
Returns all the coupons applicable for a given itemizable, Order or Quote
20
21
22
23
24
25
26
27
28
29
|
# File 'app/services/coupon/query_builder.rb', line 20
def for_itemizable(itemizable)
case itemizable.class.name
when 'Order'
for_order(itemizable)
when 'Quote'
for_quote(itemizable)
else
raise "Unsupported itemizable type"
end
end
|
#for_order(order) ⇒ Object
Returns all the coupons applicable for a given itemizable
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'app/services/coupon/query_builder.rb', line 63
def for_order(order)
customer = order.customer
shipping_state = order.shipping_address.try(:state_code)
reflect(
for_customer(customer)
.with_effective_date(order.effective_date_for_coupon)
.with_shipping_state_code(shipping_state)
.with_order_type(order.order_type)
.with_ships_economy(order.ships_economy_package?)
.with_order_reception_type(order.order_reception_type)
.query
)
end
|
#for_quote(quote, _customer_association_mode = :customer_filter) ⇒ Object
Params:
quote
customer_association_mode, one of :customer_filter or :auto_apply_customer_filter
the filtering might be different for determining the auto apply rule.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/services/coupon/query_builder.rb', line 81
def for_quote(quote, _customer_association_mode = :customer_filter)
customer = quote.customer
shipping_state = quote.shipping_address.try(:state_code) || quote.opportunity.state_code
reflect(
for_customer(customer)
.with_effective_date(quote.effective_date_for_coupon)
.with_shipping_state_code(shipping_state)
.with_opportunity_reception_type(quote.opportunity.opportunity_reception_type)
.with_opportunity_type(quote.opportunity.opportunity_type)
.with_ships_economy(quote.ships_economy_package?)
.query
)
end
|
#for_specific_customer(customer) ⇒ Object
Only look at coupons using a coupon expressely setup for customer ids
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'app/services/coupon/query_builder.rb', line 46
def for_specific_customer(customer)
matching_customer_filters_ids = CustomerFilter.where("
exists(select 1 from customer_filters_parties
where customer_filters_parties.party_id = ?
and customer_filters_parties.customer_filter_id = customer_filters.id )",
customer.id)
reflect(
if matching_customer_filters_ids.present?
query.where(customer_filter_id: matching_customer_filters_ids)
else
query.none
end
)
end
|
#with_effective_date(date) ⇒ Object
96
97
98
99
100
|
# File 'app/services/coupon/query_builder.rb', line 96
def with_effective_date(date)
reflect(
query.in_effect_on(date)
)
end
|
#with_exclude_coupon_ids(exclude_coupon_ids) ⇒ Object
155
156
157
158
159
160
161
162
163
|
# File 'app/services/coupon/query_builder.rb', line 155
def with_exclude_coupon_ids(exclude_coupon_ids)
reflect(
if exclude_coupon_ids.present?
query.where.not(id: exclude_coupon_ids)
else
query
end
)
end
|
#with_include_coupon_ids(coupon_ids) ⇒ Object
165
166
167
168
169
170
171
172
173
|
# File 'app/services/coupon/query_builder.rb', line 165
def with_include_coupon_ids(coupon_ids)
reflect(
if coupon_ids.present?
query.where(id: coupon_ids)
else
query
end
)
end
|
#with_opportunity_reception_type(reception_type) ⇒ Object
127
128
129
|
# File 'app/services/coupon/query_builder.rb', line 127
def with_opportunity_reception_type(reception_type)
array_check(:opportunity_reception_types_filter, reception_type)
end
|
#with_opportunity_type(opportunity_type) ⇒ Object
131
132
133
|
# File 'app/services/coupon/query_builder.rb', line 131
def with_opportunity_type(opportunity_type)
array_check(:opportunity_types_filter, opportunity_type)
end
|
#with_order_reception_type(reception_type) ⇒ Object
123
124
125
|
# File 'app/services/coupon/query_builder.rb', line 123
def with_order_reception_type(reception_type)
array_check(:order_reception_types_filter, reception_type)
end
|
#with_order_type(order_type) ⇒ Object
119
120
121
|
# File 'app/services/coupon/query_builder.rb', line 119
def with_order_type(order_type)
array_check(:order_types_filter, order_type)
end
|
#with_role_ids(role_ids_restriction) ⇒ Object
Coupon has to be modifiable by one of the specified roles or be empty
This is not smart, make sure you feed me all roles to check including
descendants, not this class's responsibility
105
106
107
108
109
110
111
112
113
|
# File 'app/services/coupon/query_builder.rb', line 105
def with_role_ids(role_ids_restriction)
reflect(
if role_ids_restriction.present?
query.where("coupons.role_ids_restriction = '{}' or ? && coupons.role_ids_restriction", "{#{role_ids_restriction.join(',')}}")
else
query
end
)
end
|
#with_search_query(search_query, wild_search: true) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'app/services/coupon/query_builder.rb', line 175
def with_search_query(search_query, wild_search: true)
reflect(
if search_query.present?
subquery_serial_number = "exists(select 1 from coupon_serial_numbers csn where NOT csn.used AND csn.id::varchar = :search_term_fixed AND csn.coupon_id = coupons.id)"
subquery_serial_number_col = ActiveRecord::Base.escape_sql("(select csn.id from coupon_serial_numbers csn where NOT csn.used AND csn.id::varchar = ? AND csn.coupon_id = coupons.id limit 1)", search_query)
if wild_search
query.select_append("#{subquery_serial_number_col} as matched_serial_number").where("coupons.code ilike :search_term or title ilike :search_term or #{subquery_serial_number}", search_term: "%#{search_query}%",
search_term_fixed: search_query)
else
query.select_append("#{subquery_serial_number_col} as matched_serial_number").where(
"(NOT coupons.serial_numbers_only AND (coupons.code ilike :search_term OR #{subquery_serial_number})) OR (coupons.serial_numbers_only AND #{subquery_serial_number})", search_term: search_query, search_term_fixed: search_query
)
end
else
query
end
)
end
|
#with_searchable_only(searchable_only) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'app/services/coupon/query_builder.rb', line 145
def with_searchable_only(searchable_only)
reflect(
if searchable_only
query.tier1and3.active
else
query
end
)
end
|
#with_shipping_state_code(state_code) ⇒ Object
115
116
117
|
# File 'app/services/coupon/query_builder.rb', line 115
def with_shipping_state_code(state_code)
array_check(:state_codes, state_code)
end
|
#with_ships_economy(ships_economy) ⇒ Object
135
136
137
138
139
140
141
142
143
|
# File 'app/services/coupon/query_builder.rb', line 135
def with_ships_economy(ships_economy)
reflect(
if ships_economy
query
else
query.exclude_ships_economy_only
end
)
end
|