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/item_total.rb,
app/models/spree/promotion/rules/first_order.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_line_items.rb,
app/models/spree/promotion/actions/create_item_adjustments.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
Methods inherited from Base
page
#clear_preferences, #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
.active ⇒ Object
43
44
45
46
|
# File 'app/models/spree/promotion.rb', line 43
def self.active
where('starts_at IS NULL OR starts_at < ?', Time.now).
where('expires_at IS NULL OR expires_at > ?', Time.now)
end
|
.advertised ⇒ Object
35
36
37
|
# File 'app/models/spree/promotion.rb', line 35
def self.advertised
where(advertise: true)
end
|
.order_activatable?(order) ⇒ Boolean
48
49
50
|
# File 'app/models/spree/promotion.rb', line 48
def self.order_activatable?(order)
order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end
|
.with_coupon_code(coupon_code) ⇒ Object
39
40
41
|
# File 'app/models/spree/promotion.rb', line 39
def self.with_coupon_code(coupon_code)
where("lower(#{self.table_name}.code) = ?", coupon_code.strip.downcase).first
end
|
Instance Method Details
#activate(payload) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'app/models/spree/promotion.rb', line 56
def activate(payload)
order = payload[:order]
return unless self.class.order_activatable?(order)
payload[:promotion] = self
results = actions.map do |action|
action.perform(payload)
end
action_taken = results.include?(true)
if action_taken
self.orders << order
self.save
end
return action_taken
end
|
#adjusted_credits_count(promotable) ⇒ Object
122
123
124
|
# File 'app/models/spree/promotion.rb', line 122
def adjusted_credits_count(promotable)
credits_count - promotable.adjustments.promotion.where(:source_id => actions.pluck(:id)).count
end
|
#credits ⇒ Object
126
127
128
|
# File 'app/models/spree/promotion.rb', line 126
def credits
Adjustment.eligible.promotion.where(source_id: actions.map(&:id))
end
|
#credits_count ⇒ Object
130
131
132
|
# File 'app/models/spree/promotion.rb', line 130
def credits_count
credits.count
end
|
#eligible?(promotable) ⇒ Boolean
called anytime order.update! happens
82
83
84
85
|
# File 'app/models/spree/promotion.rb', line 82
def eligible?(promotable)
return false if expired? || usage_limit_exceeded?(promotable) || 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)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'app/models/spree/promotion.rb', line 90
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
52
53
54
|
# File 'app/models/spree/promotion.rb', line 52
def expired?
!!(starts_at && Time.now < starts_at || expires_at && Time.now > expires_at)
end
|
#line_item_actionable?(order, line_item) ⇒ Boolean
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'app/models/spree/promotion.rb', line 134
def line_item_actionable?(order, line_item)
if eligible? order
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
114
115
116
|
# File 'app/models/spree/promotion.rb', line 114
def products
rules.where(type: "Spree::Promotion::Rules::Product").map(&:products).flatten.uniq
end
|
#usage_limit_exceeded?(promotable) ⇒ Boolean
118
119
120
|
# File 'app/models/spree/promotion.rb', line 118
def usage_limit_exceeded?(promotable)
usage_limit.present? && usage_limit > 0 && adjusted_credits_count(promotable) >= usage_limit
end
|
#used_by?(user, excluded_orders = []) ⇒ Boolean
149
150
151
|
# File 'app/models/spree/promotion.rb', line 149
def used_by?(user, excluded_orders = [])
orders.where.not(id: excluded_orders.map(&:id)).complete.where(user_id: user.id).exists?
end
|