Skip to content
This repository
Browse code

return early from typecasting if the value is nil

  • Loading branch information...
commit 20440fd8700e1eb9725ffa0e85d458a81b519dd2 1 parent 9454076
Aaron Patterson authored February 02, 2012
16  activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -25,6 +25,8 @@ def type_cast(value)
25 25
 
26 26
         class Money
27 27
           def type_cast(value)
  28
+            return if value.nil?
  29
+
28 30
             # Because money output is formatted according to the locale, there are two
29 31
             # cases to consider (note the decimal separators):
30 32
             #  (1) $12,345,678.12
@@ -62,18 +64,24 @@ def type_cast(value)
62 64
 
63 65
         class Integer
64 66
           def type_cast(value)
65  
-            value.to_i
  67
+            return if value.nil?
  68
+
  69
+            value.to_i rescue value ? 1 : 0
66 70
           end
67 71
         end
68 72
 
69 73
         class Boolean
70 74
           def type_cast(value)
  75
+            return if value.nil?
  76
+
71 77
             ConnectionAdapters::Column.value_to_boolean value
72 78
           end
73 79
         end
74 80
 
75 81
         class Timestamp
76 82
           def type_cast(value)
  83
+            return if value.nil?
  84
+
77 85
             # FIXME: probably we can improve this since we know it is PG
78 86
             # specific
79 87
             ConnectionAdapters::Column.string_to_time value
@@ -82,6 +90,8 @@ def type_cast(value)
82 90
 
83 91
         class Date
84 92
           def type_cast(value)
  93
+            return if value.nil?
  94
+
85 95
             # FIXME: probably we can improve this since we know it is PG
86 96
             # specific
87 97
             ConnectionAdapters::Column.value_to_date value
@@ -90,6 +100,8 @@ def type_cast(value)
90 100
 
91 101
         class Time
92 102
           def type_cast(value)
  103
+            return if value.nil?
  104
+
93 105
             # FIXME: probably we can improve this since we know it is PG
94 106
             # specific
95 107
             ConnectionAdapters::Column.string_to_dummy_time value
@@ -98,6 +110,8 @@ def type_cast(value)
98 110
 
99 111
         class Float
100 112
           def type_cast(value)
  113
+            return if value.nil?
  114
+
101 115
             value.to_f
102 116
           end
103 117
         end

0 notes on commit 20440fd

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