public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
coerce blank strings to nil values for boolean and integer fields

Signed-off-by: Michael Koziarski <michael@koziarski.com>
joshsusser (author)
Mon Aug 18 15:56:37 -0700 2008
NZKoz (committer)
Thu Aug 21 10:33:25 -0700 2008
commit  aee14630d4dc0856e597794cc731fac68c2d2e34
tree    f3ad416bc299563352fcaa70c1bb15cad6191ca7
parent  8622787f8748434b4ceb2b925a35b17a38e1f2d6
...
2572
2573
2574
2575
2576
2577
2578
2579
 
 
 
 
 
 
 
 
2580
2581
2582
...
2572
2573
2574
 
 
 
 
 
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
0
@@ -2572,11 +2572,14 @@ module ActiveRecord #:nodoc:
0
       end
0
 
0
       def convert_number_column_value(value)
0
-        case value
0
-          when FalseClass; 0
0
-          when TrueClass;  1
0
-          when '';         nil
0
-          else value
0
+        if value == false
0
+          0
0
+        elsif value == true
0
+          1
0
+        elsif value.is_a?(String) && value.blank?
0
+          nil
0
+        else
0
+          value
0
         end
0
       end
0
 
...
138
139
140
141
 
 
 
 
 
142
143
144
...
138
139
140
 
141
142
143
144
145
146
147
148
0
@@ -138,7 +138,11 @@ module ActiveRecord
0
 
0
         # convert something to a boolean
0
         def value_to_boolean(value)
0
-          TRUE_VALUES.include?(value)
0
+          if value.blank?
0
+            nil
0
+          else
0
+            TRUE_VALUES.include?(value)
0
+          end
0
         end
0
 
0
         # convert something to a BigDecimal
...
1420
1421
1422
1423
1424
 
 
1425
1426
1427
...
1434
1435
1436
1437
1438
 
 
1439
1440
1441
...
1420
1421
1422
 
 
1423
1424
1425
1426
1427
...
1434
1435
1436
 
 
1437
1438
1439
1440
1441
0
@@ -1420,8 +1420,8 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
0
   def test_validates_numericality_of_with_nil_allowed
0
     Topic.validates_numericality_of :approved, :allow_nil => true
0
 
0
-    invalid!(BLANK + JUNK)
0
-    valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
0
+    invalid!(JUNK)
0
+    valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
0
   end
0
 
0
   def test_validates_numericality_of_with_integer_only
0
@@ -1434,8 +1434,8 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
0
   def test_validates_numericality_of_with_integer_only_and_nil_allowed
0
     Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
0
 
0
-    invalid!(BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
0
-    valid!(NIL + INTEGERS)
0
+    invalid!(JUNK + FLOATS + BIGDECIMAL + INFINITY)
0
+    valid!(NIL + BLANK + INTEGERS)
0
   end
0
 
0
   def test_validates_numericality_with_greater_than

Comments