Skip to content

Commit e43adaf

Browse files
committed
Add Promotable#current_lane_discounts
When we refactor the promotion system to use in-memory adjustments/discounts to store discounts, we need to be able to ask which discounts are in the current promotion lane. This allows that.
1 parent de9eb6f commit e43adaf

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

promotions/app/models/concerns/solidus_promotions/discounted_amount.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ def discounted_amount
99
amount + previous_lane_discounts.sum(&:amount)
1010
end
1111

12+
def current_lane_discounts
13+
raise NotCalculatingPromotions unless PromotionLane.current
14+
15+
discounts_by_lanes([PromotionLane.current])
16+
end
17+
1218
private
1319

1420
# Returns discount objects added by promotion in lanes that come before the current lane.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusPromotions
4+
class NotCalculatingPromotions < StandardError
5+
end
6+
end

promotions/spec/models/spree/line_item_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,46 @@
9191
it { is_expected.to eq(26) }
9292
end
9393
end
94+
95+
describe "#current_lane_discounts" do
96+
let(:order) { Spree::Order.new }
97+
let(:tax_rate) { create(:tax_rate) }
98+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
99+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
100+
let(:line_item) { Spree::LineItem.new(adjustments:, order:, price: 14, quantity: 2) }
101+
let(:adjustments) { [tax_adjustment, pre_lane_adjustment, post_lane_adjustment] }
102+
let(:tax_adjustment) { Spree::Adjustment.new(source: tax_rate, amount: 2) }
103+
let(:pre_lane_adjustment) { Spree::Adjustment.new(source: pre_lane_promotion.benefits.first, amount: -3) }
104+
let(:post_lane_adjustment) { Spree::Adjustment.new(source: post_lane_promotion.benefits.first, amount: -2) }
105+
106+
subject { line_item.current_lane_discounts }
107+
108+
it "raises an exception when there is no current lane" do
109+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
110+
end
111+
112+
context "when in pre lane" do
113+
before do
114+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "pre" }
115+
end
116+
117+
it { is_expected.to contain_exactly(pre_lane_adjustment) }
118+
end
119+
120+
context "when in default lane" do
121+
before do
122+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "default" }
123+
end
124+
125+
it { is_expected.to be_empty }
126+
end
127+
128+
context "when in post lane" do
129+
before do
130+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "post" }
131+
end
132+
133+
it { is_expected.to contain_exactly(post_lane_adjustment) }
134+
end
135+
end
94136
end

promotions/spec/models/spree/shipment_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,46 @@
7070
it { is_expected.to eq(12) }
7171
end
7272
end
73+
74+
describe "#current_lane_discounts" do
75+
let(:order) { Spree::Order.new }
76+
let(:tax_rate) { create(:tax_rate) }
77+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
78+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
79+
let(:shipment) { Spree::Shipment.new(adjustments:, order:, cost: 14) }
80+
let(:adjustments) { [tax_adjustment, pre_lane_adjustment, post_lane_adjustment] }
81+
let(:tax_adjustment) { Spree::Adjustment.new(source: tax_rate, amount: 2) }
82+
let(:pre_lane_adjustment) { Spree::Adjustment.new(source: pre_lane_promotion.benefits.first, amount: -3) }
83+
let(:post_lane_adjustment) { Spree::Adjustment.new(source: post_lane_promotion.benefits.first, amount: -2) }
84+
85+
subject { shipment.current_lane_discounts }
86+
87+
it "raises an exception when there is no current lane" do
88+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
89+
end
90+
91+
context "when in pre lane" do
92+
before do
93+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "pre" }
94+
end
95+
96+
it { is_expected.to contain_exactly(pre_lane_adjustment) }
97+
end
98+
99+
context "when in default lane" do
100+
before do
101+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "default" }
102+
end
103+
104+
it { is_expected.to be_empty }
105+
end
106+
107+
context "when in post lane" do
108+
before do
109+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "post" }
110+
end
111+
112+
it { is_expected.to contain_exactly(post_lane_adjustment) }
113+
end
114+
end
73115
end

promotions/spec/models/spree/shipping_rate_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,45 @@
108108
it { is_expected.to eq(12) }
109109
end
110110
end
111+
112+
describe "#current_lane_discounts" do
113+
let(:order) { Spree::Order.new }
114+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
115+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
116+
let(:shipment) { Spree::Shipment.new(order:) }
117+
let(:shipping_rate) { Spree::ShippingRate.new(discounts:, shipment:, amount: 14) }
118+
let(:discounts) { [pre_lane_discount, post_lane_discount] }
119+
let(:pre_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: pre_lane_promotion.benefits.first, amount: -3) }
120+
let(:post_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: post_lane_promotion.benefits.first, amount: -2) }
121+
122+
subject { shipping_rate.current_lane_discounts }
123+
124+
it "raises an exception when there is no current lane" do
125+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
126+
end
127+
128+
context "when in pre lane" do
129+
before do
130+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "pre" }
131+
end
132+
133+
it { is_expected.to contain_exactly(pre_lane_discount) }
134+
end
135+
136+
context "when in default lane" do
137+
before do
138+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "default" }
139+
end
140+
141+
it { is_expected.to be_empty }
142+
end
143+
144+
context "when in post lane" do
145+
before do
146+
allow(SolidusPromotions::PromotionLane).to receive(:current) { "post" }
147+
end
148+
149+
it { is_expected.to contain_exactly(post_lane_discount) }
150+
end
151+
end
111152
end

0 commit comments

Comments
 (0)