Class: WhittakerTech::Midas::Ledger::Posting

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Bankable
Defined in:
app/models/whittaker_tech/midas/ledger/posting.rb

Overview

Ledger::Posting is a single debit or credit line within a balanced Ledger::Entry. Its amount is stored twice, deliberately: via the standard Bankable/Coin mechanism (amount, amount_format, amount_in, etc. — for presentation/conversion parity with the rest of the engine) and denormalized onto currency_minor directly on this table, which is what every balance/aggregation query actually reads. This exists so Phase 2 partitioning of this table (by occurred_at) pays off — if the amount only lived on the joined Coin row, partitioning postings wouldn't help any query that also needs the amount.

Postings are immutable once created, and may only be added to or removed from an Entry before that Entry is finalized — see Ledger::Entry.record! and Ledger::Entry#finalized?.

Since:

  • 0.4.0

Instance Method Summary collapse

Instance Method Details

#attach_amount_coinObject

Bankable's has_coin defines set_amount directly on this class (via define_method called with self as the includer), not through a separate module in the ancestor chain — so a def set_amount; super; end below would have no ancestor to reach. Alias the original before overriding so it can still be called.

Since:

  • 0.4.0



32
# File 'app/models/whittaker_tech/midas/ledger/posting.rb', line 32

alias attach_amount_coin set_amount

#set_amount(amount:, currency_code:) ⇒ WhittakerTech::Midas::Coin

Overrides Bankable's generated set_amount to keep the denormalized currency_minor column in sync with the Coin it just attached. Business validation of the resulting amount (positive, currency matches entry) happens in Entry#finalize!, not here — see class comment.

Parameters:

  • amount (Money, Integer, Numeric)
  • currency_code (String)

Returns:

Since:

  • 0.4.0



58
59
60
61
62
63
64
# File 'app/models/whittaker_tech/midas/ledger/posting.rb', line 58

def set_amount(amount:, currency_code:)
  ensure_entry_not_finalized!

  coin = attach_amount_coin(amount:, currency_code:)
  update_column(:currency_minor, coin.currency_minor) # rubocop:disable Rails/SkipsModelValidations
  coin
end