Skip to content

Commit 43366de

Browse files
committed
Deprecate implementing #discount on benefits
Instead, follow the deprecation message's instructions.
1 parent 8e11e47 commit 43366de

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

promotions/app/models/solidus_promotions/benefit.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,53 @@ def discount(adjustable, ...)
113113
end
114114
end
115115

116+
def self.inherited(klass)
117+
def klass.method_added(method_added)
118+
if method_added == :discount
119+
Spree.deprecator.warn <<~MSG
120+
Please refactor `#{name}`. You're defining `#discount`. Instead, define a method for each type of discountable
121+
that your benefit can discount. For example:
122+
```
123+
class MyBenefit < SolidusPromotions::Benefit
124+
def can_discount?(discountable)
125+
discountable.is_a?(Spree::LineItem)
126+
end
127+
128+
def discount(order, _options = {})
129+
amount = compute_amount(line_item, ...)
130+
return if amount.zero?
131+
132+
ItemDiscount.new(
133+
item: line_item,
134+
label: adjustment_label(line_item),
135+
amount: amount,
136+
source: self
137+
)
138+
end
139+
```
140+
can now become
141+
```
142+
class MyBenefit < SolidusPromotions::Benefit
143+
def discount_line_item?(order, ...)
144+
amount = compute_amount(line_item, ...)
145+
return if amount.zero?
146+
147+
ItemDiscount.new(
148+
item: line_item,
149+
label: adjustment_label(line_item),
150+
amount: amount,
151+
source: self
152+
)
153+
end
154+
end
155+
```
156+
MSG
157+
end
158+
super
159+
end
160+
super
161+
end
162+
116163
# Computes the discount amount for the given adjustable.
117164
#
118165
# Ensures the returned amount is negative and does not exceed the

promotions/spec/models/solidus_promotions/benefit_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,79 @@ def compute_line_item(_line_item, _options) = 1
148148
end
149149
end
150150

151+
describe "inherited hook" do
152+
context "for a well-formed benefit" do
153+
subject(:benefit) do
154+
Class.new(described_class) do
155+
def discount_line_item(_line_item, _options = {})
156+
true
157+
end
158+
end
159+
end
160+
161+
it "does not emit a deprecation warning" do
162+
expect(Spree.deprecator).not_to receive(:warn)
163+
benefit
164+
end
165+
end
166+
167+
context "for a legacy benefit" do
168+
subject(:benefit) do
169+
Class.new(described_class) do
170+
def self.name
171+
"LegacyBenefit"
172+
end
173+
174+
def discount(_line_item, _options = {})
175+
true
176+
end
177+
end
178+
end
179+
180+
it "emits a deprecation warning" do
181+
expect(Spree.deprecator).to receive(:warn).with(<<~MSG)
182+
Please refactor `LegacyBenefit`. You're defining `#discount`. Instead, define a method for each type of discountable
183+
that your benefit can discount. For example:
184+
```
185+
class MyBenefit < SolidusPromotions::Benefit
186+
def can_discount?(discountable)
187+
discountable.is_a?(Spree::LineItem)
188+
end
189+
190+
def discount(order, _options = {})
191+
amount = compute_amount(line_item, ...)
192+
return if amount.zero?
193+
194+
ItemDiscount.new(
195+
item: line_item,
196+
label: adjustment_label(line_item),
197+
amount: amount,
198+
source: self
199+
)
200+
end
201+
```
202+
can now become
203+
```
204+
class MyBenefit < SolidusPromotions::Benefit
205+
def discount_line_item?(order, ...)
206+
amount = compute_amount(line_item, ...)
207+
return if amount.zero?
208+
209+
ItemDiscount.new(
210+
item: line_item,
211+
label: adjustment_label(line_item),
212+
amount: amount,
213+
source: self
214+
)
215+
end
216+
end
217+
```
218+
MSG
219+
benefit
220+
end
221+
end
222+
end
223+
151224
describe ".original_promotion_action" do
152225
let(:spree_promotion) { create :promotion, :with_adjustable_action }
153226
let(:spree_promotion_action) { spree_promotion.actions.first }

0 commit comments

Comments
 (0)