Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d126c0d6
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,发现更多精彩内容 >>
提交
d126c0d6
编写于
7月 08, 2017
作者:
G
Gannon McGibbon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix numericality equality validation on floats
上级
0fc6804c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
34 addition
and
10 deletion
+34
-10
activemodel/CHANGELOG.md
activemodel/CHANGELOG.md
+5
-0
activemodel/lib/active_model/validations/numericality.rb
activemodel/lib/active_model/validations/numericality.rb
+22
-10
activemodel/test/cases/validations/numericality_validation_test.rb
...el/test/cases/validations/numericality_validation_test.rb
+7
-0
未找到文件。
activemodel/CHANGELOG.md
浏览文件 @
d126c0d6
*
Fix numericality equality validation of
`BigDecimal`
and
`Float`
by casting to
`BigDecimal`
on both ends of the validation.
*Gannon McGibbon*
*
Add
`#slice!`
method to
`ActiveModel::Errors`
.
*Daniel López Prat*
...
...
activemodel/lib/active_model/validations/numericality.rb
浏览文件 @
d126c0d6
...
...
@@ -9,6 +9,9 @@ class NumericalityValidator < EachValidator # :nodoc:
RESERVED_OPTIONS
=
CHECKS
.
keys
+
[
:only_integer
]
INTEGER_REGEX
=
/\A[+-]?\d+\z/
DECIMAL_REGEX
=
/\A[+-]?\d+\.?\d*(e|e[+-])?\d+\z/
def
check_validity!
keys
=
CHECKS
.
keys
-
[
:odd
,
:even
]
options
.
slice
(
*
keys
).
each
do
|
option
,
value
|
...
...
@@ -49,11 +52,7 @@ def validate_each(record, attr_name, value)
return
end
if
raw_value
.
is_a?
(
Numeric
)
value
=
raw_value
else
value
=
parse_raw_value_as_a_number
(
raw_value
)
end
value
=
parse_as_number
(
raw_value
)
options
.
slice
(
*
CHECKS
.
keys
).
each
do
|
option
,
option_value
|
case
option
...
...
@@ -69,6 +68,8 @@ def validate_each(record, attr_name, value)
option_value
=
record
.
send
(
option_value
)
end
option_value
=
parse_as_number
(
option_value
)
unless
value
.
send
(
CHECKS
[
option
],
option_value
)
record
.
errors
.
add
(
attr_name
,
option
,
filtered_options
(
value
).
merge!
(
count:
option_value
))
end
...
...
@@ -79,18 +80,29 @@ def validate_each(record, attr_name, value)
private
def
is_number?
(
raw_value
)
!
parse_
raw_value_as_a
_number
(
raw_value
).
nil?
!
parse_
as
_number
(
raw_value
).
nil?
rescue
ArgumentError
,
TypeError
false
end
def
parse_raw_value_as_a_number
(
raw_value
)
return
raw_value
.
to_i
if
is_integer?
(
raw_value
)
Kernel
.
Float
(
raw_value
)
unless
is_hexadecimal_literal?
(
raw_value
)
def
parse_as_number
(
raw_value
)
if
raw_value
.
is_a?
(
Float
)
raw_value
.
to_d
elsif
raw_value
.
is_a?
(
Numeric
)
raw_value
elsif
is_integer?
(
raw_value
)
raw_value
.
to_i
elsif
is_decimal?
(
raw_value
)
&&
!
is_hexadecimal_literal?
(
raw_value
)
BigDecimal
(
raw_value
)
end
end
def
is_integer?
(
raw_value
)
/\A[+-]?\d+\z/
===
raw_value
.
to_s
INTEGER_REGEX
===
raw_value
.
to_s
end
def
is_decimal?
(
raw_value
)
DECIMAL_REGEX
===
raw_value
.
to_s
end
def
is_hexadecimal_literal?
(
raw_value
)
...
...
activemodel/test/cases/validations/numericality_validation_test.rb
浏览文件 @
d126c0d6
...
...
@@ -289,6 +289,13 @@ def test_validates_numericality_with_invalid_args
assert_raise
(
ArgumentError
)
{
Topic
.
validates_numericality_of
:approved
,
equal_to:
"foo"
}
end
def
test_validates_numericality_equality_for_float_and_big_decimal
Topic
.
validates_numericality_of
:approved
,
equal_to:
BigDecimal
(
"65.6"
)
invalid!
([
Float
(
"65.5"
),
BigDecimal
(
"65.7"
)],
"must be equal to 65.6"
)
valid!
([
Float
(
"65.6"
),
BigDecimal
(
"65.6"
)])
end
private
def
invalid!
(
values
,
error
=
nil
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录