Skip to content

Commit

Permalink
Merge 4f22bd6 into 60efef8
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Cake committed Sep 17, 2015
2 parents 60efef8 + 4f22bd6 commit 951b2ca
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 19 deletions.
27 changes: 13 additions & 14 deletions backend/app/controllers/comable/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class OrdersController < Comable::Admin::ApplicationController

load_and_authorize_resource class: Comable::Order.name, except: :index

rescue_from ActiveRecord::RecordInvalid, with: :redirect_to_back_with_alert
rescue_from StateMachine::InvalidTransition, with: :redirect_to_back_with_alert

def index
@q = Comable::Order.complete.ransack(params[:q])
@orders = @q.result.page(params[:page]).per(15).recent.accessible_by(current_ability)
Expand Down Expand Up @@ -38,50 +41,36 @@ def export
def cancel
@order.cancel!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def resume
@order.resume!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def cancel_payment
@order.payment.cancel!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def resume_payment
@order.payment.resume!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def ship
@order.shipment.ship!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def cancel_shipment
@order.shipment.cancel!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

def resume_shipment
@order.shipment.resume!
redirect_to :back, notice: Comable.t('successful')
rescue ActiveRecord::RecordInvalid => e
redirect_to :back, alert: e.message
end

private
Expand All @@ -97,6 +86,16 @@ def order_params
order_items_attributes: [:id, :name, :code, :price, :quantity]
)
end

def redirect_to_back_with_alert(exception)
case exception
when StateMachine::InvalidTransition
flash[:alert] = [exception.object.model_name.human, exception.machine.errors_for(exception.object)].join(' ')
else
flash[:alert] = exception.message
end
redirect_to :back
end
end
end
end
26 changes: 25 additions & 1 deletion backend/spec/controllers/comable/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@
post :cancel, id: order.to_param
expect(response).to redirect_to(:back)
end

context 'with payment error' do
before do
order.payment = build(:payment)
allow(order.payment).to receive(:provider_cancel!).and_raise(StandardError)
end

it 'keep the state of requested order' do
post :cancel, id: order.to_param
order.reload
expect(order).not_to be_canceled
end

it 'redirects back' do
post :cancel, id: order.to_param
expect(response).to redirect_to(:back)
end

it 'assigns the message as flash[:alert]' do
post :cancel, id: order.to_param
order.reload
expect(flash[:alert]).to include(Comable::Payment.model_name.human)
end
end
end

describe 'POST resume' do
Expand Down Expand Up @@ -142,7 +166,7 @@
stock.update_attributes(quantity: 0)
end

it 'keep the requested order cancel' do
it 'keep the state of requested order' do
post :resume, id: order.to_param
order.reload
expect(order).to be_canceled
Expand Down
1 change: 1 addition & 0 deletions core/app/models/comable/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Order < ActiveRecord::Base
delegate :full_name, to: :ship_address, allow_nil: true, prefix: :ship
delegate :state, :human_state_name, to: :payment, allow_nil: true, prefix: true
delegate :state, :human_state_name, to: :shipment, allow_nil: true, prefix: true
delegate :cancel!, :resume!, to: :payment, allow_nil: true, prefix: true

def complete!
ActiveRecord::Base.transaction do
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/comable/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Payment < ActiveRecord::Base
before_transition to: :ready, do: -> (s) { s.provider_authorize! }
before_transition to: :completed, do: -> (s) { s.complete! }
before_transition to: :canceled, do: -> (s) { s.provider_cancel! }
before_transition to: :canceled, do: -> (s) { s.provider_resume! }
before_transition to: :resumed, do: -> (s) { s.provider_resume! }
end

class << self
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/concerns/comable/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ module Checkout
end

before_transition to: :completed, do: :complete!
after_transition to: :canceled, do: :restock!
after_transition to: :resumed, do: :unstock!
after_transition to: :canceled, do: [:restock!, :payment_cancel!]
after_transition to: :resumed, do: [:unstock!, :payment_resume!]
end
end

Expand Down
25 changes: 25 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ en:
completed: 'Completed'
canceled: 'Canceled'
resumed: 'Resumed'
events:
cancel: 'Cancel'
resume: 'Resume'
comable/shipment:
state:
states:
Expand All @@ -277,6 +280,9 @@ en:
completed: 'Completed'
canceled: 'Canceled'
resumed: 'Resumed'
events:
cancel: 'Cancel'
resume: 'Resume'

models:
comable/order: 'Orders'
Expand Down Expand Up @@ -462,6 +468,25 @@ en:
name: 'Name'
<<: *timestamps

errors:
state: &errors_state
invalid: 'is unknown'
invalid_event: 'cannot run the transition when %{state}'
invalid_transition: 'cannot run the transition via %{event}'
models:
comable/order:
attributes:
state:
<<: *errors_state
comable/shipment:
attributes:
state:
<<: *errors_state
comable/payment:
attributes:
state:
<<: *errors_state

enumerize:
comable/user:
role:
Expand Down
25 changes: 25 additions & 0 deletions core/config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ ja:
completed: '決済完了'
canceled: 'キャンセル'
resumed: '再開'
events:
cancel: 'キャンセル'
resume: '再開'
comable/shipment:
state:
states:
Expand All @@ -277,6 +280,9 @@ ja:
completed: '発送完了'
canceled: 'キャンセル'
resumed: '再開'
events:
cancel: 'キャンセル'
resume: '再開'

models:
comable/order: ご注文
Expand Down Expand Up @@ -462,6 +468,25 @@ ja:
name: '名称'
<<: *timestamps

errors:
state: &errors_state
invalid: 'エラー'
invalid_event: ' "%{state}" への状態遷移に失敗しました'
invalid_transition: 'の "%{event}" に失敗しました'
models:
comable/order:
attributes:
state:
<<: *errors_state
comable/shipment:
attributes:
state:
<<: *errors_state
comable/payment:
attributes:
state:
<<: *errors_state

enumerize:
comable/user:
role:
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/comable/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create

flash.now[:notice] = Comable.t('orders.success')
send_order_complete_mail
rescue ActiveRecord::RecordInvalid
rescue ActiveRecord::RecordInvalid, StateMachine::InvalidTransition
flash[:alert] = @order.errors.full_messages.join
redirect_to next_order_path
end
Expand Down

0 comments on commit 951b2ca

Please sign in to comment.