Guest User

Untitled

a guest
Jun 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. class Pettycash < ActiveRecord::Base
  2. include Money
  3. belongs_to :business_day
  4. validates_presence_of :amount
  5. validates_presence_of :reason
  6. validates_presence_of :account1
  7. validates_presence_of :account2
  8. before_create :not_zero
  9. validate :must_not_have_discrepancy
  10.  
  11. named_scope :positive, :conditions => 'amount > 0'
  12. named_scope :negative, :conditions => 'amount < 0'
  13.  
  14. def not_zero
  15. amount != 0
  16. end
  17.  
  18. SafeBalance::SIZES.each do |f|
  19. define_method("out_#{f}=") {|amount| write_attribute "out_#{f}".to_sym, dollars_to_cents(amount)}
  20. define_method("in_#{f}=") {|amount| write_attribute "in_#{f}".to_sym, dollars_to_cents(amount)}
  21. define_method("spent_#{f}") {send("out_#{f}") - send("in_#{f}")}
  22. end
  23.  
  24. def amount=(amount)
  25. write_attribute :amount, dollars_to_cents(amount)
  26. end
  27.  
  28. def out_total
  29. out_hundreds + out_fifties + out_twenties + out_tens + out_fives + out_ones + out_quarters + out_dimes + out_nickels + out_pennies
  30. end
  31.  
  32. def in_total
  33. in_hundreds + in_fifties + in_twenties + in_tens + in_fives + in_ones + in_quarters + in_dimes + in_nickels + in_pennies
  34. end
  35.  
  36. def total
  37. out_total - in_total
  38. end
  39.  
  40. def discrepancy?
  41. total != amount
  42. end
  43.  
  44. protected
  45.  
  46. def must_not_have_discrepancy
  47. errors.add("amount", "does not match totals given.") if discrepancy?
  48. end
  49. end
Add Comment
Please, Sign In to add comment