Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Pettycash < ActiveRecord::Base
- include Money
- belongs_to :business_day
- validates_presence_of :amount
- validates_presence_of :reason
- validates_presence_of :account1
- validates_presence_of :account2
- before_create :not_zero
- validate :must_not_have_discrepancy
- named_scope :positive, :conditions => 'amount > 0'
- named_scope :negative, :conditions => 'amount < 0'
- def not_zero
- amount != 0
- end
- SafeBalance::SIZES.each do |f|
- define_method("out_#{f}=") {|amount| write_attribute "out_#{f}".to_sym, dollars_to_cents(amount)}
- define_method("in_#{f}=") {|amount| write_attribute "in_#{f}".to_sym, dollars_to_cents(amount)}
- define_method("spent_#{f}") {send("out_#{f}") - send("in_#{f}")}
- end
- def amount=(amount)
- write_attribute :amount, dollars_to_cents(amount)
- end
- def out_total
- out_hundreds + out_fifties + out_twenties + out_tens + out_fives + out_ones + out_quarters + out_dimes + out_nickels + out_pennies
- end
- def in_total
- in_hundreds + in_fifties + in_twenties + in_tens + in_fives + in_ones + in_quarters + in_dimes + in_nickels + in_pennies
- end
- def total
- out_total - in_total
- end
- def discrepancy?
- total != amount
- end
- protected
- def must_not_have_discrepancy
- errors.add("amount", "does not match totals given.") if discrepancy?
- end
- end
Add Comment
Please, Sign In to add comment