Skip to content

Commit fc3adf7

Browse files
authored
Merge pull request #6374 from mamhoff/add-autosave-to-shipment-shipping-rates
Autosave changed shipping rates
2 parents f54d7ff + 12a5ebe commit fc3adf7

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

core/app/models/spree/shipment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Shipment < Spree::Base
1111

1212
has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :delete_all, autosave: true
1313
has_many :inventory_units, dependent: :destroy, inverse_of: :shipment
14-
has_many :shipping_rates, -> { order(:cost) }, dependent: :destroy, inverse_of: :shipment
14+
has_many :shipping_rates, -> { order(:cost) }, dependent: :destroy, inverse_of: :shipment, autosave: true
1515
has_many :shipping_methods, through: :shipping_rates
1616
has_many :state_changes, as: :stateful
1717
has_many :cartons, -> { distinct }, through: :inventory_units

core/app/models/spree/shipping_rate.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ module Spree
55
# method has been selected to deliver the shipment.
66
#
77
class ShippingRate < Spree::Base
8-
belongs_to :shipment, class_name: 'Spree::Shipment', touch: true, optional: true
8+
belongs_to :shipment, class_name: 'Spree::Shipment', touch: true, optional: true, inverse_of: :shipping_rates
99
belongs_to :shipping_method, -> { with_discarded }, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates, optional: true
1010

1111
has_many :taxes,
1212
class_name: "Spree::ShippingRateTax",
1313
foreign_key: "shipping_rate_id",
1414
inverse_of: :shipping_rate,
15-
dependent: :destroy
15+
dependent: :destroy,
16+
autosave: true
1617

1718
delegate :order, :currency, to: :shipment
1819
delegate :name, :tax_category, :tax_category_id, to: :shipping_method

core/spec/models/spree/shipment_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,4 +1007,15 @@
10071007
expect(state_change.name).to eq('shipment')
10081008
end
10091009
end
1010+
1011+
describe "autosave behavior" do
1012+
let(:shipment) { create(:shipment) }
1013+
1014+
it "allows marking shipping rates for destruction" do
1015+
shipment.shipping_rates.load
1016+
shipment.shipping_rates.first.mark_for_destruction
1017+
1018+
expect { shipment.save! }.to change(Spree::ShippingRate, :count)
1019+
end
1020+
end
10101021
end

core/spec/models/spree/shipping_rate_tax_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,19 @@ module Spree
7575
end
7676
end
7777
end
78+
79+
describe "autosaving when order is saved" do
80+
let(:tax_rate) { create(:tax_rate) }
81+
let(:shipping_rate) { create(:shipping_rate) }
82+
let(:shipment) { shipping_rate.shipment }
83+
84+
it "works" do
85+
shipping_rate.taxes.new(
86+
amount: -2,
87+
tax_rate: tax_rate
88+
)
89+
expect { shipment.save! }.to change { Spree::ShippingRateTax.count }
90+
end
91+
end
7892
end
7993
end

promotions/spec/models/solidus_promotions/shipping_rate_discount_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@
1212
it { is_expected.to respond_to(:amount) }
1313
it { is_expected.to respond_to(:display_amount) }
1414
it { is_expected.to respond_to(:label) }
15+
16+
describe "autosaving when order is saved" do
17+
let(:promotion) { create(:solidus_promotion, :with_free_shipping) }
18+
let(:benefit) { promotion.benefits.first }
19+
let(:shipping_rate) { create(:shipping_rate) }
20+
let(:shipment) { shipping_rate.shipment }
21+
22+
it "works" do
23+
shipping_rate.discounts.new(
24+
amount: -2,
25+
benefit: benefit,
26+
label: "Free Shipping"
27+
)
28+
expect { shipment.save! }.to change { SolidusPromotions::ShippingRateDiscount.count }
29+
end
30+
end
1531
end

0 commit comments

Comments
 (0)