mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #37849 from kamipo/fix_since_and_ago
Fix `since` and `ago` with a duration which has empty parts
This commit is contained in:
commit
b50fed1bb0
2 changed files with 10 additions and 4 deletions
|
@ -403,8 +403,14 @@ module ActiveSupport
|
|||
|
||||
private
|
||||
def sum(sign, time = ::Time.current)
|
||||
parts.inject(time) do |t, (type, number)|
|
||||
if t.acts_like?(:time) || t.acts_like?(:date)
|
||||
unless time.acts_like?(:time) || time.acts_like?(:date)
|
||||
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
|
||||
end
|
||||
|
||||
if parts.empty?
|
||||
time.since(sign * value)
|
||||
else
|
||||
parts.inject(time) do |t, (type, number)|
|
||||
if type == :seconds
|
||||
t.since(sign * number)
|
||||
elsif type == :minutes
|
||||
|
@ -414,8 +420,6 @@ module ActiveSupport
|
|||
else
|
||||
t.advance(type => sign * number)
|
||||
end
|
||||
else
|
||||
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -204,7 +204,9 @@ class DurationTest < ActiveSupport::TestCase
|
|||
def test_since_and_ago
|
||||
t = Time.local(2000)
|
||||
assert_equal t + 1, 1.second.since(t)
|
||||
assert_equal t + 1, (1.minute / 60).since(t)
|
||||
assert_equal t - 1, 1.second.ago(t)
|
||||
assert_equal t - 1, (1.minute / 60).ago(t)
|
||||
end
|
||||
|
||||
def test_since_and_ago_without_argument
|
||||
|
|
Loading…
Reference in a new issue