From 6f03e9fb655e07c966dd5440fb2e6c2e7498d196 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 4 Oct 2023 13:48:07 +0200 Subject: [PATCH] Add dependent: :destroy to Spree::Order#friendly_promotions This is the same change as in https://github.com/solidusio/solidus/pull/5411 --- .../solidus_friendly_promotions/order_decorator.rb | 1 + .../spec/models/spree/order_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb index a70817c8a4e..f6130deff41 100644 --- a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb @@ -5,6 +5,7 @@ module OrderDecorator def self.prepended(base) base.has_many :friendly_order_promotions, class_name: "SolidusFriendlyPromotions::OrderPromotion", + dependent: :destroy, inverse_of: :order base.has_many :friendly_promotions, through: :friendly_order_promotions, source: :promotion end diff --git a/friendly_promotions/spec/models/spree/order_spec.rb b/friendly_promotions/spec/models/spree/order_spec.rb index 15ee8d76024..6be2cbff349 100644 --- a/friendly_promotions/spec/models/spree/order_spec.rb +++ b/friendly_promotions/spec/models/spree/order_spec.rb @@ -19,4 +19,18 @@ subject end end + + describe "order deletion" do + let(:order) { create(:order) } + let(:promotion) { create(:friendly_promotion) } + + subject { order.destroy } + before do + order.friendly_promotions << promotion + end + + it "deletes join table entries when deleting an order" do + expect { subject }.to change { SolidusFriendlyPromotions::OrderPromotion.count }.from(1).to(0) + end + end end