Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion promotions/app/models/solidus_promotions/benefit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Benefit < Spree::Base
# @!attribute [rw] promotion
# The owning promotion.
# @return [SolidusPromotions::Promotion]
belongs_to :promotion, inverse_of: :benefits
belongs_to :promotion, -> { with_discarded }, inverse_of: :benefits
# @!attribute [rw] original_promotion_action
# Back-reference to the original Solidus (Spree) promotion action, when migrated.
# @return [Spree::PromotionAction, nil]
Expand Down
3 changes: 2 additions & 1 deletion promotions/app/models/solidus_promotions/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Promotion < Spree::Base
optional: true,
inverse_of: :promotions
belongs_to :original_promotion, class_name: "Spree::Promotion", optional: true
has_many :benefits, class_name: "SolidusPromotions::Benefit", dependent: :destroy
has_many :benefits, class_name: "SolidusPromotions::Benefit", dependent: :destroy, inverse_of: :promotion
has_many :conditions, through: :benefits
has_many :codes, class_name: "SolidusPromotions::PromotionCode", dependent: :destroy, inverse_of: :promotion
has_many :code_batches, class_name: "SolidusPromotions::PromotionCodeBatch", dependent: :destroy
Expand Down Expand Up @@ -174,6 +174,7 @@ def apply_automatically_disallowed_with_promotion_codes

def delete_cart_connections
order_promotions.where(order: Spree::Order.incomplete).destroy_all
benefits.each(&:remove_adjustments_from_incomplete_orders)
end
end
end
10 changes: 10 additions & 0 deletions promotions/spec/models/solidus_promotions/benefit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
it { is_expected.to respond_to :discount }
it { is_expected.to respond_to :can_discount? }

describe "promotion association" do
let(:promotion) { create(:solidus_promotion, :with_adjustable_benefit) }
let(:benefit) { promotion.benefits.first }

it "loads discarded promotions" do
promotion.discard!
expect(benefit.reload.promotion).to eq(promotion)
end
end

describe "#can_adjust?" do
let(:adjustable) { Spree::LineItem.new }
let(:benefit_class) do
Expand Down
11 changes: 11 additions & 0 deletions promotions/spec/models/solidus_promotions/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@
it "destroys the connection" do
expect { subject }.to change(SolidusPromotions::OrderPromotion, :count).by(-1)
end

it "removes adjustments from incomplete orders" do
benefit = promotion.benefits.first
order.adjustments.create!(
source: benefit,
order: order,
amount: -10.0,
label: "Test Adjustment"
)
expect { subject }.to change { order.adjustments.reload.count }.from(1).to(0)
end
end

context "when the promotion has been added to a complete order" do
Expand Down
14 changes: 14 additions & 0 deletions promotions/spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@

it { is_expected.to eq(26) }
end

context "with an adjustment from a discarded promotion" do
before do
pre_lane_promotion.discard!
end

it "does not crash when accessing promotion.lane" do
expect { subject }.not_to raise_error
end

it "still calculates the discounted amount correctly" do
is_expected.to eq(23)
end
end
end

describe "#current_lane_discounts" do
Expand Down
Loading