diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 16 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/inclusion.rb | 12 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/time_helpers.rb | 32 | ||||
-rw-r--r-- | activesupport/lib/active_support/version.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/object/inclusion_test.rb | 5 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 26 |
6 files changed, 85 insertions, 8 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 43bfeff079..713bb3c1e2 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,19 @@ +* Added `Object#present_in` to simplify value whitelisting. + + Before: + + params[:bucket_type].in?(%w( project calendar )) ? params[:bucket_type] : nil + + After: + + params[:bucket_type].present_in %w( project calendar ) + + *DHH* + +* Time helpers honor the application time zone when passed a date. + + *Xavier Noria* + * Fix the implementation of Multibyte::Unicode.tidy_bytes for JRuby The existing implementation caused JRuby to raise the error: 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 diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 9e0a3d6345..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 @@ -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. <tt>Time.current</tt> returns said timestamp, + # and <tt>Time.now</tt> its equivalent in the system time zone. Similarly, + # <tt>Date.current</tt> returns a date equal to the argument, and + # <tt>Date.today</tt> the date according to <tt>Time.now</tt>, which may + # be different. (Note that you rarely want to deal with <tt>Time.now</tt>, + # or <tt>Date.today</tt>, in order to honor the application time zone + # please always use <tt>Time.current</tt> and <tt>Date.current</tt>.) + # # This method also accepts a block, which will return the current time back to its original # state at the end of the block: # @@ -78,12 +87,21 @@ 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 - travel_back + begin + block.call + ensure + travel_back + end end end 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: diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 478706eeae..c5e2cc693a 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -47,4 +47,9 @@ class InTest < ActiveSupport::TestCase def test_no_method_catching assert_raise(ArgumentError) { 1.in?(1) } end + + def test_present_in + assert_equal "stuff", "stuff".present_in(%w( lots of stuff )) + assert_nil "stuff".present_in(%w( lots of crap )) + end end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index cd79efbe8c..79ec57af2b 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -124,6 +124,32 @@ class TimeZoneTest < ActiveSupport::TestCase travel_back end + def test_travel_to_a_date + with_env_tz do + Time.use_zone('Hawaii') do + date = Date.new(2014, 2, 18) + time = date.midnight + + travel_to date do + assert_equal date, Date.current + assert_equal time, Time.current + end + end + end + end + + def test_travel_to_travels_back_and_reraises_if_the_block_raises + ts = Time.current - 1.second + + travel_to ts do + raise + end + + flunk # ensure travel_to re-raises + rescue + assert_not_equal ts, Time.current + end + def test_local time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time |