From 65a7c571050da47ab997a5a07fb9f1e2302bf348 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 18 Feb 2014 09:35:11 +0100 Subject: time helpers honor the application time zone when passed a date Rails applications are expected to be always aware of the application time zone. To be consistent with that contract, we have to assume that a bare date passed to time helpers is a date in the application time zone, not in the system time zone. The system time zone is irrelevant, we should totally ignore it. For example, travel_to user.birth_date + 40.years should make that user be 40th years old regardless of the system time zone. Without this patch that may not be true. --- .../lib/active_support/testing/time_helpers.rb | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 9e0a3d6345..b36f3cf94c 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -61,14 +61,23 @@ module ActiveSupport travel_to Time.now + duration, &block end - # Changes current time to the given time by stubbing +Time.now+ and +Date.today+ to return the - # time or date passed into this method. + # Changes current time to the given time by stubbing +Time.now+ and + # +Date.today+ to return the time or date passed into this method. # # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 # travel_to Time.new(2004, 11, 24, 01, 04, 44) # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00 # Date.current # => Wed, 24 Nov 2004 # + # Dates are taken as their timestamp at the beginning of the day in the + # application time zone. Time.current returns said timestamp, + # and Time.now its equivalent in the system time zone. Similarly, + # Date.current returns a date equal to the argument, and + # Date.today the date according to Time.now, which may + # be different. (Note that you rarely want to deal with Time.now, + # or Date.today, in order to honor the application time zone + # please always use Time.current and Date.current.) + # # This method also accepts a block, which will return the current time back to its original # state at the end of the block: # @@ -78,8 +87,14 @@ module ActiveSupport # end # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 def travel_to(date_or_time, &block) - simple_stubs.stub_object(Time, :now, date_or_time.to_time) - simple_stubs.stub_object(Date, :today, date_or_time.to_date) + if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime) + now = date_or_time.midnight.to_time + else + now = date_or_time.to_time + end + + simple_stubs.stub_object(Time, :now, now) + simple_stubs.stub_object(Date, :today, now.to_date) if block_given? block.call -- cgit v1.2.3 From 5d037819ca80606638212f83de741cc2041db28f Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 18 Feb 2014 10:22:43 +0100 Subject: travel_to travels back and re-raises if the block raises --- activesupport/lib/active_support/testing/time_helpers.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index b36f3cf94c..eefa84262e 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -10,7 +10,7 @@ module ActiveSupport def stub_object(object, method_name, return_value) key = [object.object_id, method_name] - if (stub = @stubs[key]) + if stub = @stubs[key] unstub_object(stub) end @@ -97,8 +97,11 @@ module ActiveSupport simple_stubs.stub_object(Date, :today, now.to_date) if block_given? - block.call - travel_back + begin + block.call + ensure + travel_back + end end end -- cgit v1.2.3 From 4edca106daacc5a159289eae255207d160f22396 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 18 Feb 2014 15:20:15 +0100 Subject: Added Object#present_in to simplify value whitelisting --- .../lib/active_support/core_ext/object/inclusion.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index b5671f66d0..141f19e7b3 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -12,4 +12,16 @@ class Object rescue NoMethodError raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end + + # Returns the receiver if it's included in the argument otherwise returns +nil+. + # Argument must be any object which responds to +#include?+. Usage: + # + # params[:bucket_type].present_in %w( project calendar ) + # + # This will throw an ArgumentError if the argument doesn't respond to +#include?+. + # + # @return [Object] + def present_in(another_object) + self.in?(another_object) ? self : nil + end end -- cgit v1.2.3 From 8b20c72dd80e2faf531f308d430a145a253aeac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 18 Feb 2014 15:45:20 -0300 Subject: Preparing for 4.1.0.beta2 release --- activesupport/lib/active_support/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/version.rb b/activesupport/lib/active_support/version.rb index b3f0e7198d..b9d6417b07 100644 --- a/activesupport/lib/active_support/version.rb +++ b/activesupport/lib/active_support/version.rb @@ -1,7 +1,7 @@ module ActiveSupport # Returns the version of the currently loaded ActiveSupport as a Gem::Version def self.version - Gem::Version.new "4.1.0.beta1" + Gem::Version.new "4.1.0.beta2" end module VERSION #:nodoc: -- cgit v1.2.3