Skip to content

Commit

Permalink
Refactoring to dry up account subclasses, as discussed in PR mbulat#41
Browse files Browse the repository at this point in the history
  - Refactors ::balance and #balance methods defined in each of Account's subclasses
    to the Account model.
  - Retain original documentation
  - Adds documentation for the methods in their new locations (with a note on intended use)
  - Prevents calling ::balance or #balance on the Account class/instance directly
  - Adds specs for preventing calling ::balance and #balance on the Account class/instance directly
  • Loading branch information
Tad Hosford committed Jun 4, 2015
1 parent 89d0c9f commit 44d8c8a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 76 deletions.
55 changes: 55 additions & 0 deletions app/models/plutus/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module Plutus
#
# @author Michael Bulat
class Account < ActiveRecord::Base
class_attribute :normal_credit_balance

has_many :credit_amounts, :extend => AmountsExtension, :class_name => 'Plutus::CreditAmount'
has_many :debit_amounts, :extend => AmountsExtension, :class_name => 'Plutus::DebitAmount'
has_many :credit_entries, :through => :credit_amounts, :source => :entry, :class_name => 'Plutus::Entry'
Expand All @@ -43,6 +45,32 @@ class Account < ActiveRecord::Base
include Plutus::NoTenancy
end

# The balance of the account. This instance method is intended for use only
# on instances of account subclasses.
#
# If the account has a normal credit balance, the debits are subtracted from the credits
# unless this is a contra account, in which case credits are substracted from debits.
#
# For a normal debit balance, the credits are subtracted from the debits
# unless this is a contra account, in which case debits are subtracted from credits.
#
# @example
# >> liability.balance
# => #<BigDecimal:103259bb8,'0.2E4',4(12)>
#
# @return [BigDecimal] The decimal value balance
def balance
unless self.class == Plutus::Account
if self.normal_credit_balance ^ contra
credits_balance - debits_balance
else
debits_balance - credits_balance
end
else
raise(NoMethodError, "undefined method 'balance'")
end
end

# The credit balance for the account.
#
# @example
Expand All @@ -65,6 +93,33 @@ def debits_balance
debit_amounts.balance
end

# This class method is used to return the balance of all accounts
# for a given class and is intended for use only on account subclasses.
#
# Contra accounts are automatically subtracted from the balance.
#
# @example
# >> Plutus::Liability.balance
# => #<BigDecimal:1030fcc98,'0.82875E5',8(20)>
#
# @return [BigDecimal] The decimal value balance
def self.balance
unless self.new.class == Plutus::Account
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |account|
unless account.contra
accounts_balance += account.balance
else
accounts_balance -= account.balance
end
end
accounts_balance
else
raise(NoMethodError, "undefined method 'balance'")
end
end

# The trial balance of all accounts in the system. This should always equal zero,
# otherwise there is an error in the system.
#
Expand Down
19 changes: 4 additions & 15 deletions app/models/plutus/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Plutus
# @author Michael Bulat
class Asset < Account

self.normal_credit_balance = false

# The balance of the account.
#
# Assets have normal debit balances, so the credits are subtracted from the debits
Expand All @@ -20,11 +22,7 @@ class Asset < Account
#
# @return [BigDecimal] The decimal value balance
def balance
unless contra
debits_balance - credits_balance
else
credits_balance - debits_balance
end
super
end

# This class method is used to return
Expand All @@ -38,16 +36,7 @@ def balance
#
# @return [BigDecimal] The decimal value balance
def self.balance
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |asset|
unless asset.contra
accounts_balance += asset.balance
else
accounts_balance -= asset.balance
end
end
accounts_balance
super
end
end
end
19 changes: 4 additions & 15 deletions app/models/plutus/equity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Plutus
# @author Michael Bulat
class Equity < Account

self.normal_credit_balance = true

# The balance of the account.
#
# Equity accounts have normal credit balances, so the debits are subtracted from the credits
Expand All @@ -20,11 +22,7 @@ class Equity < Account
#
# @return [BigDecimal] The decimal value balance
def balance
unless contra
credits_balance - debits_balance
else
debits_balance - credits_balance
end
super
end

# This class method is used to return
Expand All @@ -38,16 +36,7 @@ def balance
#
# @return [BigDecimal] The decimal value balance
def self.balance
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |equity|
unless equity.contra
accounts_balance += equity.balance
else
accounts_balance -= equity.balance
end
end
accounts_balance
super
end
end
end
19 changes: 4 additions & 15 deletions app/models/plutus/expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Plutus
# @author Michael Bulat
class Expense < Account

self.normal_credit_balance = false

# The balance of the account.
#
# Expenses have normal debit balances, so the credits are subtracted from the debits
Expand All @@ -20,11 +22,7 @@ class Expense < Account
#
# @return [BigDecimal] The decimal value balance
def balance
unless contra
debits_balance - credits_balance
else
credits_balance - debits_balance
end
super
end

# This class method is used to return
Expand All @@ -38,16 +36,7 @@ def balance
#
# @return [BigDecimal] The decimal value balance
def self.balance
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |expense|
unless expense.contra
accounts_balance += expense.balance
else
accounts_balance -= expense.balance
end
end
accounts_balance
super
end
end
end
19 changes: 4 additions & 15 deletions app/models/plutus/liability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Plutus
# @author Michael Bulat
class Liability < Account

self.normal_credit_balance = true

# The balance of the account.
#
# Liability accounts have normal credit balances, so the debits are subtracted from the credits
Expand All @@ -20,11 +22,7 @@ class Liability < Account
#
# @return [BigDecimal] The decimal value balance
def balance
unless contra
credits_balance - debits_balance
else
debits_balance - credits_balance
end
super
end

# This class method is used to return
Expand All @@ -38,16 +36,7 @@ def balance
#
# @return [BigDecimal] The decimal value balance
def self.balance
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |liability|
unless liability.contra
accounts_balance += liability.balance
else
accounts_balance -= liability.balance
end
end
accounts_balance
super
end
end
end
19 changes: 4 additions & 15 deletions app/models/plutus/revenue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Plutus
# @author Michael Bulat
class Revenue < Account

self.normal_credit_balance = true

# The balance of the account.
#
# Revenue accounts have normal credit balances, so the debits are subtracted from the credits
Expand All @@ -20,11 +22,7 @@ class Revenue < Account
#
# @return [BigDecimal] The decimal value balance
def balance
unless contra
credits_balance - debits_balance
else
debits_balance - credits_balance
end
super
end

# This class method is used to return
Expand All @@ -38,16 +36,7 @@ def balance
#
# @return [BigDecimal] The decimal value balance
def self.balance
accounts_balance = BigDecimal.new('0')
accounts = self.all
accounts.each do |revenue|
unless revenue.contra
accounts_balance += revenue.balance
else
accounts_balance -= revenue.balance
end
end
accounts_balance
super
end
end
end
8 changes: 7 additions & 1 deletion spec/models/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ module Plutus
end
end

it { should_not respond_to(:balance) }
it "calling the instance method #balance should raise a NoMethodError" do
expect { subject.balance }.to raise_error NoMethodError, "undefined method 'balance'"
end

it "calling the class method ::balance should raise a NoMethodError" do
expect { subject.class.balance }.to raise_error NoMethodError, "undefined method 'balance'"
end

describe ".trial_balance" do
subject { Account.trial_balance }
Expand Down

0 comments on commit 44d8c8a

Please sign in to comment.