mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +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
|
validates_numericality_of :amount, greater_than_or_equal_to: 0
|
||||||
|
|
||||||
def credit(amount)
|
def credit(amount)
|
||||||
self.amount += amount
|
if amount.is_a?(Numeric) and amount >= 0
|
||||||
save
|
self.amount += amount
|
||||||
|
return save
|
||||||
|
end
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def debit(amount)
|
def debit(amount)
|
||||||
self.amount -= amount
|
if amount.is_a?(Numeric) and amount >= 0
|
||||||
save
|
self.amount -= amount
|
||||||
|
return save
|
||||||
|
end
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,4 +25,15 @@ class WalletTest < ActiveSupport::TestCase
|
|||||||
assert w.debit(5)
|
assert w.debit(5)
|
||||||
assert_equal w.amount, expected_amount
|
assert_equal w.amount, expected_amount
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user