From eb5b93be74ed3eca925c1ab9bd4739919ae39a41 Mon Sep 17 00:00:00 2001 From: Scott Fleckenstein Date: Wed, 7 May 2008 06:54:07 -0700 Subject: Fix Time.zone.parse from stripping time zone information and make Time aware attribute methods use Time.zone.parse instead of to_time --- activesupport/lib/active_support/values/time_zone.rb | 3 +++ activesupport/test/time_zone_test.rb | 8 ++++++++ 2 files changed, 11 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 9cdc2a74ed..2342cd182c 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -214,6 +214,9 @@ class TimeZone # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) time = Time.parse(str, now) rescue DateTime.parse(str) + unless time.is_a?(DateTime) || Date._parse(str)[:offset].nil? + time += time.in_time_zone(self).utc_offset - time.utc_offset + end ActiveSupport::TimeWithZone.new(nil, self, time) end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 2f06c347a1..dd70186676 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -173,6 +173,14 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal zone, twz.time_zone end + def test_parse_string_with_timezone + (-11..13).each do |timezone_offset| + zone = TimeZone[timezone_offset] + twz = zone.parse('1999-12-31 19:00:00') + assert_equal twz, zone.parse(twz.to_s) + end + end + def test_parse_with_old_date silence_warnings do # silence warnings raised by tzinfo gem zone = TimeZone['Eastern Time (US & Canada)'] -- cgit v1.2.3 From fb9bf16e96e2c24d4996327500d8109b5d535e2c Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 20:56:38 -0500 Subject: Time.zone.parse: compatibility with far future date with time zone offset in string. Eliminate creation of additional TimeWithZone instance to determine utc offset. --- activesupport/lib/active_support/values/time_zone.rb | 7 ++++--- activesupport/test/time_zone_test.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 2342cd182c..d597d99880 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -214,10 +214,11 @@ class TimeZone # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) time = Time.parse(str, now) rescue DateTime.parse(str) - unless time.is_a?(DateTime) || Date._parse(str)[:offset].nil? - time += time.in_time_zone(self).utc_offset - time.utc_offset + if Date._parse(str)[:offset].nil? + ActiveSupport::TimeWithZone.new(nil, self, time) + else + time.in_time_zone(self) end - ActiveSupport::TimeWithZone.new(nil, self, time) end # Returns an ActiveSupport::TimeWithZone instance representing the current time diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index dd70186676..0bfb89b675 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -189,6 +189,15 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal zone, twz.time_zone end end + + def test_parse_far_future_date_with_time_zone_offset_in_string + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC + assert_equal [0,0,0,1,1,2051], twz.to_a[0,6] + assert_equal zone, twz.time_zone + end + end uses_mocha 'TestParseWithIncompleteDate' do def test_parse_with_incomplete_date -- cgit v1.2.3 From 06a7c2948a8dbf31357b552d468fcf42002736e7 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 21:30:17 -0500 Subject: Time.zone.parse: return nil for strings with no date information --- activesupport/lib/active_support/values/time_zone.rb | 4 +++- activesupport/test/time_zone_test.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index d597d99880..0fa99135e2 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -213,8 +213,10 @@ class TimeZone # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00 # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) + date_parts = Date._parse(str) + return if date_parts.blank? time = Time.parse(str, now) rescue DateTime.parse(str) - if Date._parse(str)[:offset].nil? + if date_parts[:offset].nil? ActiveSupport::TimeWithZone.new(nil, self, time) else time.in_time_zone(self) diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 0bfb89b675..3757ddcedb 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -198,6 +198,14 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal zone, twz.time_zone end end + + def test_parse_returns_nil_when_string_without_date_information_is_passed_in + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + assert_nil zone.parse('foobar') + assert_nil zone.parse(' ') + end + end uses_mocha 'TestParseWithIncompleteDate' do def test_parse_with_incomplete_date -- cgit v1.2.3 From 618d695f1115c00ce058950af199d5d4dc06385a Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 21:58:37 -0500 Subject: Updating changelogs --- activesupport/CHANGELOG | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 3ee9ed925c..989c72533a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Time.zone.parse return nil for strings with no date information [Geoff Buesing] + +* Time.zone.parse respects offset information in string. Resolves #105. [Scott Fleckenstein, Geoff Buesing] + * Added Ruby 1.8 implementation of Process.daemon * Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now [Geoff Buesing] -- cgit v1.2.3 From 4f03190f262c07c1f389957ff7ae76901495d824 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 22:07:21 -0500 Subject: TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/time_with_zone.rb | 8 ++++++++ activesupport/test/core_ext/time_with_zone_test.rb | 10 +++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 989c72533a..ea5490eb8f 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing [Geoff Buesing] + * Time.zone.parse return nil for strings with no date information [Geoff Buesing] * Time.zone.parse respects offset information in string. Resolves #105. [Scott Fleckenstein, Geoff Buesing] diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 461d52e40e..21ddcaad48 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -163,6 +163,14 @@ module ActiveSupport utc.advance(options).in_time_zone(time_zone) end + %w(year mon month day mday hour min sec).each do |method_name| + class_eval <<-EOV + def #{method_name} + time.#{method_name} + end + EOV + end + def usec time.respond_to?(:usec) ? time.usec : 0 end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index df70e82c1d..64fcb4af09 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -328,10 +328,18 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal Time.utc(1999, 12, 31, 19), mtime.time end end - + def test_method_missing_with_non_time_return_value + silence_warnings do # silence warnings raised by tzinfo gem + @twz.time.expects(:foo).returns('bar') + assert_equal 'bar', @twz.foo + end + end + + def test_date_part_value_methods silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) + twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class assert_equal 1999, twz.year assert_equal 12, twz.month assert_equal 31, twz.day -- cgit v1.2.3 From 66728087d0eb99a524498e8f24725dae6073edd6 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 22:48:47 -0500 Subject: Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today. ActionView date_helper uses Date.current to determine locale-appropriate default --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/date/calculations.rb | 5 +++++ activesupport/test/core_ext/date_ext_test.rb | 23 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index ea5490eb8f..f72825731e 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today [Geoff Buesing] + * TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing [Geoff Buesing] * Time.zone.parse return nil for strings with no date information [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 183471706b..1e2dbf118e 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -25,6 +25,11 @@ module ActiveSupport #:nodoc: def tomorrow ::Date.today.tomorrow end + + # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today. + def current + ::Time.zone_default ? ::Time.zone.today : ::Date.today + end end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 2e363c439a..5925ae3a61 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -208,6 +208,29 @@ class DateExtCalculationsTest < Test::Unit::TestCase end end end + + uses_mocha 'TestDateCurrent' do + def test_current_returns_date_today_when_zone_default_not_set + with_env_tz 'US/Central' do + Time.stubs(:now).returns Time.local(1999, 12, 31, 23) + assert_equal Date.new(1999, 12, 31), Date.today + assert_equal Date.new(1999, 12, 31), Date.current + end + end + + def test_current_returns_time_zone_today_when_zone_default_set + silence_warnings do # silence warnings raised by tzinfo gem + Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Central' do + Time.stubs(:now).returns Time.local(1999, 12, 31, 23) + assert_equal Date.new(1999, 12, 31), Date.today + assert_equal Date.new(2000, 1, 1), Date.current + end + end + ensure + Time.zone_default = nil + end + end protected def with_env_tz(new_tz = 'US/Eastern') -- cgit v1.2.3 From dc4eec1129520ce9863c9373d7cb79d8636ab7ca Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 9 May 2008 10:38:02 +0100 Subject: Merge docrails: commit e6afd8b2736364322b673bbdcca3e9b38b6d3da0 Author: Xavier Noria Date: Thu May 8 23:49:36 2008 +0200 Overall documentation improvement and markup corrections. Zillion changes. commit 2fead68b3192332eee27945ed95a94a64ca73f70 Author: Austin Putman Date: Wed May 7 19:35:46 2008 -0700 Documented class methods on ActionController::Routing. These are dangerous, and mostly used for testing. commit f5b84182dbc39bea79c8ee319c688d00fa99f9d1 Author: Teflon Ted Date: Wed May 7 16:08:49 2008 -0400 Added explanation about errant inflections not being patched in the future in order to avoid breaking legacy applications. commit 370f4f51722cec49ace17093d29e9ce9e8f15cfb Author: Sunny Ripert Date: Wed May 7 14:00:59 2008 +0200 Applied list conventions in AR::Base commit 5bd18429f09d44e75191bec42a6db04bd33f3030 Author: Sunny Ripert Date: Wed May 7 13:53:35 2008 +0200 Renamed Options list to Attributes list whenever they weren't option hashes in AR::Base commit d912bd5672316454457ae83f6e9dda5197beeb6f Author: Yaroslav Markin Date: Wed May 7 13:50:28 2008 +0400 Add a filter_parameter_logging usage hint to generated ApplicationController. This may help to remind the developer to filter sensitive information from application logs. Closes #11578 commit b243de0db3c2605121e055079854af5090d06374 Author: Jack Danger Canty Date: Tue May 6 23:39:47 2008 -0700 doc: disambiguating an example ActiveRecord class commit f81d771f0657ae8375b84a77a059812cce5d6fd9 Author: Jack Danger Canty Date: Tue May 6 23:35:05 2008 -0700 doc: ActiveRecord::Reflection::AssociationReflection#through_reflection Added documentation demonstrating the use of #through_reflection for finding intervening reflection objects for HasManyThrough and HasOneThrough. commit ae6b46f00b5b8b2939c6b37ce3329c83de7e71db Author: Cheah Chu Yeow Date: Wed May 7 13:47:41 2008 +0800 Document AttributeAssignmentError and MultiparameterAssignmentErrors. commit 8f463550b597db2156b67733f31aed13487fbc3a Author: John Barnette Date: Tue May 6 22:46:44 2008 -0700 Killing/fixing a bunch of outdated language in the AR README. commit aca44bcd92ef783abdf484b58abdde6786db0f89 Author: Cheah Chu Yeow Date: Wed May 7 13:34:52 2008 +0800 Make a note about ActiveResource::Timeouterror being raised when ARes calls timeout. commit 284a930a93fbee16e25d06392779dbf2f03e9e12 Author: Jonathan Dance Date: Tue May 6 14:58:26 2008 -0400 improvements to the page caching docs commit 9482da621390c874da7c921c8bd6230caae7035a Author: Sunny Ripert Date: Mon May 5 18:13:40 2008 +0200 validates_numericality_of() "integer" option really is "only_integer" commit e9afd6790a8f530528f6597a7f59bb283be754f6 Author: Sunny Ripert Date: Mon May 5 12:11:59 2008 +0200 Harmonized hash notation in AR::Base commit 67ebf14a91ffd970b582be4ff2991d691a9cf3e1 Author: Sunny Ripert Date: Mon May 5 12:06:19 2008 +0200 Turned options into rdoc-lists in AR::Base commit 0ec7c0a41d889d4e5382b9dff72f1aaba89bf297 Author: Marshall Huss Date: Sun May 4 23:21:33 2008 -0400 Added information of how to set element_name in the case the user has a name confliction with an existing model Signed-off-by: Pratik Naik --- .../core_ext/hash/indifferent_access.rb | 43 +++++++++++----------- activesupport/lib/active_support/inflector.rb | 5 +++ 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 2213b09144..c96c5160b3 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -23,10 +23,7 @@ class HashWithIndifferentAccess < Hash alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :regular_update, :update unless method_defined?(:regular_update) - # - # Assigns a new value to the hash. - # - # Example: + # Assigns a new value to the hash: # # hash = HashWithIndifferentAccess.new # hash[:key] = "value" @@ -35,25 +32,15 @@ class HashWithIndifferentAccess < Hash regular_writer(convert_key(key), convert_value(value)) end + # Updates the instantized hash with values from the second: # - # Updates the instantized hash with values from the second. - # - # Example: - # - # >> hash_1 = HashWithIndifferentAccess.new - # => {} - # - # >> hash_1[:key] = "value" - # => "value" - # - # >> hash_2 = HashWithIndifferentAccess.new - # => {} + # hash_1 = HashWithIndifferentAccess.new + # hash_1[:key] = "value" # - # >> hash_2[:key] = "New Value!" - # => "New Value!" + # hash_2 = HashWithIndifferentAccess.new + # hash_2[:key] = "New Value!" # - # >> hash_1.update(hash_2) - # => {"key"=>"New Value!"} + # hash_1.update(hash_2) # => {"key"=>"New Value!"} # def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } @@ -62,7 +49,13 @@ class HashWithIndifferentAccess < Hash alias_method :merge!, :update - # Checks the hash for a key matching the argument passed in + # Checks the hash for a key matching the argument passed in: + # + # hash = HashWithIndifferentAccess.new + # hash["key"] = "value" + # hash.key? :key # => true + # hash.key? "key" # => true + # def key?(key) super(convert_key(key)) end @@ -76,7 +69,13 @@ class HashWithIndifferentAccess < Hash super(convert_key(key), *extras) end - # Returns an array of the values at the specified indicies. + # Returns an array of the values at the specified indices: + # + # hash = HashWithIndifferentAccess.new + # hash[:a] = "x" + # hash[:b] = "y" + # hash.values_at("a", "b") # => ["x", "y"] + # def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index c8736549f4..68fbf3da35 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -3,6 +3,11 @@ require 'singleton' # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept # in inflections.rb. +# +# The Rails core team has stated patches for the inflections library will not be accepted +# in order to avoid breaking legacy applications which may be relying on errant inflections. +# If you discover an incorrect inflection and require it for your application, you'll need +# to correct it yourself (explained below). module Inflector # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. Examples: -- cgit v1.2.3