Skip to content

Commit

Permalink
Refactor Plutus::Transaction validation to use AmountsExtension
Browse files Browse the repository at this point in the history
This reduces the number of different pieces of code that sums
Amounts, incidentally also catching a previously unnoticed bug in
Plutus::Transaction#difference_of_amounts, captured in the updated
spec.
  • Loading branch information
willglynn committed Feb 18, 2013
1 parent c08f3c5 commit 1fea8af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion app/models/plutus/amounts_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ module AmountsExtension
def balance
balance = BigDecimal.new('0')
each do |amount_record|
balance += amount_record.amount
if amount_record.amount
balance += amount_record.amount
else
balance = nil
end
end
return balance
end
Expand Down
12 changes: 3 additions & 9 deletions app/models/plutus/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Transaction < ActiveRecord::Base
attr_accessible :description, :commercial_document

belongs_to :commercial_document, :polymorphic => true
has_many :credit_amounts
has_many :debit_amounts
has_many :credit_amounts, :extend => AmountsExtension
has_many :debit_amounts, :extend => AmountsExtension
has_many :credit_accounts, :through => :credit_amounts, :source => :account
has_many :debit_accounts, :through => :debit_amounts, :source => :account

Expand Down Expand Up @@ -71,13 +71,7 @@ def has_debit_amounts?
end

def amounts_cancel?
errors[:base] << "The credit and debit amounts are not equal" if difference_of_amounts != 0
end

def difference_of_amounts
credit_amount_total = credit_amounts.inject(0) {|sum, credit_amount| sum + credit_amount.amount.to_i}
debit_amount_total = debit_amounts.inject(0) {|sum, debit_amount| sum + debit_amount.amount.to_i}
credit_amount_total - debit_amount_total
errors[:base] << "The credit and debit amounts are not equal" if credit_amounts.balance != debit_amounts.balance
end
end
end
8 changes: 8 additions & 0 deletions spec/models/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ module Plutus
transaction.errors['base'].should == ["The credit and debit amounts are not equal"]
end

it "should require the debit and credit amounts to cancel even with fractions" do
transaction = FactoryGirl.build(:transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :amount => 100.1, :transaction => transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :amount => 100.2, :transaction => transaction)
transaction.should_not be_valid
transaction.errors['base'].should == ["The credit and debit amounts are not equal"]
end

it "should have a polymorphic commercial document associations" do
mock_document = FactoryGirl.create(:asset) # one would never do this, but it allows us to not require a migration for the test
transaction = FactoryGirl.build(:transaction_with_credit_and_debit, :commercial_document => mock_document)
Expand Down

0 comments on commit 1fea8af

Please sign in to comment.