Skip to content

Commit

Permalink
ActiveRecord time zone aware attributes: blank string is treated as n…
Browse files Browse the repository at this point in the history
…il when assigned to writer
  • Loading branch information
gbuesing committed May 9, 2008
1 parent eb5b93b commit 328fada
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/attribute_methods.rb
Expand Up @@ -179,10 +179,10 @@ def define_write_method(attr_name)
def define_write_method_for_time_zone_conversion(attr_name)
method_body = <<-EOV
def #{attr_name}=(time)
if time
time = Time.zone.parse(time) rescue time unless time.acts_like?(:time)
time = time.in_time_zone if time.acts_like?(:time)
unless time.blank? || time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
time = time.in_time_zone rescue nil if time
write_attribute(:#{attr_name}, time)
end
EOV
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -186,6 +186,14 @@ def test_setting_time_zone_aware_attribute_with_string
end
end
end

def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
in_time_zone "Pacific Time (US & Canada)" do
record = @target.new
record.written_on = ' '
assert_nil record.written_on
end
end

def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone
time_string = 'Tue Jan 01 00:00:00 2008'
Expand Down

0 comments on commit 328fada

Please sign in to comment.