Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6c63f1aa
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,发现更多精彩内容 >>
提交
6c63f1aa
编写于
11月 30, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move some serialization stuff out of Base
上级
f4853dc1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
40 deletion
+42
-40
activerecord/lib/active_record/attribute_methods/serialization.rb
...cord/lib/active_record/attribute_methods/serialization.rb
+42
-0
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+0
-40
未找到文件。
activerecord/lib/active_record/attribute_methods/serialization.rb
浏览文件 @
6c63f1aa
...
...
@@ -3,7 +3,41 @@ module AttributeMethods
module
Serialization
extend
ActiveSupport
::
Concern
included
do
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
class_attribute
:serialized_attributes
self
.
serialized_attributes
=
{}
end
module
ClassMethods
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
# then specify the name of that attribute using this method and it will be handled automatically.
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
# class on retrieval or SerializationTypeMismatch will be raised.
#
# ==== Parameters
#
# * +attr_name+ - The field name that should be serialized.
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
# # Serialize a preferences attribute
# class User < ActiveRecord::Base
# serialize :preferences
# end
def
serialize
(
attr_name
,
class_name
=
Object
)
coder
=
if
[
:load
,
:dump
].
all?
{
|
x
|
class_name
.
respond_to?
(
x
)
}
class_name
else
Coders
::
YAMLColumn
.
new
(
class_name
)
end
# merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
# has its own hash of own serialized attributes
self
.
serialized_attributes
=
serialized_attributes
.
merge
(
attr_name
.
to_s
=>
coder
)
end
def
define_method_attribute
(
attr_name
)
if
serialized_attributes
.
include?
(
attr_name
)
generated_attribute_methods
.
module_eval
(
<<-
CODE
,
__FILE__
,
__LINE__
)
...
...
@@ -22,6 +56,14 @@ def cacheable_column?(column)
end
end
def
set_serialized_attributes
sattrs
=
self
.
class
.
serialized_attributes
sattrs
.
each
do
|
key
,
coder
|
@attributes
[
key
]
=
coder
.
load
@attributes
[
key
]
if
@attributes
.
key?
(
key
)
end
end
def
type_cast_attribute
(
column
,
value
)
coder
=
self
.
class
.
serialized_attributes
[
column
.
name
]
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
6c63f1aa
...
...
@@ -433,11 +433,6 @@ class Base
class_attribute
:default_scopes
,
:instance_writer
=>
false
self
.
default_scopes
=
[]
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
class_attribute
:serialized_attributes
self
.
serialized_attributes
=
{}
class_attribute
:_attr_readonly
,
:instance_writer
=>
false
self
.
_attr_readonly
=
[]
...
...
@@ -560,33 +555,6 @@ def readonly_attributes
self
.
_attr_readonly
end
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
# then specify the name of that attribute using this method and it will be handled automatically.
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
# class on retrieval or SerializationTypeMismatch will be raised.
#
# ==== Parameters
#
# * +attr_name+ - The field name that should be serialized.
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
# # Serialize a preferences attribute
# class User < ActiveRecord::Base
# serialize :preferences
# end
def
serialize
(
attr_name
,
class_name
=
Object
)
coder
=
if
[
:load
,
:dump
].
all?
{
|
x
|
class_name
.
respond_to?
(
x
)
}
class_name
else
Coders
::
YAMLColumn
.
new
(
class_name
)
end
# merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
# has its own hash of own serialized attributes
self
.
serialized_attributes
=
serialized_attributes
.
merge
(
attr_name
.
to_s
=>
coder
)
end
def
deprecated_property_setter
(
property
,
value
,
block
)
#:nodoc:
if
block
ActiveSupport
::
Deprecation
.
warn
(
...
...
@@ -2004,14 +1972,6 @@ def to_ary # :nodoc:
nil
end
def
set_serialized_attributes
sattrs
=
self
.
class
.
serialized_attributes
sattrs
.
each
do
|
key
,
coder
|
@attributes
[
key
]
=
coder
.
load
@attributes
[
key
]
if
@attributes
.
key?
(
key
)
end
end
# Sets the attribute used for single table inheritance to this class name if this is not the
# ActiveRecord::Base descendant.
# Considering the hierarchy Reply < Message < ActiveRecord::Base, this makes it possible to
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录