Skip to content

Commit 72f9fcd

Browse files
authored
Merge pull request solidusio#6509 from SuperGoodSoft/introduce-extensible-stock-coordinator
Delegate create_proposed_shipments to Spree::OrderShipping
2 parents 88453be + 5a57d65 commit 72f9fcd

4 files changed

Lines changed: 69 additions & 54 deletions

File tree

core/app/models/spree/order.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ def all_inventory_units_returned?
308308
inventory_units.reload.all?(&:returned?)
309309
end
310310

311+
# Since this method is called from within the order state machine, we cannot
312+
# simply delegate this to :shipping. The state_machines gem uses the arity
313+
# of the method to determine if it should pass in the transition as an
314+
# argument. The arity of the method is masked when using delegates.
315+
def create_proposed_shipments
316+
shipping.create_proposed_shipments
317+
end
318+
311319
def contents
312320
@contents ||= Spree::Config.order_contents_class.new(self)
313321
end
@@ -502,17 +510,6 @@ def billing_address_required?
502510
Spree::Config.billing_address_required
503511
end
504512

505-
def create_proposed_shipments
506-
if completed?
507-
raise CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_order_completed"))
508-
elsif shipments.any? { |shipment| !shipment.pending? }
509-
raise CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_shipments_not_pending"))
510-
else
511-
shipments.destroy_all
512-
shipments.push(*Spree::Config.stock.coordinator_class.new(self).shipments)
513-
end
514-
end
515-
516513
def create_shipments_for_line_item(line_item)
517514
inventory_units = Spree::Config.stock.inventory_unit_builder_class.new(self).missing_units_for_line_item(line_item)
518515

core/app/models/spree/order_shipping.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ def initialize(order)
88
@order = order
99
end
1010

11+
# A method that destroys the current shipments on an order, creates new
12+
# shipments for the line items, and assigns them to the order.
13+
#
14+
# @return [Array<Spree::Shipment>] The created shipments
15+
# @raise [Spree::Order::CannotRebuildShipments] raised if order is completed
16+
# or there are any non-pending shipments.
17+
def create_proposed_shipments
18+
if @order.completed?
19+
raise Spree::Order::CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_order_completed"))
20+
elsif @order.shipments.any? { |shipment| !shipment.pending? }
21+
raise Spree::Order::CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_shipments_not_pending"))
22+
else
23+
@order.shipments.destroy_all
24+
@order.shipments.push(*Spree::Config.stock.coordinator_class.new(@order).shipments)
25+
end
26+
end
27+
1128
# A shortcut method that ships *all* inventory units in a shipment in a single
1229
# carton. See also {#ship}.
1330
#

core/spec/models/spree/order_shipping_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,50 @@ def emails
5353
end
5454
end
5555

56+
describe "#create_proposed_shipments" do
57+
subject(:order) { create(:order) }
58+
59+
it "assigns the coordinator returned shipments to its shipments" do
60+
shipment = build(:shipment)
61+
allow_any_instance_of(Spree::Stock::SimpleCoordinator).to receive(:shipments).and_return([shipment])
62+
order.shipping.create_proposed_shipments
63+
expect(order.shipments).to eq [shipment]
64+
end
65+
66+
it "raises an error if any shipments are ready" do
67+
shipment = create(:shipment, order:, state: "ready")
68+
69+
expect {
70+
expect {
71+
order.shipping.create_proposed_shipments
72+
}.to raise_error(Spree::Order::CannotRebuildShipments)
73+
}.not_to change { order.reload.shipments.pluck(:id) }
74+
75+
expect { shipment.reload }.not_to raise_error
76+
end
77+
78+
it "raises an error if any shipments are shipped" do
79+
shipment = create(:shipment, order:, state: "shipped")
80+
expect {
81+
expect {
82+
order.shipping.create_proposed_shipments
83+
}.to raise_error(Spree::Order::CannotRebuildShipments)
84+
}.not_to change { order.reload.shipments.pluck(:id) }
85+
86+
expect { shipment.reload }.not_to raise_error
87+
end
88+
89+
context "when the order is already completed" do
90+
let(:order) { create(:completed_order_with_pending_payment) }
91+
92+
it "raises an error" do
93+
expect {
94+
order.shipping.create_proposed_shipments
95+
}.to raise_error(Spree::Order::CannotRebuildShipments)
96+
end
97+
end
98+
end
99+
56100
describe "#ship" do
57101
subject do
58102
order.shipping.ship(

core/spec/models/spree/order_spec.rb

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,49 +1265,6 @@ def call
12651265
end
12661266
end
12671267

1268-
describe "#create_proposed_shipments" do
1269-
subject(:order) { create(:order) }
1270-
it "assigns the coordinator returned shipments to its shipments" do
1271-
shipment = build(:shipment)
1272-
allow_any_instance_of(Spree::Stock::SimpleCoordinator).to receive(:shipments).and_return([shipment])
1273-
subject.create_proposed_shipments
1274-
expect(subject.shipments).to eq [shipment]
1275-
end
1276-
1277-
it "raises an error if any shipments are ready" do
1278-
shipment = create(:shipment, order: subject, state: "ready")
1279-
1280-
expect {
1281-
expect {
1282-
subject.create_proposed_shipments
1283-
}.to raise_error(Spree::Order::CannotRebuildShipments)
1284-
}.not_to change { subject.reload.shipments.pluck(:id) }
1285-
1286-
expect { shipment.reload }.not_to raise_error
1287-
end
1288-
1289-
it "raises an error if any shipments are shipped" do
1290-
shipment = create(:shipment, order: subject, state: "shipped")
1291-
expect {
1292-
expect {
1293-
subject.create_proposed_shipments
1294-
}.to raise_error(Spree::Order::CannotRebuildShipments)
1295-
}.not_to change { subject.reload.shipments.pluck(:id) }
1296-
1297-
expect { shipment.reload }.not_to raise_error
1298-
end
1299-
1300-
context "when the order is already completed" do
1301-
let(:order) { create(:completed_order_with_pending_payment) }
1302-
1303-
it "raises an error" do
1304-
expect {
1305-
order.create_proposed_shipments
1306-
}.to raise_error(Spree::Order::CannotRebuildShipments)
1307-
end
1308-
end
1309-
end
1310-
13111268
describe "#all_inventory_units_returned?" do
13121269
let(:order) { create(:order_with_line_items, line_items_count: 3) }
13131270

0 commit comments

Comments
 (0)