Skip to content

Commit

Permalink
Merge pull request solidusio#1406 from jordan-brough/add-order-paymen…
Browse files Browse the repository at this point in the history
…t-inverse-of

Add `inverse_of` to order.payments
  • Loading branch information
jordan-brough authored and bbuchalter committed May 26, 2017
1 parent e56a410 commit 3bd125d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CannotRebuildShipments < StandardError; end
belongs_to :store, class_name: 'Spree::Store'
has_many :state_changes, as: :stateful
has_many :line_items, -> { order(:created_at, :id) }, dependent: :destroy, inverse_of: :order
has_many :payments, dependent: :destroy
has_many :payments, dependent: :destroy, inverse_of: :order
has_many :return_authorizations, dependent: :destroy, inverse_of: :order
has_many :reimbursements, inverse_of: :order
has_many :adjustments, -> { order(:created_at) }, as: :adjustable, inverse_of: :adjustable, dependent: :destroy
Expand Down
23 changes: 8 additions & 15 deletions core/app/models/spree/order_capturing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,15 @@ def capture_payments
Spree::OrderMutex.with_lock!(@order) do
uncaptured_amount = @order.display_total.cents

begin
sorted_payments(@order).each do |payment|
amount = [uncaptured_amount, payment.money.cents].min

if amount > 0
payment.capture!(amount)
uncaptured_amount -= amount
elsif Spree::OrderCapturing.void_unused_payments
payment.void_transaction!
end
sorted_payments(@order).each do |payment|
amount = [uncaptured_amount, payment.money.cents].min

if amount > 0
payment.capture!(amount)
uncaptured_amount -= amount
elsif Spree::OrderCapturing.void_unused_payments
payment.void_transaction!
end
ensure
# FIXME: Adding the inverse_of on the payments relation for orders -should- fix this,
# however it only appears to make it worse (calling with changes three times instead of once.
# Warrants an investigation. Reloading for now.
@order.reload.update!
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/order_capturing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
describe '#capture_payments' do
subject { Spree::OrderCapturing.new(order, payment_methods).capture_payments }

# Regression for the order.update! in the ensure block.
# See the comment there.
# Regression for https://github.com/solidusio/solidus/pull/407
# See also https://github.com/solidusio/solidus/pull/1406
context "updating the order" do
let(:order) { create :completed_order_with_totals }
let(:payment_methods) { [] }
Expand Down

0 comments on commit 3bd125d

Please sign in to comment.