Skip to content
This repository
Browse code

ActiveRecord time zone aware attributes: blank string is treated as n…

…il when assigned to writer
  • Loading branch information...
commit 328fada610aa9128386bc4b372d3e0b68aede945 1 parent eb5b93b
Geoff Buesing gbuesing authored
6 activerecord/lib/active_record/attribute_methods.rb
@@ -179,10 +179,10 @@ def define_write_method(attr_name)
179 179 def define_write_method_for_time_zone_conversion(attr_name)
180 180 method_body = <<-EOV
181 181 def #{attr_name}=(time)
182   - if time
183   - time = Time.zone.parse(time) rescue time unless time.acts_like?(:time)
184   - time = time.in_time_zone if time.acts_like?(:time)
  182 + unless time.blank? || time.acts_like?(:time)
  183 + time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
185 184 end
  185 + time = time.in_time_zone rescue nil if time
186 186 write_attribute(:#{attr_name}, time)
187 187 end
188 188 EOV
8 activerecord/test/cases/attribute_methods_test.rb
@@ -186,6 +186,14 @@ def test_setting_time_zone_aware_attribute_with_string
186 186 end
187 187 end
188 188 end
  189 +
  190 + def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
  191 + in_time_zone "Pacific Time (US & Canada)" do
  192 + record = @target.new
  193 + record.written_on = ' '
  194 + assert_nil record.written_on
  195 + end
  196 + end
189 197
190 198 def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone
191 199 time_string = 'Tue Jan 01 00:00:00 2008'

0 comments on commit 328fada

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