Class: Spree::Promotion
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::Promotion
show all
- Defined in:
- app/models/spree/promotion.rb,
app/models/spree/promotion/rules/user.rb,
app/models/spree/promotion/rules/taxon.rb,
app/models/spree/promotion/rules/product.rb,
app/models/spree/promotion/rules/nth_order.rb,
app/models/spree/promotion/rules/item_total.rb,
app/models/spree/promotion/rules/first_order.rb,
app/models/spree/promotion/rules/option_value.rb,
app/models/spree/promotion/rules/user_logged_in.rb,
app/models/spree/promotion/actions/free_shipping.rb,
app/models/spree/promotion/rules/one_use_per_user.rb,
app/models/spree/promotion/actions/create_adjustment.rb,
app/models/spree/promotion/actions/create_item_adjustments.rb,
app/models/spree/promotion/rules/first_repeat_purchase_since.rb
Defined Under Namespace
Modules: Actions, Rules
Constant Summary
collapse
- MATCH_POLICIES =
%w(all any)
- UNACTIVATABLE_ORDER_STATES =
["complete", "awaiting_return", "returned"]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil) ⇒ Object
-
#active? ⇒ Boolean
-
#as_json(options = {}) ⇒ Object
-
#code ⇒ Object
-
#code=(_val) ⇒ Object
-
#columns ⇒ Object
-
#eligible?(promotable, promotion_code: nil) ⇒ Boolean
called anytime order.update! happens.
-
#eligible_rules(promotable, options = {}) ⇒ Object
eligible_rules returns an array of promotion rules where eligible? is true for the promotable if there are no such rules, an empty array is returned if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible).
-
#expired? ⇒ Boolean
-
#inactive? ⇒ Boolean
-
#line_item_actionable?(order, line_item, promotion_code: nil) ⇒ Boolean
-
#products ⇒ Object
-
#usage_count ⇒ Integer
Number of times the code has been used overall.
-
#usage_limit_exceeded? ⇒ Boolean
Whether the promotion has exceeded it’s usage restrictions.
-
#used_by?(user, excluded_orders = []) ⇒ Boolean
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#eligibility_errors ⇒ Object
Returns the value of attribute eligibility_errors.
6
7
8
|
# File 'app/models/spree/promotion.rb', line 6
def eligibility_errors
@eligibility_errors
end
|
Class Method Details
.order_activatable?(order) ⇒ Boolean
53
54
55
|
# File 'app/models/spree/promotion.rb', line 53
def self.order_activatable?(order)
order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end
|
.with_coupon_code(val) ⇒ Object
65
66
67
68
69
|
# File 'app/models/spree/promotion.rb', line 65
def self.with_coupon_code(val)
joins(:codes).where(
PromotionCode.arel_table[:value].eq(val.downcase)
).first
end
|
Instance Method Details
#activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'app/models/spree/promotion.rb', line 94
def activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil)
return unless self.class.order_activatable?(order)
payload = {
order: order,
promotion: self,
line_item: line_item,
user: user,
path: path,
promotion_code: promotion_code
}
results = actions.map do |action|
action.perform(payload)
end
action_taken = results.include?(true)
if action_taken
order_promotions.find_or_create_by!(
order_id: order.id,
promotion_code_id: promotion_code.try!(:id)
)
end
action_taken
end
|
#active? ⇒ Boolean
76
77
78
79
|
# File 'app/models/spree/promotion.rb', line 76
def active?
(starts_at.nil? || starts_at < Time.current) &&
(expires_at.nil? || expires_at > Time.current)
end
|
#as_json(options = {}) ⇒ Object
71
72
73
74
|
# File 'app/models/spree/promotion.rb', line 71
def as_json(options = {})
options[:except] ||= :code
super
end
|
#code ⇒ Object
57
58
59
|
# File 'app/models/spree/promotion.rb', line 57
def code
raise "Attempted to call code on a Spree::Promotion. Promotions are now tied to multiple code records"
end
|
#code=(_val) ⇒ Object
61
62
63
|
# File 'app/models/spree/promotion.rb', line 61
def code=(_val)
raise "Attempted to call code= on a Spree::Promotion. Promotions are now tied to multiple code records"
end
|
#columns ⇒ Object
temporary code. remove after the column is dropped from the db.
49
50
51
|
# File 'app/models/spree/promotion.rb', line 49
def columns
super.reject { |column| column.name == "code" }
end
|
#eligible?(promotable, promotion_code: nil) ⇒ Boolean
called anytime order.update! happens
127
128
129
130
131
132
133
|
# File 'app/models/spree/promotion.rb', line 127
def eligible?(promotable, promotion_code: nil)
return false if inactive?
return false if usage_limit_exceeded?
return false if promotion_code && promotion_code.usage_limit_exceeded?
return false if blacklisted?(promotable)
!!eligible_rules(promotable, {})
end
|
#eligible_rules(promotable, options = {}) ⇒ Object
eligible_rules returns an array of promotion rules where eligible? is true for the promotable if there are no such rules, an empty array is returned if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible)
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'app/models/spree/promotion.rb', line 138
def eligible_rules(promotable, options = {})
return [] if rules.none?
eligible = lambda { |r| r.eligible?(promotable, options) }
specific_rules = rules.for(promotable)
return [] if specific_rules.none?
if match_all?
unless specific_rules.all?(&eligible)
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
specific_rules
else
unless specific_rules.any?(&eligible)
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
specific_rules.select(&eligible)
end
end
|
#expired? ⇒ Boolean
85
86
87
88
89
90
91
92
|
# File 'app/models/spree/promotion.rb', line 85
def expired?
Spree::Deprecation.warn <<-WARN.squish, caller
#expired? is deprecated, and will be removed in Solidus 2.0.
Please use #inactive? instead.
WARN
inactive?
end
|
#inactive? ⇒ Boolean
81
82
83
|
# File 'app/models/spree/promotion.rb', line 81
def inactive?
!active?
end
|
#line_item_actionable?(order, line_item, promotion_code: nil) ⇒ Boolean
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'app/models/spree/promotion.rb', line 188
def line_item_actionable?(order, line_item, promotion_code: nil)
return false if blacklisted?(line_item)
if eligible?(order, promotion_code: promotion_code)
rules = eligible_rules(order)
if rules.blank?
true
else
rules.send(match_all? ? :all? : :any?) do |rule|
rule.actionable? line_item
end
end
else
false
end
end
|
#products ⇒ Object
162
163
164
|
# File 'app/models/spree/promotion.rb', line 162
def products
rules.where(type: "Spree::Promotion::Rules::Product").map(&:products).flatten.uniq
end
|
#usage_count ⇒ Integer
Number of times the code has been used overall
178
179
180
181
182
183
184
185
186
|
# File 'app/models/spree/promotion.rb', line 178
def usage_count
Spree::Adjustment.eligible.
promotion.
where(source_id: actions.map(&:id)).
joins(:order).
merge(Spree::Order.complete).
distinct.
count(:order_id)
end
|
#usage_limit_exceeded? ⇒ Boolean
Whether the promotion has exceeded it’s usage restrictions.
169
170
171
172
173
|
# File 'app/models/spree/promotion.rb', line 169
def usage_limit_exceeded?
if usage_limit
usage_count >= usage_limit
end
end
|
#used_by?(user, excluded_orders = []) ⇒ Boolean
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'app/models/spree/promotion.rb', line 205
def used_by?(user, excluded_orders = [])
[
:adjustments,
:line_item_adjustments,
:shipment_adjustments
].any? do |adjustment_type|
user.orders.complete.joins(adjustment_type).where(
spree_adjustments: {
source_type: "Spree::PromotionAction",
source_id: actions.map(&:id),
eligible: true
}
).where.not(
id: excluded_orders.map(&:id)
).any?
end
end
|