diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-05-09 10:51:57 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-09 10:51:57 +0100 |
commit | c99e54f4ec67cefe11787aa9ab9d8bd006e6ca71 (patch) | |
tree | c7548000db7be1adb3b12ec5e6d3877d9262f979 /activerecord | |
parent | e6afd8b2736364322b673bbdcca3e9b38b6d3da0 (diff) | |
parent | dc4eec1129520ce9863c9373d7cb79d8636ab7ca (diff) | |
download | rails-c99e54f4ec67cefe11787aa9ab9d8bd006e6ca71.tar.gz rails-c99e54f4ec67cefe11787aa9ab9d8bd006e6ca71.tar.bz2 rails-c99e54f4ec67cefe11787aa9ab9d8bd006e6ca71.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/associations_test.rb | 6 | ||||
-rwxr-xr-x | activerecord/test/cases/attribute_methods_test.rb | 35 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 4 |
7 files changed, 55 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 04cf72b38c..597b876f22 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Base#instantiate_time_object: eliminate check for Time.zone, since we can assume this is set if time_zone_aware_attributes is set to true [Geoff Buesing] + +* Time zone aware attribute methods use Time.zone.parse instead of #to_time for String arguments, so that offset information in String is respected. Resolves #105. [Scott Fleckenstein, Geoff Buesing] + * Added change_table for migrations (Jeff Dean) [#71]. Example: change_table :videos do |t| diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index c415ad2df3..68503a3c40 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -118,7 +118,7 @@ module ActiveRecord end def inspect - reload unless loaded? + load_target @target.inspect end @@ -167,7 +167,7 @@ module ActiveRecord def with_scope(*args, &block) @reflection.klass.send :with_scope, *args, &block end - + private def method_missing(method, *args) if load_target diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 46ecfc1969..2db27226f2 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -179,10 +179,10 @@ module ActiveRecord def define_write_method_for_time_zone_conversion(attr_name) method_body = <<-EOV def #{attr_name}=(time) - if time - time = time.to_time rescue time unless time.acts_like?(:time) - time = time.in_time_zone if time.acts_like?(:time) + unless 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 diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 60fa5890f0..74299bd572 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2588,7 +2588,7 @@ module ActiveRecord #:nodoc: end def instantiate_time_object(name, values) - if Time.zone && self.class.time_zone_aware_attributes && !self.class.skip_time_zone_conversion_for_attributes.include?(name.to_sym) + if self.class.time_zone_aware_attributes && !self.class.skip_time_zone_conversion_for_attributes.include?(name.to_sym) Time.zone.local(*values) else Time.time_with_datetime_fallback(@@default_timezone, *values) diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index ed2fab6d22..d8fe98bf57 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -149,6 +149,12 @@ class AssociationProxyTest < ActiveRecord::TestCase assert !david.projects.loaded? end + def test_inspect_does_not_reload_a_not_yet_loaded_target + andreas = Developer.new :name => 'Andreas', :log => 'new developer added' + assert !andreas.audit_logs.loaded? + assert_match(/message: "new developer added"/, andreas.audit_logs.inspect) + end + def test_save_on_parent_saves_children developer = Developer.create :name => "Bryan", :salary => 50_000 assert_equal 1, developer.reload.audit_logs.size diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 61a049ab36..c336fd9afb 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -173,6 +173,41 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end + def test_setting_time_zone_aware_attribute_with_string + utc_time = Time.utc(2008, 1, 1) + (-11..13).each do |timezone_offset| + time_string = utc_time.in_time_zone(timezone_offset).to_s + in_time_zone "Pacific Time (US & Canada)" do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time + 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' + (-11..13).each do |timezone_offset| + in_time_zone timezone_offset do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone[timezone_offset], record.written_on.time_zone + assert_equal Time.utc(2008, 1, 1), record.written_on.time + end + end + end + def test_setting_time_zone_aware_attribute_in_current_time_zone utc_time = Time.utc(2008, 1, 1) in_time_zone "Pacific Time (US & Canada)" do diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 192c2cb5ab..f77fd0e96d 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -49,6 +49,10 @@ class Developer < ActiveRecord::Base before_create do |developer| developer.audit_logs.build :message => "Computer created" end + + def log=(message) + audit_logs.build :message => message + end end class AuditLog < ActiveRecord::Base |