Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Handle the case when shipment's shipping method is nil when deciding whether shipment is digital (#12544)
Damian Kaczmarczykc6d8bf291c12
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex d059b46558..0e76f83dc8 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -230,7 +230,7 @@ module Spreeenddef digital?- shipping_method.calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery)+ shipping_method&.calculator&.is_a?(Spree::Calculator::Shipping::DigitalDelivery)endManifestItem = Struct.new(:line_item, :variant, :quantity, :states)diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex 32863f42cc..618e12e558 100644--- a/core/spec/models/spree/shipment_spec.rb+++ b/core/spec/models/spree/shipment_spec.rb@@ -1,20 +1,18 @@require 'spec_helper'describe Spree::Shipment, type: :model do- it_behaves_like 'metadata'-- let(:store) { @default_store }- let!(:order) { create(:order, number: 'S12345', store: store) }- let(:shipping_method) { create(:shipping_method, name: 'UPS') }+ let(:inventory_units) { create_list(:inventory_unit, 2) }+ let(:variant) { line_item.variant }+ let!(:line_item) { create(:line_item) }let(:shipment) docreate(:shipment, number: 'H21265865494', cost: 1, state: 'pending', stock_location: create(:stock_location)).tap do |shipment|allow(shipment).to receive_messages order: orderallow(shipment).to receive_messages shipping_method: shipping_methodendend- let!(:line_item) { create(:line_item) }- let(:variant) { line_item.variant }- let(:inventory_units) { create_list(:inventory_unit, 2) }+ let(:shipping_method) { create(:shipping_method, name: 'UPS') }+ let!(:order) { create(:order, number: 'S12345', store: store) }+ let(:store) { @default_store }before doallow(order).to receive_messages(backordered?: false, canceled?: false, can_ship?: true, paid?: false, touch_later: false)@@ -22,6 +20,8 @@ describe Spree::Shipment, type: :model doallow(inventory_units.first).to receive_messages backordered?: trueend+ it_behaves_like 'metadata'+describe 'precision of pre_tax_amount' dobefore { line_item.update(pre_tax_amount: 4.2051) }@@ -32,6 +32,22 @@ describe Spree::Shipment, type: :model doendend+ describe '#digital?' do+ it 'returns true if shipping method has a digital calculator' do+ shipment.shipping_method.calculator = Spree::Calculator::Shipping::DigitalDelivery.new+ expect(shipment.digital?).to eq(true)+ end++ it 'returns false if shipping method does not have a digital calculator' do+ expect(shipment.digital?).to eq(false)+ end++ it 'returns false if shipping method is nil' do+ shipment.shipping_rates.delete_all+ expect(shipment.digital?).to eq(false)+ end+ end+describe '#name' doit 'returns the shipment number and shipping method name' doexpect(shipment.name).to eq('H21265865494 UPS')@@ -91,7 +107,7 @@ describe Spree::Shipment, type: :model doexpect(shipment).to be_backorderedend- context '#determine_state' do+ describe '#determine_state' dolet(:inventory_units) { create_list(:inventory_unit, 1) }before { allow(inventory_units.first).to receive_messages backordered: true }@@ -157,7 +173,7 @@ describe Spree::Shipment, type: :model doendend- context '#item_cost' do+ describe '#item_cost' dodef create_shipment(order, stock_location)order.shipments.create(stock_location_id: stock_location.id).inventory_units.create(order_id: order.id,@@ -216,7 +232,7 @@ describe Spree::Shipment, type: :model doexpect(shipment.final_price).to eq(8)end- context '#free?' do+ describe '#free?' dolet!(:order) { create(:order) }let!(:shipment) { create(:shipment, cost: 10, order: order) }let(:free_shipping_promotion) { create(:free_shipping_promotion, code: 'freeship', kind: :coupon_code) }@@ -251,7 +267,7 @@ describe Spree::Shipment, type: :model doendend- context '#store' do+ describe '#store' dolet(:store) { @default_store }let!(:order) { create(:order, store: store) }let!(:shipment) { create(:shipment, cost: 10, order: order) }@@ -261,7 +277,7 @@ describe Spree::Shipment, type: :model doendend- context '#currency' do+ describe '#currency' dolet!(:order) { create(:order, currency: 'EUR') }let!(:shipment) { create(:shipment, cost: 10, order: order) }@@ -358,7 +374,7 @@ describe Spree::Shipment, type: :model doendend- context '#update!' do+ describe '#update!' doshared_examples_for 'immutable once shipped' doit 'remains in shipped state once shipped' doshipment.state = 'shipped'@@ -391,6 +407,7 @@ describe Spree::Shipment, type: :model doexpect(shipment).to receive(:update_columns).with(state: 'ready', updated_at: kind_of(Time))shipment.update!(order)end+it_behaves_like 'immutable once shipped'it_behaves_like 'pending if backordered'end@@ -403,6 +420,7 @@ describe Spree::Shipment, type: :model doexpect(shipment).to receive(:update_columns).with(state: 'pending', updated_at: kind_of(Time))shipment.update!(order)end+it_behaves_like 'immutable once shipped'it_behaves_like 'pending if backordered'end@@ -415,6 +433,7 @@ describe Spree::Shipment, type: :model doexpect(shipment).to receive(:update_columns).with(state: 'ready', updated_at: kind_of(Time))shipment.update!(order)end+it_behaves_like 'immutable once shipped'it_behaves_like 'pending if backordered'end@@ -507,7 +526,7 @@ describe Spree::Shipment, type: :model doendend- context '#cancel' do+ describe '#cancel' dolet(:inventory_unit) { create(:inventory_unit, state: 'on_hand', line_item: line_item, variant: variant, quantity: 1) }it 'cancels the shipment' do@@ -555,7 +574,7 @@ describe Spree::Shipment, type: :model doendend- context '#resume' do+ describe '#resume' dolet(:inventory_unit) { create(:inventory_unit, quantity: 1, line_item: line_item, variant: variant) }it 'transitions state to ready if the order is ready' do@@ -587,7 +606,7 @@ describe Spree::Shipment, type: :model doendend- context '#ship' do+ describe '#ship' docontext 'when the shipment is canceled' dolet(:shipment_with_inventory_units) { create(:shipment, order: create(:order_with_line_items), state: 'canceled') }let(:subject) { shipment_with_inventory_units.ship! }@@ -635,7 +654,7 @@ describe Spree::Shipment, type: :model doendend- context '#ready' do+ describe '#ready' docontext 'with Config.auto_capture_on_dispatch == false' do# Regression test for #2040it 'cannot ready a shipment for an order if the order is unpaid' do@@ -795,7 +814,7 @@ describe Spree::Shipment, type: :model doendend- context '#tracking_url' do+ describe '#tracking_url' doit 'uses shipping method to determine url' doexpect(shipping_method).to receive(:build_tracking_url).with('1Z12345').and_return(:some_url)shipment.tracking = '1Z12345'@@ -804,7 +823,7 @@ describe Spree::Shipment, type: :model doendend- context '#transfer_to_location' do+ describe '#transfer_to_location' do# Order with 2 line items in order to be able to split one shipment into 2let(:order) { create(:completed_order_with_totals, line_items_count: 2, store: store) }let!(:stock_location) { create(:stock_location, propagate_all_variants: true, backorderable_default: true) }@@ -867,7 +886,7 @@ describe Spree::Shipment, type: :model doend# Regression test for #3349- context '#destroy' do+ describe '#destroy' doit 'destroys linked shipping_rates' doreflection = Spree::Shipment.reflect_on_association(:shipping_rates)expect(reflection.options[:dependent]).to be(:delete_all)diff --git a/storefront/app/views/themes/default/spree/shared/_order_shipment.html.erb b/storefront/app/views/themes/default/spree/shared/_order_shipment.html.erbindex 06aea0109e..f2d77c310b 100644--- a/storefront/app/views/themes/default/spree/shared/_order_shipment.html.erb+++ b/storefront/app/views/themes/default/spree/shared/_order_shipment.html.erb@@ -47,15 +47,13 @@<% end %></div></div>- <% if shipment.canceled? && shipment.shipped_at.present? %>- <%# No message if shipment has been shipped already %>- <% elsif shipment.canceled? && shipment.shipped_at.nil? %>+ <% if shipment.canceled? && shipment.shipped_at.nil? %><div class="alert-warning text-sm uppercase mt-3 px-3 py-2"><strong><%= Spree.t(:shipment) %><%= Spree.t("shipment_states.canceled") %></strong>.<%= Spree.t(:shipment_refunded_message) %></div>- <% elsif !shipment.digital? %>+ <% elsif (!shipment.tracked? || !shipment.shipped?) && !shipment.digital? %><div class="text-sm uppercase text-center bg-neutral-100 mt-3 px-3 py-2"><%= Spree.t(:no_tracking_present) %></div>