Skip to content

Commit

Permalink
Deprecate Spree::Payment#offsets
Browse files Browse the repository at this point in the history
The old system for refunds was removed in 2014 [1].

We deprecate the `.offsets` association, but we still need to silence
the warning on the internal method `.credit_allowed` method until the
full removal.

[1] - spree/spree#4883
  • Loading branch information
waiting-for-dev committed Apr 11, 2023
1 parent 813a9a1 commit 66b3652
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Payment < Spree::Base
belongs_to :payment_method, -> { with_discarded }, class_name: 'Spree::PaymentMethod', inverse_of: :payments, optional: true

has_many :offsets, -> { offset_payment }, class_name: "Spree::Payment", foreign_key: :source_id
# TODO: When removing the method we can also drop the `source_id` column
def offsets
Spree::Deprecation.warn <<~MSG
`Spree::Payment#offsets` are deprecated. Use the refund system (`Spree::Refund`) instead.
MSG

super
end
has_many :log_entries, as: :source
has_many :state_changes, as: :stateful
has_many :capture_events, class_name: 'Spree::PaymentCaptureEvent'
Expand Down Expand Up @@ -101,7 +109,10 @@ def offsets_total
# @return [BigDecimal] the amount of this payment minus the offsets
# (old-style refunds) and refunds
def credit_allowed
amount - (offsets_total.abs + refunds.sum(:amount))
amount - (
(Spree::Deprecation.silence { offsets_total.abs }) +
refunds.sum(:amount)
)
end

# @return [Boolean] true when this payment can be credited
Expand Down
14 changes: 14 additions & 0 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1332,4 +1332,18 @@
expect(described_class.valid).to be_empty
end
end

describe '::offsets' do
it 'is deprecated' do
expect(Spree::Deprecation).to receive(:warn).with(/offsets` are deprecated/)

described_class.new.offsets
end

it 'still calls the original method' do
Spree::Deprecation.silence do
expect(described_class.new.offsets.class.name).to eq('ActiveRecord::Associations::CollectionProxy')
end
end
end
end

0 comments on commit 66b3652

Please sign in to comment.