Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c2b02d00
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,发现更多精彩内容 >>
提交
c2b02d00
编写于
6月 24, 2014
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15895 from sgrif/sg-numeric-changes
Always assume strings with non-numeric characters change numeric types
上级
1a299f1d
50fa3667
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
3 deletion
+35
-3
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+14
-0
activerecord/lib/active_record/type/numeric.rb
activerecord/lib/active_record/type/numeric.rb
+3
-3
activerecord/test/cases/types_test.rb
activerecord/test/cases/types_test.rb
+18
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
c2b02d00
*
Assume numeric types have changed if they were assigned to a value that
would fail numericality validation, regardless of the old value. Previously
this would only occur if the old value was 0.
Example:
model = Model.create!(number: 5)
model.number = '5wibble'
model.number_changed? # => true
Fixes #14731.
*Sean Griffin*
*
`reload`
no longer merges with the existing attributes.
The attribute hash is fully replaced. The record is put into the same state
as it would be with
`Model.find(model.id)`
.
...
...
activerecord/lib/active_record/type/numeric.rb
浏览文件 @
c2b02d00
...
...
@@ -16,13 +16,13 @@ def type_cast(value)
end
def
changed?
(
old_value
,
_new_value
,
new_value_before_type_cast
)
# :nodoc:
super
||
zero
_to_non_number?
(
old_value
,
new_value_before_type_cast
)
super
||
number
_to_non_number?
(
old_value
,
new_value_before_type_cast
)
end
private
def
zero
_to_non_number?
(
old_value
,
new_value_before_type_cast
)
old_value
==
0
&&
non_numeric_string?
(
new_value_before_type_cast
)
def
number
_to_non_number?
(
old_value
,
new_value_before_type_cast
)
old_value
!=
nil
&&
non_numeric_string?
(
new_value_before_type_cast
)
end
def
non_numeric_string?
(
value
)
...
...
activerecord/test/cases/types_test.rb
浏览文件 @
c2b02d00
...
...
@@ -79,11 +79,29 @@ def test_type_cast_nan_and_infinity_to_integer
assert_nil
type
.
type_cast_from_user
(
1.0
/
0.0
)
end
def
test_changing_integers
type
=
Type
::
Integer
.
new
assert
type
.
changed?
(
5
,
5
,
'5wibble'
)
assert_not
type
.
changed?
(
5
,
5
,
'5'
)
assert_not
type
.
changed?
(
5
,
5
,
'5.0'
)
assert_not
type
.
changed?
(
nil
,
nil
,
nil
)
end
def
test_type_cast_float
type
=
Type
::
Float
.
new
assert_equal
1.0
,
type
.
type_cast_from_user
(
"1"
)
end
def
test_changing_float
type
=
Type
::
Float
.
new
assert
type
.
changed?
(
5.0
,
5.0
,
'5wibble'
)
assert_not
type
.
changed?
(
5.0
,
5.0
,
'5'
)
assert_not
type
.
changed?
(
5.0
,
5.0
,
'5.0'
)
assert_not
type
.
changed?
(
nil
,
nil
,
nil
)
end
def
test_type_cast_decimal
type
=
Type
::
Decimal
.
new
assert_equal
BigDecimal
.
new
(
"0"
),
type
.
type_cast_from_user
(
BigDecimal
.
new
(
"0"
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录