public
Created

test and possible solution for #3965

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 433d508..10ae5dc 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -86,7 +86,15 @@ module ActiveRecord
# be typecast back to 0 (''.to_i => 0)
value = nil
else
- value = column.type_cast(value)
+ # if time zone aware attributes, attribute is
+ if time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(attr.to_sym) && [:datetime, :timestamp].include?(column.type)
+ unless value.acts_like?(:time)
+ value = value.is_a?(String) ? Time.zone.parse(value) : value.to_time rescue value
+ end
+ value = value.in_time_zone rescue nil if value
+ else
+ value = column.type_cast(value)
+ end
end
end
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 4078b7e..1f503ee 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -624,6 +624,20 @@ class AttributeMethodsTest < ActiveRecord::TestCase
end
end
end
+
+ def test_time_zone_aware_attribute_dirty_tracking
+ time_string_1 = "2012-02-20 10:00:00"
+ time_string_2 = "2012-02-20 09:00:00"
+
+ in_time_zone 1 do
+ record = @target.create(:written_on => time_string_1)
+
+ record.written_on = time_string_2
+ assert record.written_on_changed?, "expected written_on to be changed"
+ record.written_on = time_string_1
+ assert !record.written_on_changed?, "expected written_on not to be changed"
+ end
+ end
def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
in_time_zone "Pacific Time (US & Canada)" do

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.