diff --git a/promotions/app/models/solidus_promotions/benefit.rb b/promotions/app/models/solidus_promotions/benefit.rb index 5128a500eaa..e0a645fa650 100644 --- a/promotions/app/models/solidus_promotions/benefit.rb +++ b/promotions/app/models/solidus_promotions/benefit.rb @@ -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] diff --git a/promotions/app/models/solidus_promotions/promotion.rb b/promotions/app/models/solidus_promotions/promotion.rb index a80bf663339..d74fa5f20c0 100644 --- a/promotions/app/models/solidus_promotions/promotion.rb +++ b/promotions/app/models/solidus_promotions/promotion.rb @@ -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 @@ -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 diff --git a/promotions/spec/models/solidus_promotions/benefit_spec.rb b/promotions/spec/models/solidus_promotions/benefit_spec.rb index ededd39a4cf..0ada1c30018 100644 --- a/promotions/spec/models/solidus_promotions/benefit_spec.rb +++ b/promotions/spec/models/solidus_promotions/benefit_spec.rb @@ -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 diff --git a/promotions/spec/models/solidus_promotions/promotion_spec.rb b/promotions/spec/models/solidus_promotions/promotion_spec.rb index 8b706915f97..a68098c675d 100644 --- a/promotions/spec/models/solidus_promotions/promotion_spec.rb +++ b/promotions/spec/models/solidus_promotions/promotion_spec.rb @@ -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 diff --git a/promotions/spec/models/spree/line_item_spec.rb b/promotions/spec/models/spree/line_item_spec.rb index 7438be8955c..6a885a1b890 100644 --- a/promotions/spec/models/spree/line_item_spec.rb +++ b/promotions/spec/models/spree/line_item_spec.rb @@ -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