From 16d75d0acb1807657eca8e3bf2a3af367bc42afc Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 29 Jul 2024 15:42:20 +0200 Subject: [PATCH 1/2] Format payment source --- core/app/models/spree/payment_source.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/payment_source.rb b/core/app/models/spree/payment_source.rb index 14133f6f7d7..1251f43a67a 100644 --- a/core/app/models/spree/payment_source.rb +++ b/core/app/models/spree/payment_source.rb @@ -7,7 +7,10 @@ class PaymentSource < Spree::Base belongs_to :payment_method, optional: true has_many :payments, as: :source - has_many :wallet_payment_sources, class_name: 'Spree::WalletPaymentSource', as: :payment_source, inverse_of: :payment_source + has_many :wallet_payment_sources, + class_name: 'Spree::WalletPaymentSource', + as: :payment_source, + inverse_of: :payment_source attr_accessor :imported From 4ca261c152a67190d71018ca6c17a0c57166b9dd Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 27 Aug 2024 14:05:45 +0200 Subject: [PATCH 2/2] Destroy wallet payment source when source gets deleted A wallet payment source without an actual payment source does not make any sense. --- core/app/models/spree/payment_source.rb | 3 ++- core/spec/support/concerns/payment_source.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/payment_source.rb b/core/app/models/spree/payment_source.rb index 1251f43a67a..0832fc5de76 100644 --- a/core/app/models/spree/payment_source.rb +++ b/core/app/models/spree/payment_source.rb @@ -10,7 +10,8 @@ class PaymentSource < Spree::Base has_many :wallet_payment_sources, class_name: 'Spree::WalletPaymentSource', as: :payment_source, - inverse_of: :payment_source + inverse_of: :payment_source, + dependent: :destroy attr_accessor :imported diff --git a/core/spec/support/concerns/payment_source.rb b/core/spec/support/concerns/payment_source.rb index 10ef52b9cc2..c64c3ea93e8 100644 --- a/core/spec/support/concerns/payment_source.rb +++ b/core/spec/support/concerns/payment_source.rb @@ -13,6 +13,24 @@ it { is_expected.to respond_to(:user) } end + describe "#wallet_payment_sources" do + let(:user) { create(:user) } + + let!(:wallet_payment_source) do + # There are nasty validations that do not matter for this test + payment_source.save(validate: false) + Spree::WalletPaymentSource.new(user: user, payment_source: payment_source).tap do |wps| + wps.save(validate: false) + end + end + + context "when the payment source gets destroyed" do + it "destroys the wallet payment source" do + expect { payment_source.destroy }.to change { Spree::WalletPaymentSource.count }.by(-1) + end + end + end + describe "#can_capture?" do it "should be true if payment is pending" do payment = mock_model(Spree::Payment, pending?: true, created_at: Time.current)