1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

cant debit/credit a negative

This commit is contained in:
Peng DU 2016-07-05 10:38:39 +02:00
parent 363fd73bc4
commit cac9e16c17
2 changed files with 21 additions and 4 deletions

View File

@ -5,12 +5,18 @@ class Wallet < ActiveRecord::Base
validates_numericality_of :amount, greater_than_or_equal_to: 0
def credit(amount)
self.amount += amount
save
if amount.is_a?(Numeric) and amount >= 0
self.amount += amount
return save
end
false
end
def debit(amount)
self.amount -= amount
save
if amount.is_a?(Numeric) and amount >= 0
self.amount -= amount
return save
end
false
end
end

View File

@ -25,4 +25,15 @@ class WalletTest < ActiveSupport::TestCase
assert w.debit(5)
assert_equal w.amount, expected_amount
end
test 'cant debit/credit a negative' do
w = Wallet.new
assert_not w.credit(-5)
assert_not w.debit(-5)
end
test 'wallet amount cant < 0 after debit' do
w = Wallet.new
assert_not w.debit(5)
end
end