Created
February 21, 2012 10:08
-
-
Save StefanH/1875597 to your computer and use it in GitHub Desktop.
test and possible solution for #3965
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment