Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
dcc1cae5
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,发现更多精彩内容 >>
提交
dcc1cae5
编写于
6月 27, 2017
作者:
R
Rafael França
提交者:
GitHub
6月 27, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29594 from ahazem/fix-regression-with-date-helper
Fix regression with date helper in 5.1.2
上级
250cf573
2c97fbf6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
18 deletion
+41
-18
actionview/CHANGELOG.md
actionview/CHANGELOG.md
+5
-0
actionview/lib/action_view/helpers/date_helper.rb
actionview/lib/action_view/helpers/date_helper.rb
+26
-18
actionview/test/template/date_helper_test.rb
actionview/test/template/date_helper_test.rb
+10
-0
未找到文件。
actionview/CHANGELOG.md
浏览文件 @
dcc1cae5
...
@@ -14,6 +14,11 @@
...
@@ -14,6 +14,11 @@
*Yuji Yaginuma*
*Yuji Yaginuma*
*
Update distance_of_time_in_words helper to display better error messages
for bad input.
*Jay Hayes*
## Rails 5.1.1 (May 12, 2017) ##
## Rails 5.1.1 (May 12, 2017) ##
...
...
actionview/lib/action_view/helpers/date_helper.rb
浏览文件 @
dcc1cae5
...
@@ -95,8 +95,8 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
...
@@ -95,8 +95,8 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
scope: :'datetime.distance_in_words'
scope: :'datetime.distance_in_words'
}.
merge!
(
options
)
}.
merge!
(
options
)
from_time
=
from_time
.
to_time
if
from_time
.
respond_to?
(
:to
_time
)
from_time
=
normalize_distance_of_time_argument_to_time
(
from
_time
)
to_time
=
to_time
.
to_time
if
to_time
.
respond_to?
(
:
to_time
)
to_time
=
normalize_distance_of_time_argument_to_time
(
to_time
)
from_time
,
to_time
=
to_time
,
from_time
if
from_time
>
to_time
from_time
,
to_time
=
to_time
,
from_time
if
from_time
>
to_time
distance_in_minutes
=
((
to_time
-
from_time
)
/
60.0
).
round
distance_in_minutes
=
((
to_time
-
from_time
)
/
60.0
).
round
distance_in_seconds
=
(
to_time
-
from_time
).
round
distance_in_seconds
=
(
to_time
-
from_time
).
round
...
@@ -130,22 +130,18 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
...
@@ -130,22 +130,18 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
# 60 days up to 365 days
# 60 days up to 365 days
when
86400
...
525600
then
locale
.
t
:x_months
,
count:
(
distance_in_minutes
.
to_f
/
43200.0
).
round
when
86400
...
525600
then
locale
.
t
:x_months
,
count:
(
distance_in_minutes
.
to_f
/
43200.0
).
round
else
else
if
from_time
.
acts_like?
(
:time
)
&&
to_time
.
acts_like?
(
:time
)
from_year
=
from_time
.
year
fyear
=
from_time
.
year
from_year
+=
1
if
from_time
.
month
>=
3
fyear
+=
1
if
from_time
.
month
>=
3
to_year
=
to_time
.
year
tyear
=
to_time
.
year
to_year
-=
1
if
to_time
.
month
<
3
tyear
-=
1
if
to_time
.
month
<
3
leap_years
=
(
from_year
>
to_year
)
?
0
:
(
from_year
..
to_year
).
count
{
|
x
|
Date
.
leap?
(
x
)
}
leap_years
=
(
fyear
>
tyear
)
?
0
:
(
fyear
..
tyear
).
count
{
|
x
|
Date
.
leap?
(
x
)
}
minute_offset_for_leap_year
=
leap_years
*
1440
minute_offset_for_leap_year
=
leap_years
*
1440
# Discount the leap year days when calculating year distance.
# Discount the leap year days when calculating year distance.
# e.g. if there are 20 leap year days between 2 dates having the same day
# e.g. if there are 20 leap year days between 2 dates having the same day
# and month then the based on 365 days calculation
# and month then the based on 365 days calculation
# the distance in years will come out to over 80 years when in written
# the distance in years will come out to over 80 years when in written
# English it would read better as about 80 years.
# English it would read better as about 80 years.
minutes_with_offset
=
distance_in_minutes
-
minute_offset_for_leap_year
minutes_with_offset
=
distance_in_minutes
-
minute_offset_for_leap_year
else
minutes_with_offset
=
distance_in_minutes
end
remainder
=
(
minutes_with_offset
%
MINUTES_IN_YEAR
)
remainder
=
(
minutes_with_offset
%
MINUTES_IN_YEAR
)
distance_in_years
=
(
minutes_with_offset
.
div
MINUTES_IN_YEAR
)
distance_in_years
=
(
minutes_with_offset
.
div
MINUTES_IN_YEAR
)
if
remainder
<
MINUTES_IN_QUARTER_YEAR
if
remainder
<
MINUTES_IN_QUARTER_YEAR
...
@@ -687,6 +683,18 @@ def time_tag(date_or_time, *args, &block)
...
@@ -687,6 +683,18 @@ def time_tag(date_or_time, *args, &block)
content_tag
(
"time"
.
freeze
,
content
,
options
.
reverse_merge
(
datetime:
datetime
),
&
block
)
content_tag
(
"time"
.
freeze
,
content
,
options
.
reverse_merge
(
datetime:
datetime
),
&
block
)
end
end
private
def
normalize_distance_of_time_argument_to_time
(
value
)
if
value
.
is_a?
(
Numeric
)
Time
.
at
(
value
)
elsif
value
.
respond_to?
(
:to_time
)
value
.
to_time
else
raise
ArgumentError
,
"
#{
value
.
inspect
}
can't be converted to a Time value"
end
end
end
end
class
DateTimeSelector
#:nodoc:
class
DateTimeSelector
#:nodoc:
...
...
actionview/test/template/date_helper_test.rb
浏览文件 @
dcc1cae5
...
@@ -143,6 +143,16 @@ def test_distance_in_words_doesnt_use_the_quotient_operator
...
@@ -143,6 +143,16 @@ def test_distance_in_words_doesnt_use_the_quotient_operator
klass
.
send
:public
,
:/
klass
.
send
:public
,
:/
end
end
def
test_distance_in_words_with_nil_input
assert_raises
(
ArgumentError
)
{
distance_of_time_in_words
(
nil
)
}
assert_raises
(
ArgumentError
)
{
distance_of_time_in_words
(
0
,
nil
)
}
end
def
test_distance_in_words_with_mixed_argument_types
assert_equal
"1 minute"
,
distance_of_time_in_words
(
0
,
Time
.
at
(
60
))
assert_equal
"10 minutes"
,
distance_of_time_in_words
(
Time
.
at
(
600
),
0
)
end
def
test_time_ago_in_words_passes_include_seconds
def
test_time_ago_in_words_passes_include_seconds
assert_equal
"less than 20 seconds"
,
time_ago_in_words
(
15
.
seconds
.
ago
,
include_seconds:
true
)
assert_equal
"less than 20 seconds"
,
time_ago_in_words
(
15
.
seconds
.
ago
,
include_seconds:
true
)
assert_equal
"less than a minute"
,
time_ago_in_words
(
15
.
seconds
.
ago
,
include_seconds:
false
)
assert_equal
"less than a minute"
,
time_ago_in_words
(
15
.
seconds
.
ago
,
include_seconds:
false
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录