Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fb6df2ac
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 搜索 >>
提交
fb6df2ac
编写于
12月 29, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract an explicit type caster class
上级
7eed50c7
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
69 addition
and
39 deletion
+69
-39
activerecord/lib/active_record/attributes.rb
activerecord/lib/active_record/attributes.rb
+1
-0
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+1
-0
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+5
-7
activerecord/lib/active_record/model_schema.rb
activerecord/lib/active_record/model_schema.rb
+1
-0
activerecord/lib/active_record/table_metadata.rb
activerecord/lib/active_record/table_metadata.rb
+1
-32
activerecord/lib/active_record/type_caster.rb
activerecord/lib/active_record/type_caster.rb
+7
-0
activerecord/lib/active_record/type_caster/connection.rb
activerecord/lib/active_record/type_caster/connection.rb
+34
-0
activerecord/lib/active_record/type_caster/map.rb
activerecord/lib/active_record/type_caster/map.rb
+19
-0
未找到文件。
activerecord/lib/active_record/attributes.rb
浏览文件 @
fb6df2ac
...
...
@@ -122,6 +122,7 @@ def add_user_provided_columns(schema_columns)
end
def
clear_caches_calculated_from_columns
@arel_table
=
nil
@attributes_builder
=
nil
@column_names
=
nil
@column_types
=
nil
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
fb6df2ac
...
...
@@ -22,6 +22,7 @@
require
'active_record/explain_subscriber'
require
'active_record/relation/delegation'
require
'active_record/attributes'
require
'active_record/type_caster'
module
ActiveRecord
#:nodoc:
# = Active Record
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
fb6df2ac
...
...
@@ -235,7 +235,7 @@ def ===(object)
# scope :published_and_commented, -> { published.and(self.arel_table[:comments_count].gt(0)) }
# end
def
arel_table
# :nodoc:
@arel_table
||=
Arel
::
Table
.
new
(
table_name
,
type_caster:
self
)
@arel_table
||=
Arel
::
Table
.
new
(
table_name
,
type_caster:
type_caster
)
end
# Returns the Arel engine.
...
...
@@ -252,12 +252,6 @@ def predicate_builder # :nodoc:
@predicate_builder
||=
PredicateBuilder
.
new
(
table_metadata
)
end
def
type_cast_for_database
(
attribute_name
,
value
)
return
value
if
value
.
is_a?
(
Arel
::
Nodes
::
BindParam
)
type
=
type_for_attribute
(
attribute_name
.
to_s
)
type
.
type_cast_for_database
(
value
)
end
private
def
relation
# :nodoc:
...
...
@@ -273,6 +267,10 @@ def relation # :nodoc:
def
table_metadata
# :nodoc:
TableMetadata
.
new
(
self
,
arel_table
)
end
def
type_caster
# :nodoc:
TypeCaster
::
Map
.
new
(
self
)
end
end
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
...
...
activerecord/lib/active_record/model_schema.rb
浏览文件 @
fb6df2ac
...
...
@@ -298,6 +298,7 @@ def reset_column_information
connection
.
schema_cache
.
clear_table_cache!
(
table_name
)
if
table_exists?
@arel_engine
=
nil
@arel_table
=
nil
@column_names
=
nil
@column_types
=
nil
@content_columns
=
nil
...
...
activerecord/lib/active_record/table_metadata.rb
浏览文件 @
fb6df2ac
...
...
@@ -34,7 +34,7 @@ def associated_table(table_name)
association_klass
=
association
.
klass
arel_table
=
association_klass
.
arel_table
else
type_caster
=
ConnectionAdapterTypeCaster
.
new
(
klass
.
connection
,
table_name
)
type_caster
=
TypeCaster
::
Connection
.
new
(
klass
.
connection
,
table_name
)
association_klass
=
nil
arel_table
=
Arel
::
Table
.
new
(
table_name
,
type_caster:
type_caster
)
end
...
...
@@ -50,35 +50,4 @@ def polymorphic_association?
attr_reader
:klass
,
:arel_table
,
:association
end
class
ConnectionAdapterTypeCaster
def
initialize
(
connection
,
table_name
)
@connection
=
connection
@table_name
=
table_name
end
def
type_cast_for_database
(
attribute_name
,
value
)
return
value
if
value
.
is_a?
(
Arel
::
Nodes
::
BindParam
)
type
=
type_for
(
attribute_name
)
type
.
type_cast_for_database
(
value
)
end
protected
attr_reader
:connection
,
:table_name
private
def
type_for
(
attribute_name
)
if
connection
.
schema_cache
.
table_exists?
(
table_name
)
column_for
(
attribute_name
).
cast_type
else
Type
::
Value
.
new
end
end
def
column_for
(
attribute_name
)
connection
.
schema_cache
.
columns_hash
(
table_name
)[
attribute_name
.
to_s
]
end
end
end
activerecord/lib/active_record/type_caster.rb
0 → 100644
浏览文件 @
fb6df2ac
require
'active_record/type_caster/map'
require
'active_record/type_caster/connection'
module
ActiveRecord
module
TypeCaster
end
end
activerecord/lib/active_record/type_caster/connection.rb
0 → 100644
浏览文件 @
fb6df2ac
module
ActiveRecord
module
TypeCaster
class
Connection
def
initialize
(
connection
,
table_name
)
@connection
=
connection
@table_name
=
table_name
end
def
type_cast_for_database
(
attribute_name
,
value
)
return
value
if
value
.
is_a?
(
Arel
::
Nodes
::
BindParam
)
type
=
type_for
(
attribute_name
)
type
.
type_cast_for_database
(
value
)
end
protected
attr_reader
:connection
,
:table_name
private
def
type_for
(
attribute_name
)
if
connection
.
schema_cache
.
table_exists?
(
table_name
)
column_for
(
attribute_name
).
cast_type
else
Type
::
Value
.
new
end
end
def
column_for
(
attribute_name
)
connection
.
schema_cache
.
columns_hash
(
table_name
)[
attribute_name
.
to_s
]
end
end
end
end
activerecord/lib/active_record/type_caster/map.rb
0 → 100644
浏览文件 @
fb6df2ac
module
ActiveRecord
module
TypeCaster
class
Map
def
initialize
(
types
)
@types
=
types
end
def
type_cast_for_database
(
attr_name
,
value
)
return
value
if
value
.
is_a?
(
Arel
::
Nodes
::
BindParam
)
type
=
types
.
type_for_attribute
(
attr_name
.
to_s
)
type
.
type_cast_for_database
(
value
)
end
protected
attr_reader
:types
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录