Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
4d82e2aa
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
4d82e2aa
编写于
7月 27, 2017
作者:
A
Andrew White
提交者:
Kasper Timm Hansen
7月 27, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #29971 from rails/fix-duration-division
Fix division where a duration is the denominator
上级
af5ec3db
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
28 addition
and
15 deletion
+28
-15
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+10
-0
activesupport/lib/active_support/duration.rb
activesupport/lib/active_support/duration.rb
+4
-5
activesupport/test/core_ext/duration_test.rb
activesupport/test/core_ext/duration_test.rb
+14
-10
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
4d82e2aa
*
Fix division where a duration is the denominator
PR #29163 introduced a change in behavior when a duration was the denominator
in a calculation - this was incorrect as dividing by a duration should always
return a `Numeric`. The behavior of previous versions of Rails has been restored.
Fixes #29592.
*Andrew White*
## Rails 5.1.3.rc1 (July 19, 2017) ##
*
No changes.
...
...
activesupport/lib/active_support/duration.rb
浏览文件 @
4d82e2aa
...
...
@@ -74,10 +74,7 @@ def *(other)
def
/
(
other
)
if
Duration
===
other
new_parts
=
other
.
parts
.
map
{
|
part
,
other_value
|
[
part
,
value
/
other_value
]
}.
to_h
new_value
=
new_parts
.
inject
(
0
)
{
|
total
,
(
part
,
value
)
|
total
+
value
*
Duration
::
PARTS_IN_SECONDS
[
part
]
}
Duration
.
new
(
new_value
,
new_parts
)
value
/
other
.
value
else
calculate
(
:/
,
other
)
end
...
...
@@ -232,8 +229,10 @@ def *(other)
# Divides this Duration by a Numeric and returns a new Duration.
def
/
(
other
)
if
Scalar
===
other
||
Duration
===
other
if
Scalar
===
other
Duration
.
new
(
value
/
other
.
value
,
parts
.
map
{
|
type
,
number
|
[
type
,
number
/
other
.
value
]
})
elsif
Duration
===
other
value
/
other
.
value
elsif
Numeric
===
other
Duration
.
new
(
value
/
other
,
parts
.
map
{
|
type
,
number
|
[
type
,
number
/
other
]
})
else
...
...
activesupport/test/core_ext/duration_test.rb
浏览文件 @
4d82e2aa
...
...
@@ -408,9 +408,11 @@ def test_scalar_divide
assert_equal
5
,
scalar
/
2
assert_instance_of
ActiveSupport
::
Duration
::
Scalar
,
scalar
/
2
assert_equal
10
,
100
.
seconds
/
scalar
assert_instance_of
ActiveSupport
::
Duration
,
100
.
seconds
/
scalar
assert_equal
20
,
2
.
seconds
*
scalar
assert_instance_of
ActiveSupport
::
Duration
,
2
.
seconds
*
scalar
assert_equal
5
,
scalar
/
2
.
seconds
assert_
instance_of
ActiveSupport
::
Duration
,
scalar
/
2
.
seconds
assert_
kind_of
Integer
,
scalar
/
2
.
seconds
exception
=
assert_raises
(
TypeError
)
do
scalar
/
"foo"
...
...
@@ -419,15 +421,6 @@ def test_scalar_divide
assert_equal
"no implicit conversion of String into ActiveSupport::Duration::Scalar"
,
exception
.
message
end
def
test_scalar_divide_parts
scalar
=
ActiveSupport
::
Duration
::
Scalar
.
new
(
10
)
assert_equal
({
days:
2
},
(
scalar
/
5
.
days
).
parts
)
assert_equal
(
172800
,
(
scalar
/
5
.
days
).
value
)
assert_equal
({
days:
-
2
},
(
scalar
/
-
5
.
days
).
parts
)
assert_equal
(
-
172800
,
(
scalar
/
-
5
.
days
).
value
)
end
def
test_twelve_months_equals_one_year
assert_equal
12
.
months
,
1
.
year
end
...
...
@@ -436,6 +429,17 @@ def test_thirty_days_does_not_equal_one_month
assert_not_equal
30
.
days
,
1
.
month
end
def
test_division
assert_equal
1
.
hour
,
1
.
day
/
24
assert_instance_of
ActiveSupport
::
Duration
,
1
.
day
/
24
assert_equal
24
,
86400
/
1
.
hour
assert_kind_of
Integer
,
86400
/
1
.
hour
assert_equal
24
,
1
.
day
/
1
.
hour
assert_kind_of
Integer
,
1
.
day
/
1
.
hour
end
def
test_adding_one_month_maintains_day_of_month
(
1
..
11
).
each
do
|
month
|
[
1
,
14
,
28
].
each
do
|
day
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录