Skip to content
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

chancancode
Copy link
Member

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

@@ -1,3 +1,7 @@
* Deprecated Numeric#{ago,until,since,from_now}, use 5.seconds.ago instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request description is so clear and understandable, but this just describes the diff. Think of the Rails dev reviewing the CHANGELOG to see what's changed and why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 😁

@@ -63,7 +63,8 @@ def fortnights

# Reads best without arguments: 10.minutes.ago
def ago(time = ::Time.current)
time - self
ActiveSupport::Deprecation.warn "Numeric#ago (#{self.to_s}.ago) is deprecated and will be removed in the future, use #{self.to_s}.seconds.ago instead"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to say Numeric#ago (2013-09-27 14:36:52 -0700.ago) is deprecated ... which doesn't make sense. Consider something like: "Calling the #ago method on a number, like 5.ago, is deprecated in favor of calling it on a duration, like 5.seconds.ago."

@chancancode
Copy link
Member Author

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

@@ -63,6 +63,7 @@ def fortnights

# Reads best without arguments: 10.minutes.ago
def ago(time = ::Time.current)
ActiveSupport::Deprecation.warn "Calling #ago or #until on a nunmber (e.g. 5.ago) is deprecated and will be removed in the future, use 5.seconds.ago instead"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nunmber -> number

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
Copy link
Member Author

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

@jeremy
Copy link
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
chancancode added a commit that referenced this pull request May 30, 2014
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
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants