Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index d059b46558..0e76f83dc8 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -230,7 +230,7 @@ module Spree
end
def digital?
- shipping_method.calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery)
+ shipping_method&.calculator&.is_a?(Spree::Calculator::Shipping::DigitalDelivery)
end
ManifestItem = Struct.new(:line_item, :variant, :quantity, :states)
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 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) do
create(:shipment, number: 'H21265865494', cost: 1, state: 'pending', stock_location: create(:stock_location)).tap do |shipment|
allow(shipment).to receive_messages order: order
allow(shipment).to receive_messages shipping_method: shipping_method
end
end
- 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 do
allow(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 do
allow(inventory_units.first).to receive_messages backordered?: true
end
+ it_behaves_like 'metadata'
+
describe 'precision of pre_tax_amount' do
before { line_item.update(pre_tax_amount: 4.2051) }
@@ -32,6 +32,22 @@ describe Spree::Shipment, type: :model do
end
end
+ 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' do
it 'returns the shipment number and shipping method name' do
expect(shipment.name).to eq('H21265865494 UPS')
@@ -91,7 +107,7 @@ describe Spree::Shipment, type: :model do
expect(shipment).to be_backordered
end
- context '#determine_state' do
+ describe '#determine_state' do
let(: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 do
end
end
- context '#item_cost' do
+ describe '#item_cost' do
def 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 do
expect(shipment.final_price).to eq(8)
end
- context '#free?' do
+ describe '#free?' do
let!(: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 do
end
end
- context '#store' do
+ describe '#store' do
let(: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 do
end
end
- context '#currency' do
+ describe '#currency' do
let!(:order) { create(:order, currency: 'EUR') }
let!(:shipment) { create(:shipment, cost: 10, order: order) }
@@ -358,7 +374,7 @@ describe Spree::Shipment, type: :model do
end
end
- context '#update!' do
+ describe '#update!' do
shared_examples_for 'immutable once shipped' do
it 'remains in shipped state once shipped' do
shipment.state = 'shipped'
@@ -391,6 +407,7 @@ describe Spree::Shipment, type: :model do
expect(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 do
expect(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 do
expect(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 do
end
end
- context '#cancel' do
+ describe '#cancel' do
let(: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 do
end
end
- context '#resume' do
+ describe '#resume' do
let(: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 do
end
end
- context '#ship' do
+ describe '#ship' do
context 'when the shipment is canceled' do
let(: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 do
end
end
- context '#ready' do
+ describe '#ready' do
context 'with Config.auto_capture_on_dispatch == false' do
# Regression test for #2040
it 'cannot ready a shipment for an order if the order is unpaid' do
@@ -795,7 +814,7 @@ describe Spree::Shipment, type: :model do
end
end
- context '#tracking_url' do
+ describe '#tracking_url' do
it 'uses shipping method to determine url' do
expect(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 do
end
end
- 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 2
let(: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 do
end
# Regression test for #3349
- context '#destroy' do
+ describe '#destroy' do
it 'destroys linked shipping_rates' do
reflection = 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.erb
index 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>