Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
42be84ba
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,发现更多精彩内容 >>
提交
42be84ba
编写于
7月 06, 2014
作者:
D
Dylan Thacker-Smith
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
active_record: Type cast booleans and durations for string columns.
上级
93e24dea
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
49 addition
and
5 deletion
+49
-5
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+11
-0
activerecord/lib/active_record/type/binary.rb
activerecord/lib/active_record/type/binary.rb
+1
-1
activerecord/lib/active_record/type/string.rb
activerecord/lib/active_record/type/string.rb
+3
-1
activerecord/test/cases/adapters/mysql2/boolean_test.rb
activerecord/test/cases/adapters/mysql2/boolean_test.rb
+2
-2
activerecord/test/cases/relation/where_test.rb
activerecord/test/cases/relation/where_test.rb
+32
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
42be84ba
*
Avoid type casting boolean and ActiveSupport::Duration values to numeric
values for string columns. Otherwise, in some database, the string column
values will be coerced to a numeric allowing false or 0.seconds match any
string starting with a non-digit.
Example:
App.where(apikey: false) # => SELECT * FROM users WHERE apikey = '0'
*Dylan Thacker-Smith*
*
Add a
`:required`
option to singular associations, providing a nicer
API for presence validations on associations.
...
...
activerecord/lib/active_record/type/binary.rb
浏览文件 @
42be84ba
...
...
@@ -24,7 +24,7 @@ def type_cast_for_database(value)
class
Data
# :nodoc:
def
initialize
(
value
)
@value
=
value
@value
=
value
.
to_s
end
def
to_s
...
...
activerecord/lib/active_record/type/string.rb
浏览文件 @
42be84ba
...
...
@@ -17,8 +17,10 @@ def changed_in_place?(raw_old_value, new_value)
def
type_cast_for_database
(
value
)
case
value
when
::
Numeric
then
value
.
to_s
when
::
Numeric
,
ActiveSupport
::
Duration
then
value
.
to_s
when
::
String
then
::
String
.
new
(
value
)
when
true
then
"1"
when
false
then
"0"
else
super
end
end
...
...
activerecord/test/cases/adapters/mysql2/boolean_test.rb
浏览文件 @
42be84ba
...
...
@@ -47,7 +47,7 @@ class BooleanType < ActiveRecord::Base
assert_equal
"1"
,
attributes
[
"published"
]
assert_equal
1
,
@connection
.
type_cast
(
true
,
boolean_column
)
assert_equal
1
,
@connection
.
type_cast
(
true
,
string_column
)
assert_equal
"1"
,
@connection
.
type_cast
(
true
,
string_column
)
end
test
"test type casting without emulated booleans"
do
...
...
@@ -60,7 +60,7 @@ class BooleanType < ActiveRecord::Base
assert_equal
"1"
,
attributes
[
"published"
]
assert_equal
1
,
@connection
.
type_cast
(
true
,
boolean_column
)
assert_equal
1
,
@connection
.
type_cast
(
true
,
string_column
)
assert_equal
"1"
,
@connection
.
type_cast
(
true
,
string_column
)
end
test
"with booleans stored as 1 and 0"
do
...
...
activerecord/test/cases/relation/where_test.rb
浏览文件 @
42be84ba
...
...
@@ -6,10 +6,11 @@
require
'models/comment'
require
'models/edge'
require
'models/topic'
require
'models/binary'
module
ActiveRecord
class
WhereTest
<
ActiveRecord
::
TestCase
fixtures
:posts
,
:edges
,
:authors
fixtures
:posts
,
:edges
,
:authors
,
:binaries
def
test_where_copies_bind_params
author
=
authors
(
:david
)
...
...
@@ -179,5 +180,35 @@ def test_where_with_blank_conditions
assert_equal
4
,
Edge
.
where
(
blank
).
order
(
"sink_id"
).
to_a
.
size
end
end
def
test_where_with_integer_for_string_column
count
=
Post
.
where
(
:title
=>
0
).
count
assert_equal
0
,
count
end
def
test_where_with_float_for_string_column
count
=
Post
.
where
(
:title
=>
0.0
).
count
assert_equal
0
,
count
end
def
test_where_with_boolean_for_string_column
count
=
Post
.
where
(
:title
=>
false
).
count
assert_equal
0
,
count
end
def
test_where_with_decimal_for_string_column
count
=
Post
.
where
(
:title
=>
BigDecimal
.
new
(
0
)).
count
assert_equal
0
,
count
end
def
test_where_with_duration_for_string_column
count
=
Post
.
where
(
:title
=>
0
.
seconds
).
count
assert_equal
0
,
count
end
def
test_where_with_integer_for_binary_column
count
=
Binary
.
where
(
:data
=>
0
).
count
assert_equal
0
,
count
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录