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

[#860 state:resolved]
joshsusser (author)
Mon Aug 18 15:56:37 -0700 2008
jeremy (committer)
Fri Aug 22 16:26:40 -0700 2008
commit  e48e77e0222292176cd9f68658dd54524f582d9b
tree    b264ca7a6fb67fd7acad9d8f3eb1eb06fd8ddc41
parent  707ee0e2695e85186d59aa407f09691ebfcc3125
...
2607
2608
2609
2610
2611
2612
2613
2614
 
 
 
 
 
 
 
 
2615
2616
2617
...
2607
2608
2609
 
 
 
 
 
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
0
@@ -2607,11 +2607,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.is_a?(String) && value.blank?
0
+            nil
0
+          else
0
+            TRUE_VALUES.include?(value)
0
+          end
0
         end
0
 
0
         # convert something to a BigDecimal
...
138
139
140
141
 
142
143
144
...
1114
1115
1116
 
 
1117
1118
1119
1120
1121
 
 
1122
1123
1124
...
1126
1127
1128
 
 
1129
1130
1131
1132
1133
 
 
1134
1135
1136
...
138
139
140
 
141
142
143
144
...
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
...
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
0
@@ -138,7 +138,7 @@ class BasicsTest < ActiveRecord::TestCase
0
   if current_adapter?(:MysqlAdapter)
0
     def test_read_attributes_before_type_cast_on_boolean
0
       bool = Booleantest.create({ "value" => false })
0
-      assert_equal 0, bool.attributes_before_type_cast["value"]
0
+      assert_equal "0", bool.reload.attributes_before_type_cast["value"]
0
     end
0
   end
0
 
0
@@ -1114,11 +1114,15 @@ class BasicsTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_boolean
0
+    b_nil = Booleantest.create({ "value" => nil })
0
+    nil_id = b_nil.id
0
     b_false = Booleantest.create({ "value" => false })
0
     false_id = b_false.id
0
     b_true = Booleantest.create({ "value" => true })
0
     true_id = b_true.id
0
 
0
+    b_nil = Booleantest.find(nil_id)
0
+    assert_nil b_nil.value
0
     b_false = Booleantest.find(false_id)
0
     assert !b_false.value?
0
     b_true = Booleantest.find(true_id)
0
@@ -1126,11 +1130,15 @@ class BasicsTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_boolean_cast_from_string
0
+    b_blank = Booleantest.create({ "value" => "" })
0
+    blank_id = b_blank.id
0
     b_false = Booleantest.create({ "value" => "0" })
0
     false_id = b_false.id
0
     b_true = Booleantest.create({ "value" => "1" })
0
     true_id = b_true.id
0
 
0
+    b_blank = Booleantest.find(blank_id)
0
+    assert_nil b_blank.value
0
     b_false = Booleantest.find(false_id)
0
     assert !b_false.value?
0
     b_true = Booleantest.find(true_id)
...
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
...
60
61
62
63
 
64
65
66
...
60
61
62
 
63
64
65
66
0
@@ -60,7 +60,7 @@ ActiveRecord::Schema.define do
0
   end
0
 
0
   create_table :booleantests, :force => true do |t|
0
-    t.integer :value
0
+    t.boolean :value
0
   end
0
 
0
   create_table :categories, :force => true do |t|

Comments