Extract #restart_checkout to a service

This commit is contained in:
Pau Perez
2018-11-22 16:49:29 +01:00
parent ac9cfaa8c3
commit a62a2cb52f
3 changed files with 30 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ class CheckoutController < Spree::CheckoutController
# This is only required because of spree_paypal_express. If we implement
# a version of paypal that uses this controller, and more specifically
# the #update_failed method, then we can remove this call
restart_checkout
RestartCheckout.new(@order).restart_checkout
end
def update
@@ -139,7 +139,8 @@ class CheckoutController < Spree::CheckoutController
def update_failed
clear_ship_address
restart_checkout
RestartCheckout.new(@order).restart_checkout
respond_to do |format|
format.html do
render :edit
@@ -159,12 +160,7 @@ class CheckoutController < Spree::CheckoutController
end
def restart_checkout
return if @order.state == 'cart'
@order.restart_checkout! # resets state to 'cart'
@order.update_attributes!(shipping_method_id: nil)
@order.shipments.with_state(:pending).destroy_all
@order.payments.with_state(:checkout).destroy_all
@order.reload
RestartCheckout.new(@order).restart_checkout
end
def skip_state_validation?

View File

@@ -0,0 +1,15 @@
# Resets the passed order to cart state while clearing associated payments and shipments
class RestartCheckout
def initialize(order)
@order = order
end
def restart_checkout
return if @order.state == 'cart'
@order.restart_checkout! # resets state to 'cart'
@order.update_attributes!(shipping_method_id: nil)
@order.shipments.with_state(:pending).destroy_all
@order.payments.with_state(:checkout).destroy_all
@order.reload
end
end

View File

@@ -189,11 +189,14 @@ describe CheckoutController, type: :controller do
describe "Paypal routing" do
let(:payment_method) { create(:payment_method, type: "Spree::Gateway::PayPalExpress") }
let(:restart_checkout) { instance_double(RestartCheckout, restart_checkout: true) }
before do
allow(controller).to receive(:current_distributor) { distributor }
allow(controller).to receive(:current_order_cycle) { order_cycle }
allow(controller).to receive(:current_order) { order }
allow(controller).to receive(:restart_checkout)
allow(RestartCheckout).to receive(:new) { restart_checkout }
end
it "should check the payment method for Paypalness if we've selected one" do
@@ -205,14 +208,20 @@ describe CheckoutController, type: :controller do
end
describe "#update_failed" do
let(:restart_checkout) do
instance_double(RestartCheckout, restart_checkout: true)
end
before do
controller.instance_variable_set(:@order, order)
allow(RestartCheckout).to receive(:new) { restart_checkout }
end
it "clears the shipping address and restarts the checkout" do
expect(controller).to receive(:clear_ship_address)
expect(controller).to receive(:restart_checkout)
expect(restart_checkout).to receive(:restart_checkout)
expect(controller).to receive(:respond_to)
controller.send(:update_failed)
end
end