Skip to content

Commit

Permalink
Use service in admin/payments#create
Browse files Browse the repository at this point in the history
This separates logic for bang and non-bang versions of
Spree::Order#next.

The different conditions used in both methods (state == "completed" vs
order.completed?) have implications in whether a transition is attempted
or not.
  • Loading branch information
kristinalim committed Mar 13, 2019
1 parent 0e69181 commit 8782f20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
6 changes: 2 additions & 4 deletions app/controllers/spree/admin/payments_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def create

redirect_to admin_order_payments_path(@order)
else
#This is the first payment (admin created order)
until @order.completed?
@order.next!
end
AdvanceOrderService.new(@order).call(true)

flash[:success] = Spree.t(:new_order_completed)
redirect_to edit_admin_order_url(@order)
end
Expand Down
30 changes: 23 additions & 7 deletions app/services/advance_order_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ def initialize(order)
@order = order
end

def call
shipping_method_id = @order.shipping_method.id if @order.shipping_method.present?
def call(raise_on_error = false)
shipping_method_id = order.shipping_method.id if order.shipping_method.present?
options = { shipping_method_id: shipping_method_id }
raise_on_error ? advance_order!(options) : advance_order(options)
end

private

while @order.state != "complete"
break unless @order.next
def advance_order(options)
until order.state == "complete"
break unless order.next
after_transition_hook(options)
end
end

def advance_order!(options)
until order.completed?
order.next!
after_transition_hook(options)
end
end

if @order.state == "delivery"
@order.select_shipping_method(shipping_method_id) if shipping_method_id.present?
end
def after_transition_hook(options)
if order.state == "delivery"
order.select_shipping_method(options[:shipping_method_id]) if options[:shipping_method_id]
end
end
end
20 changes: 19 additions & 1 deletion spec/controllers/spree/admin/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@
let!(:order) { create(:order, distributor: shop, state: 'complete') }
let!(:line_item) { create(:line_item, order: order, price: 5.0) }

before do
allow(controller).to receive(:spree_current_user) { user }
end

context "#create" do
let!(:payment_method) { create(:payment_method, distributors: [ shop ]) }
let!(:order) do
create(:order_with_totals_and_distribution, distributor: shop, state: "payment")
end

let(:params) { { amount: order.total, payment_method_id: payment_method.id } }

it "advances the order state" do
expect {
spree_post :create, payment: params, order_id: order.number
}.to change { order.reload.state }.from("payment").to("complete")
end
end

context "as an enterprise user" do
before do
allow(controller).to receive(:spree_current_user) { user }
order.reload.update_totals
end

Expand Down

0 comments on commit 8782f20

Please sign in to comment.