Module: LedgerAccountable
- Defined in:
- lib/ledger_accountable/ledger_item.rb,
lib/ledger_accountable.rb,
lib/ledger_accountable/version.rb,
lib/ledger_accountable/ledger_owner.rb,
lib/ledger_accountable/ledger_item/entry_creator.rb,
lib/ledger_accountable/ledger_item/state_transition.rb
Overview
LedgerAccountable::LedgerItem adds ledger functionality to any model that acts as an item in a ledger.
It supports tracking two types of ledger entries:
-
Debits: outgoing items which decrease the ledger balance (for example, an item being sold)
-
Credits: incoming items which increase the ledger balance (for example, a payment taken)
Usage: Include LedgerAccountable in a model to have it generate ledger entries based on its addition/deletion/update events relative to its ledger owner.
Specify ‘ledger_owner`, the owner of the ledger entries, and optionally `ledger_attributes` to track specific attributes changes.
Example: “‘ruby class Order < ApplicationRecord
has_many :ledger_entries, as: :owner
has_many :order_items, dependent: :destroy
has_many :payments, dependent: :destroy
end
class OrderItem < ApplicationRecord
include LedgerAccountable::LedgerItem
belongs_to :order
# Track ledger changes on order with the cost method and provide a net_amount
# to dynamically compute net changes to its cost
track_ledger :order,
amount: :cost,
net_amount: :net_cost_change,
type: :debit
def cost
quantity * unit_price
end
private
def net_cost_change
cost - (quantity_was * unit_price_was)
end
end
class Payment < ApplicationRecord
include LedgerAccountable::LedgerItem
belongs_to :order
# Track ledger changes on order with the amount attribute and mark it as a credit
track_ledger :order, amount: :amount, type: :credit
end “‘
Note:
- The `ledger_owner` association must exist in the LedgerAccountable::LedgerItem model.
Defined Under Namespace
Modules: LedgerItem, LedgerOwner
Constant Summary collapse
- VERSION =
'0.1.10.pre'
Class Method Summary collapse
-
.setup {|_self| ... } ⇒ Object
Default way to configure LedgerAccountable.
Class Method Details
.setup {|_self| ... } ⇒ Object
Default way to configure LedgerAccountable. Run rails generate ledger_accountable to create or update an initializer with default configuration values.
7 8 9 |
# File 'lib/ledger_accountable.rb', line 7 def self.setup yield self end |