Guest User

Untitled

a guest
May 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. class Order < ActiveRecord::Base
  2. belongs_to :user
  3. belongs_to :reward
  4. validates_presence_of :reward
  5. validates_presence_of :user
  6. validates_presence_of :title
  7. validates_presence_of :status
  8. validates_inclusion_of :status, :in => [:processing, :in_review, :ready_to_ship, :complete]
  9. validates_presence_of :tracking
  10. validates_presence_of :value
  11. validates_numericality_of :value
  12.  
  13. validate do |order|
  14. order.errors.add(:value, "exceeds the cashout maximum of $#{Globals::max_cashout}, please request less per cashout") if order.value > Globals::max_cashout
  15. order.errors.add(:value, "is less than the cashout minimum of $#{Globals::min_cashout}, you need to earn $#{order.user.balance - Globals::min_cashout} more to cash out") if order.value < Globals::min_cashout
  16. order.errors.add(:value, "exceeds your available balance") if order.value > order.user.balance
  17. end
  18. attr_accessible :item, :value
  19.  
  20. after_create :deduct_from_balance
  21. before_destroy :add_back_to_balance
  22.  
  23. def add_back_to_balance
  24. self.user.balance += self.value
  25. self.user.save
  26. end
  27. def deduct_from_balance
  28. #puts "user balance: #{self.user.balance} bet amount: #{self.amount}"
  29. self.user.balance -= self.value
  30. #puts ", balance after: #{self.user.balance}"
  31. self.user.save
  32. end
  33. end
Add Comment
Please, Sign In to add comment