From f9801af6376f3c47ae07fc8bf3cd8d8032131d8c Mon Sep 17 00:00:00 2001 From: Dean Holdren Date: Tue, 29 Nov 2016 20:33:21 -0500 Subject: [PATCH] delete shipping_rate_taxes when a shipment is destroyed --- core/app/models/spree/shipment.rb | 2 +- core/spec/models/spree/shipment_spec.rb | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 83c6d8885e7..d295081a0bf 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -7,7 +7,7 @@ class Shipment < Spree::Base has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :delete_all has_many :inventory_units, dependent: :destroy, inverse_of: :shipment - has_many :shipping_rates, -> { order(:cost) }, dependent: :delete_all + has_many :shipping_rates, -> { order(:cost) }, dependent: :destroy has_many :shipping_methods, through: :shipping_rates has_many :state_changes, as: :stateful has_many :cartons, -> { uniq }, through: :inventory_units diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb index 70788cf5832..a50469fb0e2 100644 --- a/core/spec/models/spree/shipment_spec.rb +++ b/core/spec/models/spree/shipment_spec.rb @@ -637,9 +637,21 @@ # Regression test for https://github.com/spree/spree/issues/3349 context "#destroy" do - it "destroys linked shipping_rates" do - reflection = Spree::Shipment.reflect_on_association(:shipping_rates) - expect(reflection.options[:dependent]).to be(:delete_all) + let(:shipping_rate) do + Spree::ShippingRate.create!( + shipping_method: shipping_method, + selected: true, + taxes: [Spree::ShippingRateTax.new(amount: 20)] + ) + end + it "destroys linked shipping_rates and shipping_rate_taxes" do + shipping_rate = shipment.shipping_rates.first + shipping_rate_tax = shipping_rate.taxes.first + + shipment.destroy + + expect{shipping_rate.reload}.to raise_error(ActiveRecord::RecordNotFound) + expect{shipping_rate_tax.reload}.to raise_error(ActiveRecord::RecordNotFound) end end