From a62a2cb52f03df21d659309f148f87f647abbd33 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 22 Nov 2018 16:49:29 +0100 Subject: [PATCH] Extract #restart_checkout to a service --- app/controllers/checkout_controller.rb | 12 ++++-------- app/services/restart_checkout.rb | 15 +++++++++++++++ spec/controllers/checkout_controller_spec.rb | 13 +++++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 app/services/restart_checkout.rb diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 8cba0d8a0e..162f3c529f 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -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? diff --git a/app/services/restart_checkout.rb b/app/services/restart_checkout.rb new file mode 100644 index 0000000000..d7dd10520b --- /dev/null +++ b/app/services/restart_checkout.rb @@ -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 diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index b373ae64b5..a202de7dec 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -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