Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Order < ActiveRecord::Base
- belongs_to :user
- belongs_to :reward
- validates_presence_of :reward
- validates_presence_of :user
- validates_presence_of :title
- validates_presence_of :status
- validates_inclusion_of :status, :in => [:processing, :in_review, :ready_to_ship, :complete]
- validates_presence_of :tracking
- validates_presence_of :value
- validates_numericality_of :value
- validate do |order|
- order.errors.add(:value, "exceeds the cashout maximum of $#{Globals::max_cashout}, please request less per cashout") if order.value > Globals::max_cashout
- 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
- order.errors.add(:value, "exceeds your available balance") if order.value > order.user.balance
- end
- attr_accessible :item, :value
- after_create :deduct_from_balance
- before_destroy :add_back_to_balance
- def add_back_to_balance
- self.user.balance += self.value
- self.user.save
- end
- def deduct_from_balance
- #puts "user balance: #{self.user.balance} bet amount: #{self.amount}"
- self.user.balance -= self.value
- #puts ", balance after: #{self.user.balance}"
- self.user.save
- end
- end
Add Comment
Please, Sign In to add comment