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:
parent
363fd73bc4
commit
cac9e16c17
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user