Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
439145c9
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 搜索 >>
未验证
提交
439145c9
编写于
5月 21, 2019
作者:
R
Ryuta Kamizono
提交者:
GitHub
5月 21, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #36314 from kamipo/fallback_type_casting
Fall back to type casting from the connection adapter
上级
0810c076
82a54be0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
28 addition
and
21 deletion
+28
-21
activerecord/lib/active_record/table_metadata.rb
activerecord/lib/active_record/table_metadata.rb
+6
-10
activerecord/lib/active_record/type_caster/connection.rb
activerecord/lib/active_record/type_caster/connection.rb
+16
-10
activerecord/test/cases/relation/where_test.rb
activerecord/test/cases/relation/where_test.rb
+6
-1
未找到文件。
activerecord/lib/active_record/table_metadata.rb
浏览文件 @
439145c9
...
...
@@ -4,8 +4,9 @@ module ActiveRecord
class
TableMetadata
# :nodoc:
delegate
:foreign_type
,
:foreign_key
,
:join_primary_key
,
:join_foreign_key
,
to: :association
,
prefix:
true
def
initialize
(
klass
,
arel_table
,
association
=
nil
)
def
initialize
(
klass
,
arel_table
,
association
=
nil
,
types
=
klass
)
@klass
=
klass
@types
=
types
@arel_table
=
arel_table
@association
=
association
end
...
...
@@ -29,11 +30,7 @@ def arel_attribute(column_name)
end
def
type
(
column_name
)
if
klass
klass
.
type_for_attribute
(
column_name
)
else
Type
.
default_value
end
types
.
type_for_attribute
(
column_name
)
end
def
has_column?
(
column_name
)
...
...
@@ -52,13 +49,12 @@ def associated_table(table_name)
elsif
association
&&
!
association
.
polymorphic?
association_klass
=
association
.
klass
arel_table
=
association_klass
.
arel_table
.
alias
(
table_name
)
TableMetadata
.
new
(
association_klass
,
arel_table
,
association
)
else
type_caster
=
TypeCaster
::
Connection
.
new
(
klass
,
table_name
)
association_klass
=
nil
arel_table
=
Arel
::
Table
.
new
(
table_name
,
type_caster:
type_caster
)
TableMetadata
.
new
(
nil
,
arel_table
,
association
,
type_caster
)
end
TableMetadata
.
new
(
association_klass
,
arel_table
,
association
)
end
def
polymorphic_association?
...
...
@@ -74,6 +70,6 @@ def reflect_on_aggregation(aggregation_name)
end
private
attr_reader
:klass
,
:arel_table
,
:association
attr_reader
:klass
,
:
types
,
:
arel_table
,
:association
end
end
activerecord/lib/active_record/type_caster/connection.rb
浏览文件 @
439145c9
...
...
@@ -8,21 +8,27 @@ def initialize(klass, table_name)
@table_name
=
table_name
end
def
type_cast_for_database
(
attr
ibute
_name
,
value
)
def
type_cast_for_database
(
attr_name
,
value
)
return
value
if
value
.
is_a?
(
Arel
::
Nodes
::
BindParam
)
column
=
column_for
(
attribute
_name
)
connection
.
type_cast_from_column
(
column
,
value
)
type
=
type_for_attribute
(
attr
_name
)
type
.
serialize
(
value
)
end
private
attr_reader
:table_name
delegate
:connection
,
to: :@klass
def
type_for_attribute
(
attr_name
)
schema_cache
=
connection
.
schema_cache
def
column_for
(
attribute_name
)
if
connection
.
schema_cache
.
data_source_exists?
(
table_name
)
connection
.
schema_cache
.
columns_hash
(
table_name
)[
attribute_name
.
to_s
]
end
if
schema_cache
.
data_source_exists?
(
table_name
)
column
=
schema_cache
.
columns_hash
(
table_name
)[
attr_name
.
to_s
]
type
=
connection
.
lookup_cast_type_from_column
(
column
)
if
column
end
type
||
Type
.
default_value
end
delegate
:connection
,
to: :@klass
,
private:
true
private
attr_reader
:table_name
end
end
end
activerecord/test/cases/relation/where_test.rb
浏览文件 @
439145c9
...
...
@@ -18,7 +18,7 @@
module
ActiveRecord
class
WhereTest
<
ActiveRecord
::
TestCase
fixtures
:posts
,
:edges
,
:authors
,
:author_addresses
,
:binaries
,
:essays
,
:cars
,
:treasures
,
:price_estimates
,
:topics
fixtures
:posts
,
:
comments
,
:
edges
,
:authors
,
:author_addresses
,
:binaries
,
:essays
,
:cars
,
:treasures
,
:price_estimates
,
:topics
def
test_in_clause_is_correctly_sliced
assert_called
(
Author
.
connection
,
:in_clause_length
,
returns:
1
)
do
...
...
@@ -27,6 +27,11 @@ def test_in_clause_is_correctly_sliced
end
end
def
test_type_casting_nested_joins
comment
=
comments
(
:eager_other_comment1
)
assert_equal
[
comment
],
Comment
.
joins
(
post: :author
).
where
(
authors:
{
id:
"2-foo"
})
end
def
test_where_copies_bind_params
author
=
authors
(
:david
)
posts
=
author
.
posts
.
where
(
"posts.id != 1"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录