From 6cd464de2fa02d3d9d17ec186d118abbdb3511fa Mon Sep 17 00:00:00 2001 From: Michael Bulat Date: Tue, 15 May 2012 12:39:55 -0400 Subject: [PATCH] making transaction validation methods private --- app/models/plutus/transaction.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/models/plutus/transaction.rb b/app/models/plutus/transaction.rb index 30deca82..bcaa6cb8 100644 --- a/app/models/plutus/transaction.rb +++ b/app/models/plutus/transaction.rb @@ -29,21 +29,10 @@ class Transaction < ActiveRecord::Base has_many :debit_accounts, :through => :debit_amounts, :source => :account validates_presence_of :description - validate :has_credit_amounts? - def has_credit_amounts? - errors[:base] << "Transaction must have at least one credit amount" if self.credit_amounts.blank? - end - validate :has_debit_amounts? - def has_debit_amounts? - errors[:base] << "Transaction must have at least one debit amount" if self.debit_amounts.blank? - end - validate :amounts_cancel? - def amounts_cancel? - errors[:base] << "The credit and debit amounts are not equal" if difference_of_amounts != 0 - end + # Simple API for building a transaction and associated debit and credit amounts # @@ -71,6 +60,18 @@ def self.build(hash) end private + def has_credit_amounts? + errors[:base] << "Transaction must have at least one credit amount" if self.credit_amounts.blank? + end + + def has_debit_amounts? + errors[:base] << "Transaction must have at least one debit amount" if self.debit_amounts.blank? + 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}