Skip to content

Is balance_for_locked_account necessary? #223

@ghiculescu

Description

@ghiculescu

Thanks for sharing this great gem. I have been diving deep into the code and trying to understand its internals. One question I can't work out is is if balance_for_locked_account is necessary.

diff --git a/lib/double_entry/transfer.rb b/lib/double_entry/transfer.rb
index a846f90..4eb1de4 100644
--- a/lib/double_entry/transfer.rb
+++ b/lib/double_entry/transfer.rb
@@ -98,8 +98,8 @@ module DoubleEntry
     def create_lines(amount, code, detail, from_account, to_account, metadata)
       credit, debit = Line.new, Line.new

-      credit_balance = Locking.balance_for_locked_account(from_account)
-      debit_balance  = Locking.balance_for_locked_account(to_account)
+      credit_balance = AccountBalance.find_by_account(from_account, lock: true)
+      debit_balance  = AccountBalance.find_by_account(to_account, lock: true)

       credit_balance.update_attribute :balance, credit_balance.balance - amount
       debit_balance.update_attribute :balance, debit_balance.balance + amount

With this change, on Ruby 3.3, all tests and the jackhammer pass for me, using Postgres. (I ran the jackhammer with threads, I couldn't get it to run with processes, maybe the issue is there.)

It's obviously not a critical part of the code but it does add a little bit of extra indirection, and as far as I can tell it's not necessary, as long as Locking.lock_accounts wraps around this code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions