The personal finance app for everyone (by everyone)
Ruby
fix : account destroyed cascade transfer destruction then … (#1795)
arumaioeca8c6ce1f23
diff --git a/app/models/account.rb b/app/models/account.rbindex b0595d308a..6e11de9c49 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -2,7 +2,10 @@ class Account < ApplicationRecordinclude AASM, Syncable, Monetizable, Chartable, Linkable, Enrichable, Anchorable, Reconcileable, TaxTreatablebefore_validation :assign_default_owner, if: -> { owner_id.blank? }+before_destroy :capture_account_statement_ids_to_move+ before_destroy :cleanup_transfers+after_destroy_commit :move_account_statements_to_inboxvalidates :name, :balance, :currency, presence: true@@ -543,4 +546,12 @@ class Account < ApplicationRecordupdated_at: Time.current)end++ def cleanup_transfers+ transaction_ids = entries.where(entryable_type: "Transaction").pluck(:entryable_id)++ transfers = Transfer.where(inflow_transaction_id: transaction_ids).or(Transfer.where(outflow_transaction_id: transaction_ids))++ transfers.find_each(&:destroy!)+ endenddiff --git a/app/models/transfer.rb b/app/models/transfer.rbindex 878e899be7..9b39d0f7d9 100644--- a/app/models/transfer.rb+++ b/app/models/transfer.rb@@ -38,8 +38,16 @@ class Transfer < ApplicationRecord# Once transfer is destroyed, we need to mark the denormalized kind fields on the transactionsdef destroy!Transfer.transaction do- inflow_transaction.update!(kind: "standard")- outflow_transaction.update!(kind: "standard")+ [ inflow_transaction, outflow_transaction ].each do |transaction|+ next if transaction.nil?+ next unless Transaction.exists?(transaction.id)+ begin+ transaction.update!(kind: "standard")+ rescue ActiveRecord::RecordNotFound+ rescue NoMethodError+ next+ end+ endsuperendenddiff --git a/test/models/account_test.rb b/test/models/account_test.rbindex 6b1507159f..de44d90cbe 100644--- a/test/models/account_test.rb+++ b/test/models/account_test.rb@@ -382,4 +382,41 @@ class AccountTest < ActiveSupport::TestCaseassert_equal [ provider_holding.id, second_provider_holding.id ].sort, account.current_holdings.pluck(:id).sortassert_equal %w[CHF EUR], account.current_holdings.pluck(:currency).sortend++ test "on account destroyed cascade transfer destroyed" do+ outflow_account = @family.accounts.create!({+ owner: @admin,+ name: "test_account_outflow",+ balance: 100,+ currency: "USD",+ accountable_type: "Depository",+ accountable_attributes: {}+ })+ inflow_account = @family.accounts.create!({+ owner: @admin,+ name: "test_account_inflow",+ balance: 100,+ currency: "USD",+ accountable_type: "Depository",+ accountable_attributes: {}+ })++ transfer = create_transfer(+ from_account: outflow_account,+ to_account: inflow_account,+ amount: 50+ )++ outflow_transaction = transfer.outflow_transaction++ outflow_transaction.reload+ assert_equal "funds_movement", outflow_transaction.kind++ inflow_account.destroy!++ assert_raises(ActiveRecord::RecordNotFound) { transfer.reload }++ outflow_transaction.reload+ assert_equal "standard", outflow_transaction.kind+ endend