Skip to content
This repository
Browse code

Better last chance check

  • Loading branch information...
commit 9bffceb52942bd359915de856ca71f161298ff85 1 parent 6c7d57d
David FRANCOIS authored December 20, 2011
3  app/models/account_operation.rb
@@ -16,7 +16,8 @@ class AccountOperation < ActiveRecord::Base
16 16
   attr_accessible :amount, :currency
17 17
    
18 18
   validates :amount,
19  
-    :presence => true
  19
+    :presence => true,
  20
+    :user_balance => true
20 21
   
21 22
   validates :currency,
22 23
     :presence => true,
2  lib/validators/user_balance_validator.rb
... ...
@@ -1,6 +1,6 @@
1 1
 class UserBalanceValidator < ActiveModel::EachValidator
2 2
   def validate_each(record, field, value)
3  
-    if record.new_record? and value and (value < 0) and (value.abs > record.account.balance(record.currency))
  3
+    if record.new_record? and value and (value < 0) and record.account.is_a?(User) and (value.abs > record.account.balance(record.currency))
4 4
       record.errors[field] << (I18n.t "errors.greater_than_balance", :balance => record.account.balance(record.currency), :currency => record.currency)
5 5
     end
6 6
   end
9  test/unit/account_operation_test.rb
@@ -86,4 +86,13 @@ class AccountOperationTest < ActiveSupport::TestCase
86 86
     assert_equal JSON::parse(AccountOperation.new.to_json({})),
87 87
       JSON::parse("{\"comment\":null,\"bt_tx_id\":null,\"address\":null,\"created_at\":null,\"unread\":null,\"amount\":0.0,\"bt_tx_confirmations\":0,\"currency\":null,\"email\":null}")
88 88
   end
  89
+  
  90
+  # Under no circumstances should a user account have a negative balance
  91
+  test "should not lead to negative balance" do
  92
+    u = Factory(:user)
  93
+
  94
+    assert_no_difference ("u.balance(:eur)") do
  95
+      add_money(u, -1000, :eur)
  96
+    end
  97
+  end
89 98
 end

0 notes on commit 9bffceb

Please sign in to comment.
Something went wrong with that request. Please try again.