Replace helper with service object

This commit is contained in:
Pau Perez
2017-07-18 13:50:38 +02:00
committed by Rob Harrington
parent e087a008c2
commit 85fefcd946
3 changed files with 42 additions and 10 deletions

View File

@@ -1,6 +1,4 @@
Spree::PaypalController.class_eval do
include CheckoutHelper
after_filter :reset_order_when_complete, only: :confirm
before_filter :enable_embedded_shopfront
@@ -9,13 +7,19 @@ Spree::PaypalController.class_eval do
redirect_to main_app.checkout_path
end
def expire_current_order
session[:order_id] = nil
@current_order = nil
end
private
def reset_order_when_complete
if current_order.complete?
flash[:success] = t(:order_processed_successfully)
reset_order
ResetOrderService.new(self, current_order).call
session[:access_token] = current_order.token
end
end
end

View File

@@ -104,10 +104,6 @@ module CheckoutHelper
render "shared/validated_select", name: name, path: path, options: options, attributes: attributes
end
def reset_order
ResetOrderService.new(self, current_order).call
end
def payment_method_price(method, order)
price = method.compute_amount(order)
if price == 0

View File

@@ -2,9 +2,41 @@ require 'spec_helper'
module Spree
describe PaypalController do
it "should redirect back to checkout after cancel" do
spree_get :cancel
response.should redirect_to checkout_path
context 'when cancelling' do
it 'redirects back to checkout' do
expect(spree_get(:cancel)).to redirect_to checkout_path
end
end
context 'when confirming' do
let(:previous_order) { controller.current_order(true) }
let(:payment_method) { create(:payment_method) }
before do
allow(previous_order).to receive(:complete?).and_return(true)
end
it 'resets the order' do
spree_post :confirm, payment_method_id: payment_method.id
expect(controller.current_order).not_to eq(previous_order)
end
it 'sets the access token of the session' do
spree_post :confirm, payment_method_id: payment_method.id
expect(session[:access_token]).to eq(controller.current_order.token)
end
end
describe '#expire_current_order' do
it 'empties the order_id of the session' do
expect(session).to receive(:[]=).with(:order_id, nil)
controller.expire_current_order
end
it 'resets the @current_order ivar' do
controller.expire_current_order
expect(controller.instance_variable_get(:@current_order)).to be_nil
end
end
end
end