Skip to content

Commit a101831

Browse files
committed
Add Discountable module to shipping rates
1 parent 7384590 commit a101831

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def promo_total
2323
discounts.sum(&:amount)
2424
end
2525

26+
def discounts_by_lanes(lanes)
27+
discounts.select do |discount|
28+
discount.benefit.promotion.lane.to_sym.in?(lanes.map(&:to_sym))
29+
end
30+
end
31+
32+
Spree::ShippingRate.prepend SolidusPromotions::Discountable
2633
Spree::ShippingRate.prepend SolidusPromotions::DiscountableAmount
2734
Spree::ShippingRate.prepend self
2835
end

promotions/spec/models/spree/shipping_rate_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,86 @@
7575
expect { subject }.to change { shipping_rate.current_discounts.length }.from(1).to(0)
7676
end
7777
end
78+
79+
describe "#previous_lane_discounts" do
80+
let(:order) { Spree::Order.new }
81+
let(:shipment) { Spree::Shipment.new(order:) }
82+
let(:tax_rate) { create(:tax_rate) }
83+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
84+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
85+
let(:shipping_rate) { Spree::ShippingRate.new(discounts:, shipment:) }
86+
let(:discounts) { [pre_lane_discount, post_lane_discount] }
87+
let(:pre_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: pre_lane_promotion.benefits.first) }
88+
let(:post_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: post_lane_promotion.benefits.first) }
89+
90+
subject { shipping_rate.previous_lane_discounts }
91+
92+
it "raises unless we're doing a promotion calculation" do
93+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
94+
end
95+
96+
context "while calculating promotions" do
97+
around do |example|
98+
SolidusPromotions::Promotion.within_lane(lane) do
99+
example.run
100+
end
101+
end
102+
103+
let(:lane) { "pre" }
104+
it { is_expected.to be_empty }
105+
106+
context "if lane is default" do
107+
let(:lane) { "default" }
108+
109+
it { is_expected.to contain_exactly(pre_lane_discount) }
110+
end
111+
112+
context "if lane is post" do
113+
let(:lane) { "post" }
114+
115+
it { is_expected.to contain_exactly(pre_lane_discount) }
116+
end
117+
end
118+
end
119+
120+
describe "#current_lane_discounts" do
121+
let(:order) { Spree::Order.new }
122+
let(:shipment) { Spree::Shipment.new(order:) }
123+
let(:tax_rate) { create(:tax_rate) }
124+
let(:pre_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :pre) }
125+
let(:post_lane_promotion) { create(:solidus_promotion, :with_adjustable_benefit, lane: :post) }
126+
let(:shipping_rate) { Spree::ShippingRate.new(discounts:, shipment:) }
127+
let(:discounts) { [pre_lane_discount, post_lane_discount] }
128+
let(:pre_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: pre_lane_promotion.benefits.first) }
129+
let(:post_lane_discount) { SolidusPromotions::ShippingRateDiscount.new(benefit: post_lane_promotion.benefits.first) }
130+
131+
subject { shipping_rate.current_lane_discounts }
132+
133+
it "raises unless we're doing a promotion calculation" do
134+
expect { subject }.to raise_exception(SolidusPromotions::NotCalculatingPromotions)
135+
end
136+
137+
context "while calculating promotions" do
138+
around do |example|
139+
SolidusPromotions::Promotion.within_lane(lane) do
140+
example.run
141+
end
142+
end
143+
144+
let(:lane) { "pre" }
145+
it { is_expected.to contain_exactly(pre_lane_discount) }
146+
147+
context "if lane is default" do
148+
let(:lane) { "default" }
149+
150+
it { is_expected.to be_empty }
151+
end
152+
153+
context "if lane is post" do
154+
let(:lane) { "post" }
155+
156+
it { is_expected.to contain_exactly(post_lane_discount) }
157+
end
158+
end
159+
end
78160
end

0 commit comments

Comments
 (0)