Skip to content
This repository
Browse code

Make before_type_cast available for datetime fields

[#3973 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
commit 817e37013610c8e8866197594d5e408b4d5daec5 1 parent ed2eb6f
Akira Matsuda authored December 16, 2010 spastorino committed February 01, 2011
5  activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -40,12 +40,13 @@ def _#{attr_name}(reload = false)
40 40
           def define_method_attribute=(attr_name)
41 41
             if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
42 42
               method_body, line = <<-EOV, __LINE__ + 1
43  
-                def #{attr_name}=(time)
  43
+                def #{attr_name}=(original_time)
  44
+                  time = original_time.dup
44 45
                   unless time.acts_like?(:time)
45 46
                     time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
46 47
                   end
47 48
                   time = time.in_time_zone rescue nil if time
48  
-                  write_attribute(:#{attr_name}, time)
  49
+                  write_attribute(:#{attr_name}, (time || original_time))
49 50
                 end
50 51
               EOV
51 52
               generated_attribute_methods.module_eval(method_body, __FILE__, line)
31  activerecord/test/cases/attribute_methods_test.rb
@@ -116,24 +116,23 @@ def test_read_attributes_before_type_cast_on_boolean
116 116
     end
117 117
   end
118 118
 
119  
-  unless current_adapter?(:Mysql2Adapter)
120  
-    def test_read_attributes_before_type_cast_on_datetime
121  
-      developer = Developer.find(:first)
122  
-      # Oracle adapter returns Time before type cast
123  
-      unless current_adapter?(:OracleAdapter)
124  
-        assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s
125  
-      else
126  
-        assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db)
  119
+  def test_read_attributes_before_type_cast_on_datetime
  120
+    developer = Developer.find(:first)
  121
+    if current_adapter?(:Mysql2Adapter)
  122
+      # Mysql2 keeps the value in Time instance
  123
+      assert_equal developer.created_at.to_s(:db), developer.attributes_before_type_cast["created_at"].to_s(:db)
  124
+    else
  125
+      assert_equal developer.created_at.to_s(:db), developer.attributes_before_type_cast["created_at"].to_s
  126
+    end
127 127
 
128  
-        developer.created_at = "345643456"
129  
-        assert_equal developer.created_at_before_type_cast, "345643456"
130  
-        assert_equal developer.created_at, nil
  128
+    developer.created_at = "345643456"
131 129
 
132  
-        developer.created_at = "2010-03-21 21:23:32"
133  
-        assert_equal developer.created_at_before_type_cast.to_s, "2010-03-21 21:23:32"
134  
-        assert_equal developer.created_at, Time.parse("2010-03-21 21:23:32")
135  
-      end
136  
-    end
  130
+    assert_equal developer.created_at_before_type_cast, "345643456"
  131
+    assert_equal developer.created_at, nil
  132
+
  133
+    developer.created_at = "2010-03-21 21:23:32"
  134
+    assert_equal developer.created_at_before_type_cast.to_s, "2010-03-21 21:23:32"
  135
+    assert_equal developer.created_at, Time.parse("2010-03-21 21:23:32")
137 136
   end
138 137
 
139 138
   def test_hash_content

0 notes on commit 817e370

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