Skip to content

Commit

Permalink
Merge pull request #3 from TommyJohnWear/add-inverse-order-payments
Browse files Browse the repository at this point in the history
Merge pull request solidusio#1406 from jordan-brough/add-order-payment-inverse-of
  • Loading branch information
gmacdougall authored Nov 16, 2016
2 parents 7d17686 + a5aa679 commit 2143ea1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build-ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(name)
@name = name
end

ALL = %w[api backend core frontend sample].map(&method(:new)).freeze
ALL = %w[api backend core].map(&method(:new)).freeze

# Install subproject
#
Expand Down
6 changes: 3 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
machine:
environment:
DB: postgresql
DB: mysql
services:
- postgresql
- mysql
ruby:
version: 2.1.5
version: 2.2.5
dependencies:
override:
- bundle check --path=vendor/bundle || bundle install --path=vendor/bundle
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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
5 changes: 3 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 All @@ -14,6 +14,7 @@

before do
payment.pend!
order.save!

allow_any_instance_of(Spree::Order).to receive(:thingamajig) do |order|
changes_spy.change_callback_occured if order.changes.any?
Expand Down

0 comments on commit 2143ea1

Please sign in to comment.