Skip to content

Commit 366fa27

Browse files
committed
Add LineItem#previous_lane_discounts
This can be used to calculate the current discountable amount while calculating promotions.
1 parent b1a9cd3 commit 366fa27

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusPromotions
4+
module Discountable
5+
def current_lane_discounts
6+
raise NotCalculatingPromotions unless Promotion.current_lane
7+
8+
discounts_by_lanes([Promotion.current_lane])
9+
end
10+
11+
def previous_lane_discounts
12+
raise NotCalculatingPromotions unless Promotion.current_lane
13+
14+
discounts_by_lanes(Promotion.lanes_before_current_lane)
15+
end
16+
end
17+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusPromotions
4+
class NotCalculatingPromotions < StandardError; end
5+
end

promotions/app/patches/models/solidus_promotions/line_item_patch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def reset_quantity_setter
2323

2424
Spree::LineItem.prepend self
2525
Spree::LineItem.prepend SolidusPromotions::AdjustmentDiscounts
26+
Spree::LineItem.prepend SolidusPromotions::Discountable
2627
Spree::LineItem.prepend SolidusPromotions::DiscountableAmount
2728
end
2829
end

promotions/spec/models/spree/line_item_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,88 @@
7373
end
7474
end
7575

76+
describe "#previous_lane_discounts" do
77+
let(:order) { Spree::Order.new }
78+
let(:tax_rate) { create(:tax_rate) }
79+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
80+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
81+
let(:line_item) { Spree::LineItem.new(adjustments:, order:) }
82+
let(:adjustments) { [tax_adjustment, pre_lane_adjustment, post_lane_adjustment] }
83+
let(:tax_adjustment) { Spree::Adjustment.new(source: tax_rate, amount: 2) }
84+
let(:pre_lane_adjustment) { Spree::Adjustment.new(source: pre_lane_promotion.benefits.first) }
85+
let(:post_lane_adjustment) { Spree::Adjustment.new(source: post_lane_promotion.benefits.first) }
86+
87+
subject { line_item.previous_lane_discounts }
88+
89+
it "raises unless we're doing a promotion calculation" do
90+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
91+
end
92+
93+
context "while calculating promotions" do
94+
around do |example|
95+
SolidusPromotions::Promotion.within_lane(lane) do
96+
example.run
97+
end
98+
end
99+
100+
let(:lane) { "pre" }
101+
it { is_expected.to be_empty }
102+
103+
context "if lane is default" do
104+
let(:lane) { "default" }
105+
106+
it { is_expected.to contain_exactly(pre_lane_adjustment) }
107+
end
108+
109+
context "if lane is post" do
110+
let(:lane) { "post" }
111+
112+
it { is_expected.to contain_exactly(pre_lane_adjustment) }
113+
end
114+
end
115+
end
116+
117+
describe "#current_lane_discounts" do
118+
let(:order) { Spree::Order.new }
119+
let(:tax_rate) { create(:tax_rate) }
120+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
121+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
122+
let(:line_item) { Spree::LineItem.new(adjustments:, order:) }
123+
let(:adjustments) { [tax_adjustment, pre_lane_adjustment, post_lane_adjustment] }
124+
let(:tax_adjustment) { Spree::Adjustment.new(source: tax_rate, amount: 2) }
125+
let(:pre_lane_adjustment) { Spree::Adjustment.new(source: pre_lane_promotion.benefits.first) }
126+
let(:post_lane_adjustment) { Spree::Adjustment.new(source: post_lane_promotion.benefits.first) }
127+
128+
subject { line_item.current_lane_discounts }
129+
130+
it "raises unless we're doing a promotion calculation" do
131+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
132+
end
133+
134+
context "while calculating promotions" do
135+
around do |example|
136+
SolidusPromotions::Promotion.within_lane(lane) do
137+
example.run
138+
end
139+
end
140+
141+
let(:lane) { "pre" }
142+
it { is_expected.to contain_exactly(pre_lane_adjustment) }
143+
144+
context "if lane is default" do
145+
let(:lane) { "default" }
146+
147+
it { is_expected.to be_empty }
148+
end
149+
150+
context "if lane is post" do
151+
let(:lane) { "post" }
152+
153+
it { is_expected.to contain_exactly(post_lane_adjustment) }
154+
end
155+
end
156+
end
157+
76158
describe "changing quantities" do
77159
context "when line item is managed by an automation" do
78160
let(:order) { create(:order) }

0 commit comments

Comments
 (0)