diff options
author | Kristopher Murata <kris@kside.net> | 2010-04-08 03:39:46 -0400 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-04-08 10:26:16 -0700 |
commit | 36129f21b86db4bd69e932e586129e246c2a5ca8 (patch) | |
tree | f65bf7e2f8e16607ebd16d97c23e7bfd6b3a7c4e /activerecord/test | |
parent | ab5aa55cb86424286f65b273105a3602bbd77258 (diff) | |
download | rails-36129f21b86db4bd69e932e586129e246c2a5ca8.tar.gz rails-36129f21b86db4bd69e932e586129e246c2a5ca8.tar.bz2 rails-36129f21b86db4bd69e932e586129e246c2a5ca8.zip |
Dirty datetime attributes should be aware of time zone info [#3658 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index a64ccb2120..7a17ef1ee0 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -54,6 +54,89 @@ class DirtyTest < ActiveRecord::TestCase assert_nil pirate.catchphrase_change end + def test_time_attributes_changes_with_time_zone + in_time_zone 'Paris' do + target = Class.new(ActiveRecord::Base) + target.table_name = 'pirates' + + # New record - no changes. + pirate = target.new + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Saved - no changes. + pirate.catchphrase = 'arrrr, time zone!!' + pirate.save! + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Change created_on. + old_created_on = pirate.created_on + pirate.created_on = Time.now - 1.day + assert pirate.created_on_changed? + assert_kind_of ActiveSupport::TimeWithZone, pirate.created_on_was + assert_equal old_created_on, pirate.created_on_was + end + end + + def test_time_attributes_changes_without_time_zone_by_skip + in_time_zone 'Paris' do + target = Class.new(ActiveRecord::Base) + target.table_name = 'pirates' + + target.skip_time_zone_conversion_for_attributes = [:created_on] + + # New record - no changes. + pirate = target.new + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Saved - no changes. + pirate.catchphrase = 'arrrr, time zone!!' + pirate.save! + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Change created_on. + old_created_on = pirate.created_on + pirate.created_on = Time.now + 1.day + assert pirate.created_on_changed? + # kind_of does not work because + # ActiveSupport::TimeWithZone.name == 'Time' + assert_equal Time, pirate.created_on_was.class + assert_equal old_created_on, pirate.created_on_was + end + end + + def test_time_attributes_changes_without_time_zone + + target = Class.new(ActiveRecord::Base) + target.table_name = 'pirates' + + target.time_zone_aware_attributes = false + + # New record - no changes. + pirate = target.new + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Saved - no changes. + pirate.catchphrase = 'arrrr, time zone!!' + pirate.save! + assert !pirate.created_on_changed? + assert_nil pirate.created_on_change + + # Change created_on. + old_created_on = pirate.created_on + pirate.created_on = Time.now + 1.day + assert pirate.created_on_changed? + # kind_of does not work because + # ActiveSupport::TimeWithZone.name == 'Time' + assert_equal Time, pirate.created_on_was.class + assert_equal old_created_on, pirate.created_on_was + end + + def test_aliased_attribute_changes # the actual attribute here is name, title is an # alias setup via alias_attribute @@ -406,4 +489,16 @@ class DirtyTest < ActiveRecord::TestCase assert_equal %w(parrot_id), pirate.changed assert_nil pirate.parrot_id_was end + + def in_time_zone(zone) + old_zone = Time.zone + old_tz = ActiveRecord::Base.time_zone_aware_attributes + + Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil + ActiveRecord::Base.time_zone_aware_attributes = !zone.nil? + yield + ensure + Time.zone = old_zone + ActiveRecord::Base.time_zone_aware_attributes = old_tz + end end |