From 2a0f132b8b641ab1d18632e485f717b24cc435bd Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 8 Dec 2020 18:32:22 +0000 Subject: [PATCH] Improve ShippingMethod -> Shipment association https://github.com/spree/spree/commit/5a54a0b6b0abc9ea3b928a243c2daace9a36c1b1 --- app/models/spree/shipping_method.rb | 2 +- spec/models/spree/shipping_method_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index fd7b1e4415..38f8a41438 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -10,7 +10,7 @@ module Spree default_scope -> { where(deleted_at: nil) } - has_many :shipments + has_many :shipments, through: :shipping_rates has_many :shipping_method_categories has_many :shipping_categories, through: :shipping_method_categories has_many :shipping_rates, inverse_of: :shipping_method diff --git a/spec/models/spree/shipping_method_spec.rb b/spec/models/spree/shipping_method_spec.rb index 35ecfa6d83..c3c65d0215 100644 --- a/spec/models/spree/shipping_method_spec.rb +++ b/spec/models/spree/shipping_method_spec.rb @@ -176,5 +176,19 @@ module Spree expect(shipping_method.calculator.calculable).to eq(shipping_method) end end + + # Regression test for Spree #4492 + context "#shipments" do + let!(:shipping_method) { create(:shipping_method) } + let!(:shipment) do + shipment = create(:shipment) + shipment.shipping_rates.create!(shipping_method: shipping_method) + shipment + end + + it "can gather all the related shipments" do + expect(shipping_method.shipments).to include(shipment) + end + end end end