Increase readability (a bit) of OrderUpdater

This commit is contained in:
Pau Perez
2018-09-17 16:48:01 +02:00
parent 99cdeca0b1
commit 1fdc578901
2 changed files with 45 additions and 3 deletions

View File

@@ -8,7 +8,8 @@ module Spree
# https://github.com/spree/spree/commit/38b8456183d11fc1e00e395e7c9154c76ef65b85
# https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
def update_payment_state
last_state = order.payment_state
last_payment_state = order.payment_state
if payments.present? && payments.valid.empty?
order.payment_state = 'failed'
elsif order.state == 'canceled' && order.payment_total.zero?
@@ -26,16 +27,26 @@ module Spree
# order.payment_state = 'credit_owed' if order.outstanding_balance < 0
# order.payment_state = 'paid' if !order.outstanding_balance?
end
order.state_changed('payment') if last_state != order.payment_state
track_payment_state_change(last_payment_state)
order.payment_state
end
private
def track_payment_state_change(last_payment_state)
return unless last_payment_state != order.payment_state
order.state_changed('payment')
end
# Taken from order.outstanding_balance in Spree 2.4
# See: https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
def canceled_and_paid_for?
order.canceled? && order.payments.present? && !order.payments.completed.empty?
order.canceled? && paid?
end
def paid?
order.payments.present? && !order.payments.completed.empty?
end
end
end

View File

@@ -86,4 +86,35 @@ describe Spree::OrderUpdater do
end
end
end
context 'when the set payment_state does not match the last payment_state' do
before { order.payment_state = 'previous_to_paid' }
context 'and the order is being updated' do
before { allow(order).to receive(:persisted?) { true } }
it 'creates a new state_change for the order' do
expect { updater.update_payment_state }
.to change { order.state_changes.size }.by(1)
end
end
context 'and the order is being created' do
before { allow(order).to receive(:persisted?) { false } }
it 'creates a new state_change for the order' do
expect { updater.update_payment_state }
.not_to change { order.state_changes.size }
end
end
end
context 'when the set payment_state matches the last payment_state' do
before { order.payment_state = 'paid' }
it 'does not create any state_change' do
expect { updater.update_payment_state }
.not_to change { order.state_changes.size }
end
end
end