Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/ledgerizer/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Account < ApplicationRecord
include LedgerizerLinesRelated

belongs_to :tenant, polymorphic: true
belongs_to :accountable, polymorphic: true
belongs_to :accountable, polymorphic: true, optional: true
has_many :lines, dependent: :destroy

enumerize :account_type, in: Ledgerizer::Definition::Account::TYPES,
Expand Down
2 changes: 1 addition & 1 deletion lib/ledgerizer/definition/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def entry(entry_code, document: nil, &block)
@current_entry = nil
end

def debit(account:, accountable:)
def debit(account:, accountable: nil)
in_context do
@current_tenant.add_movement(
movement_type: :debit,
Expand Down
16 changes: 13 additions & 3 deletions lib/ledgerizer/definition/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def find_movement(movement_type:, account_name:, accountable:)
end

def add_movement(movement_type:, account:, accountable:)
ar_accountable = format_to_symbol_identifier(accountable)
validate_active_record_model_name!(ar_accountable, "accountable")
validate_unique_account!(movement_type, account.name, ar_accountable)
ar_accountable = find_ar_accountable(movement_type, account.name, accountable)

Ledgerizer::Definition::Movement.new(
account: account,
Expand All @@ -41,6 +39,18 @@ def movements

private

def find_ar_accountable(movement_type, account_name, accountable)
ar_accountable = nil

if !accountable.blank?
ar_accountable = format_to_symbol_identifier(accountable)
validate_active_record_model_name!(ar_accountable, "accountable")
end

validate_unique_account!(movement_type, account_name, ar_accountable)
ar_accountable
end

def infer_model_name(value)
return format_model_to_sym(value) if value.is_a?(ActiveRecord::Base)

Expand Down
10 changes: 9 additions & 1 deletion lib/ledgerizer/definition/movement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ class Movement
def initialize(account:, accountable:, movement_type:)
@account = account
@movement_type = format_to_symbol_identifier(movement_type)
@accountable = format_to_symbol_identifier(accountable)
@accountable = format_to_symbol_identifier(accountable) if accountable
end

def accountable_class
return unless accountable

format_sym_to_model(accountable)
end

def accountable_string_class
return unless accountable

accountable_class.to_s
end
end
end
end
4 changes: 2 additions & 2 deletions lib/ledgerizer/execution/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def execute_entry(entry_code, tenant:, document:, date:, &block)
@executor = nil
end

def debit(account:, accountable:, amount:)
def debit(account:, amount:, accountable: nil)
in_context do
@executor.add_movement(
movement_type: :debit,
Expand All @@ -49,7 +49,7 @@ def debit(account:, accountable:, amount:)
end
end

def credit(account:, accountable:, amount:)
def credit(account:, amount:, accountable: nil)
in_context do
@executor.add_movement(
movement_type: :credit,
Expand Down
6 changes: 3 additions & 3 deletions lib/ledgerizer/execution/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def for_each_grouped_by_accountable_and_currency_movement(entry, &block)
groups = amounts_grouped_by_accountable_and_currency(entry, movement_definition)
groups.each do |accountabe_data, amount_cents|
accountable_id, currency = accountabe_data
accountable = movement_definition.accountable_class.find(accountable_id)
accountable = movement_definition.accountable_class&.find(accountable_id)

block.call(
accountable: accountable,
Expand All @@ -75,7 +75,7 @@ def lines_by_movement_definition(entry, movement_definition)
tenant: entry.tenant,
entry_code: code,
document: entry.document,
accountable_type: movement_definition.accountable_class.to_s,
accountable_type: movement_definition.accountable_string_class,
account_name: movement_definition.account_name
)
end
Expand All @@ -89,7 +89,7 @@ def validate_entry_document!(document)
end

def get_movement_definition!(movement_type, account_name, accountable)
validate_active_record_instance!(accountable, "accountable")
validate_active_record_instance!(accountable, "accountable") if accountable
movement_definition = entry_definition.find_movement(
movement_type: movement_type,
account_name: account_name,
Expand Down
44 changes: 44 additions & 0 deletions spec/dummy/spec/lib/execution/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
credit(account: :account2, accountable: :user)
credit(account: :account3, accountable: :user)
end

entry(:entry3, document: :user) do
debit(account: :account1, accountable: :user)
credit(account: :account2)
end
end
end

Expand Down Expand Up @@ -133,6 +138,45 @@ def perform
it { expect(Ledgerizer::Entry.last).to have_ledger_line(credit_data) }
end

context "with movement without accountable" do
let(:expected_entry) do
{
entry_code: :entry3,
entry_date: date,
document: document
}
end

let(:debit_data) do
{
account: :account1,
accountable: create(:user),
amount: clp(1)
}
end

let(:credit_data) do
{
account: :account2,
accountable: nil,
amount: clp(1)
}
end

before do
LedgerizerTestExecution.new(debit: debit_data, credit: credit_data).execute_entry3_entry(
tenant: tenant, document: document, date: date
) do
debit(data[:debit])
credit(data[:credit])
end
end

it { expect(tenant).to have_ledger_entry(expected_entry) }
it { expect(Ledgerizer::Entry.last).to have_ledger_line(debit_data) }
it { expect(Ledgerizer::Entry.last).to have_ledger_line(credit_data) }
end

context "with multiple movements" do
let(:expected_entry) do
{
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/spec/models/ledgerizer/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Ledgerizer

describe "associations" do
it { is_expected.to belong_to(:tenant) }
it { is_expected.to belong_to(:accountable) }
it { is_expected.to belong_to(:accountable).optional }
it { is_expected.to have_many(:lines).dependent(:destroy) }
end

Expand Down
24 changes: 24 additions & 0 deletions spec/dummy/spec/support/shared_examples/definition_dsl_movement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
it { expect(LedgerizerTestDefinition).to have_ledger_movement_definition(expected) }
end

context "with no accountable" do
let_definition_class do
tenant('portfolio') do
asset(:cash)

entry(:deposit, document: :portfolio) do
send(type, account: :cash)
end
end
end

let(:expected) do
{
tenant_class: :portfolio,
entry_code: :deposit,
movement_type: type,
account: :cash,
accountable: nil
}
end

it { expect(LedgerizerTestDefinition).to have_ledger_movement_definition(expected) }
end

context "with multiple movements" do
let_definition_class do
tenant('portfolio') do
Expand Down