From e842db501f5240fd547e904f173ad8d7b1304f8c Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 26 Feb 2017 10:19:48 +0530 Subject: AS CHANGELOG Pass [ci skip] --- activesupport/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 6dc301e5b4..2f08d8bc82 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -5,7 +5,7 @@ * In Core Extensions, make `MarshalWithAutoloading#load` pass through the second, optional argument for `Marshal#load( source [, proc] )`. This way we don't have to do - `Marshal.method(:load).super_method.call(sourse, proc)` just to be able to pass a proc. + `Marshal.method(:load).super_method.call(source, proc)` just to be able to pass a proc. *Jeff Latz* @@ -20,7 +20,7 @@ *Adam Rice* -* Deprecate `.halt_callback_chains_on_return_false`. +* Deprecate `ActiveSupport.halt_callback_chains_on_return_false`. *Rafael Mendonça França* @@ -83,7 +83,7 @@ duration's numeric value isn't used in calculations, only parts are used. Methods on `Numeric` like `2.days` now use these predefined durations - to avoid duplicating of duration constants through the codebase and + to avoid duplication of duration constants through the codebase and eliminate creation of intermediate durations. *Andrey Novikov, Andrew White* -- cgit v1.2.3 From 2d84a6bc74fbe9d9b97bf7d63ec6f27418d9c3a6 Mon Sep 17 00:00:00 2001 From: Nick Johnstone Date: Wed, 22 Feb 2017 20:45:48 +1300 Subject: Add Duration#before and #after as aliases for #ago and #since It's common in test cases at my job to have code like this: let(:today) { customer_start_date + 2.weeks } let(:earlier_date) { today - 5.days } With this change, we can instead write let(:today) { 2.weeks.after(customer_start_date) } let(:earlier_date) { 5.days.before(today) } Closes #27721 --- activesupport/CHANGELOG.md | 16 ++++++++++++++++ activesupport/lib/active_support/duration.rb | 2 ++ activesupport/test/core_ext/duration_test.rb | 13 +++++++++++++ 3 files changed, 31 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 2f08d8bc82..0614cdaabd 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,19 @@ +* Add `ActiveSupport::Duration#before` and `#after` as aliases for `#until` and `#since` + + These read more like English and require less mental gymnastics to read and write. + + Before: + + 2.weeks.since(customer_start_date) + 5.days.until(today) + + After: + + 2.weeks.after(customer_start_date) + 5.days.before(today) + + *Nick Johnstone* + * Soft-deprecated the top-level `HashWithIndifferentAccess` constant. `ActiveSupport::HashWithIndifferentAccess` should be used instead. diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 003f6203ef..70cf78519d 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -180,6 +180,7 @@ module ActiveSupport sum(1, time) end alias :from_now :since + alias :after :since # Calculates a new Time or Date that is as far in the past # as this Duration represents. @@ -187,6 +188,7 @@ module ActiveSupport sum(-1, time) end alias :until :ago + alias :before :ago def inspect #:nodoc: parts. diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 6a275d1d5b..6facb04f1f 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -179,6 +179,19 @@ class DurationTest < ActiveSupport::TestCase Time.zone = nil end + def test_before_and_afer + t = Time.local(2000) + assert_equal t + 1, 1.second.after(t) + assert_equal t - 1, 1.second.before(t) + end + + def test_before_and_after_without_argument + Time.stub(:now, Time.local(2000)) do + assert_equal Time.now - 1.second, 1.second.before + assert_equal Time.now + 1.second, 1.second.after + end + end + def test_adding_hours_across_dst_boundary with_env_tz "CET" do assert_equal Time.local(2009, 3, 29, 0, 0, 0) + 24.hours, Time.local(2009, 3, 30, 1, 0, 0) -- cgit v1.2.3