New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated Numeric#{ago,until,since,from_now} #12389

Merged
merged 1 commit into from Nov 27, 2013

Conversation

Projects
None yet
3 participants
@chancancode
Member

chancancode commented Sep 27, 2013

The user is expected to explicitly convert the value into an AS::Duration, i.e. 5.ago => 5.seconds.ago

This will help to catch subtle bugs like:

def recent?(days = 3)
  self.created_at >= days.ago
end

The above code would check if the model is created within the last 3 seconds.

In the future, Numeric#{ago,until,since,from_now} should be removed completely, or throw some sort of errors to indicate there are no implicit conversion from Numeric to AS::Duration.

/cc @jeremy @sikachu

@jeremy

View changes

Show outdated Hide outdated activesupport/CHANGELOG.md
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@sikachu

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/lib/active_support/core_ext/numeric/time.rb
def test_intervals
@seconds.values.each do |seconds|
assert_equal seconds.since(@now), @now + seconds
assert_equal seconds.until(@now), @now - seconds

This comment has been minimized.

@jeremy

jeremy Sep 27, 2013

Member

Time#+ and Time#- should not be deprecated, since they're plain Ruby. This tests that + and - behave correctly; it's not testing since/ago. Can fix by doing: assert_equal seconds.seconds.since(@now), @now + seconds

@jeremy

jeremy Sep 27, 2013

Member

Time#+ and Time#- should not be deprecated, since they're plain Ruby. This tests that + and - behave correctly; it's not testing since/ago. Can fix by doing: assert_equal seconds.seconds.since(@now), @now + seconds

This comment has been minimized.

@chancancode

chancancode Nov 6, 2013

Member

As discussed on campfire, the original test was wrong (arguments were flipped)

@chancancode

chancancode Nov 6, 2013

Member

As discussed on campfire, the original test was wrong (arguments were flipped)

@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
assert_equal 30.days.to_i.seconds.since(@now), 1.month.to_i.seconds.since(@now)
assert_equal 365.25.days.to_f.seconds.since(@now), 1.year.to_f.seconds.since(@now)
assert_equal 30.days.to_i.seconds.since(@dtnow), 1.month.to_i.seconds.since(@dtnow)
assert_equal 365.25.days.to_f.seconds.since(@dtnow), 1.year.to_f.seconds.since(@dtnow)

This comment has been minimized.

@jeremy

jeremy Sep 27, 2013

Member

👍

@jeremy
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@chancancode

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Sep 27, 2013

Member

@jeremy I trimmed this down to an MVP / Minimal Viable Pull-request. Doubts-- ? 😁

Member

chancancode commented Sep 27, 2013

@jeremy I trimmed this down to an MVP / Minimal Viable Pull-request. Doubts-- ? 😁

@jeremy

View changes

Show outdated Hide outdated activesupport/lib/active_support/core_ext/numeric/time.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/lib/active_support/core_ext/numeric/time.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
@jeremy

View changes

Show outdated Hide outdated activesupport/test/core_ext/numeric_ext_test.rb
now = Time.now
assert assert_deprecated { 1.since } >= now + 1
now = Time.now
assert assert_deprecated { 1.ago } >= now - 1
end

This comment has been minimized.

@chancancode

chancancode Nov 6, 2013

Member

ditto

@chancancode
Deprecated Numeric#{ago,until,since,from_now}
The user is expected to explicitly convert the value into an
AS::Duration, i.e. `5.ago` => `5.seconds.ago`

This will help to catch subtle bugs like:

  def recent?(days = 3)
    self.created_at >= days.ago
  end

The above code would check if the model is created within the last 3
**seconds**.

In the future, `Numeric#{ago,until,since,from_now}` should be removed
completely, or throw some sort of errors to indicate there are no
implicit conversion from `Numeric` to `AS::Duration`.

Also fixed & refactor the test cases for Numeric#{ago,since} and
AS::Duration#{ago,since}. The original test case had the assertion
flipped and the purpose of the test wasn't very clear.
@chancancode

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Nov 27, 2013

Member

Rebased to keep it fresh :) Probably would have to do it again when someone is ready to merge this, stupid CHANGELOGs

Member

chancancode commented Nov 27, 2013

Rebased to keep it fresh :) Probably would have to do it again when someone is ready to merge this, stupid CHANGELOGs

@jeremy

This comment has been minimized.

Show comment
Hide comment
@jeremy

jeremy Nov 27, 2013

Member

Merged d4016f2

Member

jeremy commented Nov 27, 2013

Merged d4016f2

@jeremy jeremy closed this Nov 27, 2013

@jeremy jeremy merged commit 1f16136 into rails:master Nov 27, 2013

1 check passed

default The Travis CI build passed
Details

chancancode added a commit that referenced this pull request May 30, 2014

✂️ removed deprecated `Numeric#ago` and friends
Replacements:

   5.ago   => 5.seconds.ago
   5.until => 5.seconds.until
   5.since => 5.seconds.since
   5.from_now => 5.seconds.from_now

The removed tests does not affect coverage – we have equivalent test cases in
the tests for `AS::Duration`.

See #12389 for the history and rationale behind this.

jrafanie added a commit to tenderlove/manageiq that referenced this pull request Jan 8, 2015

Numeric#from_now/ago is removed, convert to AS::Duration first.
Fixes: undefined method `from_now' for 600:Fixnum

See also: rails/rails#12389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment