Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c57064a8
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,发现更多精彩内容 >>
提交
c57064a8
编写于
9月 21, 2012
作者:
F
Francesco Rodriguez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update AR/attribute_methods documentation [ci skip]
上级
fe78e1da
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
36 addition
and
26 deletion
+36
-26
activerecord/lib/active_record/attribute_methods/primary_key.rb
...record/lib/active_record/attribute_methods/primary_key.rb
+15
-11
activerecord/lib/active_record/attribute_methods/read.rb
activerecord/lib/active_record/attribute_methods/read.rb
+7
-5
activerecord/lib/active_record/attribute_methods/serialization.rb
...cord/lib/active_record/attribute_methods/serialization.rb
+11
-8
activerecord/lib/active_record/attribute_methods/write.rb
activerecord/lib/active_record/attribute_methods/write.rb
+3
-2
未找到文件。
activerecord/lib/active_record/attribute_methods/primary_key.rb
浏览文件 @
c57064a8
...
...
@@ -5,28 +5,29 @@ module AttributeMethods
module
PrimaryKey
extend
ActiveSupport
::
Concern
# Returns this record's primary key value wrapped in an Array if one is available
# Returns this record's primary key value wrapped in an Array if one is
# available.
def
to_key
key
=
self
.
id
[
key
]
if
key
end
# Returns the primary key value
# Returns the primary key value
.
def
id
read_attribute
(
self
.
class
.
primary_key
)
end
# Sets the primary key value
# Sets the primary key value
.
def
id
=
(
value
)
write_attribute
(
self
.
class
.
primary_key
,
value
)
if
self
.
class
.
primary_key
end
# Queries the primary key value
# Queries the primary key value
.
def
id?
query_attribute
(
self
.
class
.
primary_key
)
end
# Returns the primary key value before type cast
# Returns the primary key value before type cast
.
def
id_before_type_cast
read_attribute_before_type_cast
(
self
.
class
.
primary_key
)
end
...
...
@@ -52,14 +53,16 @@ def dangerous_attribute_method?(method_name)
super
&&
!
ID_ATTRIBUTE_METHODS
.
include?
(
method_name
)
end
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
# primary_key_prefix_type setting, though.
# Defines the primary key field -- can be overridden in subclasses.
# Overwriting will negate any effect of the +primary_key_prefix_type+
# setting, though.
def
primary_key
@primary_key
=
reset_primary_key
unless
defined?
@primary_key
@primary_key
end
# Returns a quoted version of the primary key name, used to construct SQL statements.
# Returns a quoted version of the primary key name, used to construct
# SQL statements.
def
quoted_primary_key
@quoted_primary_key
||=
connection
.
quote_column_name
(
primary_key
)
end
...
...
@@ -92,16 +95,17 @@ def get_primary_key(base_name) #:nodoc:
# Sets the name of the primary key column.
#
# class Project < ActiveRecord::Base
# self.primary_key =
"sysid"
# self.primary_key =
'sysid'
# end
#
# You can also define the
primary_key
method yourself:
# You can also define the
+primary_key+
method yourself:
#
# class Project < ActiveRecord::Base
# def self.primary_key
#
"foo_"
+ super
#
'foo_'
+ super
# end
# end
#
# Project.primary_key # => "foo_id"
def
primary_key
=
(
value
)
@primary_key
=
value
&&
value
.
to_s
...
...
activerecord/lib/active_record/attribute_methods/read.rb
浏览文件 @
c57064a8
...
...
@@ -14,9 +14,10 @@ module Read
end
module
ClassMethods
# +cache_attributes+ allows you to declare which converted attribute values should
# be cached. Usually caching only pays off for attributes with expensive conversion
# methods, like time related columns (e.g. +created_at+, +updated_at+).
# +cache_attributes+ allows you to declare which converted attribute
# values should be cached. Usually caching only pays off for attributes
# with expensive conversion methods, like time related columns (e.g.
# +created_at+, +updated_at+).
def
cache_attributes
(
*
attribute_names
)
cached_attributes
.
merge
attribute_names
.
map
{
|
attr
|
attr
.
to_s
}
end
...
...
@@ -65,8 +66,9 @@ def cacheable_column?(column)
ActiveRecord
::
Model
.
attribute_types_cached_by_default
=
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
# Returns the value of the attribute identified by <tt>attr_name</tt> after
# it has been typecast (for example, "2004-12-12" in a data column is cast
# to a date object, like Date.new(2004, 12, 12)).
def
read_attribute
(
attr_name
)
return
unless
attr_name
name_sym
=
attr_name
.
to_sym
...
...
activerecord/lib/active_record/attribute_methods/serialization.rb
浏览文件 @
c57064a8
...
...
@@ -4,17 +4,19 @@ 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.
# 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
,
instance_accessor:
false
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.
# 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
# <tt>SerializationTypeMismatch</tt> will be raised.
#
# ==== Parameters
#
...
...
@@ -22,7 +24,8 @@ module ClassMethods
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
# # Serialize a preferences attribute
#
# # Serialize a preferences attribute.
# class User < ActiveRecord::Base
# serialize :preferences
# end
...
...
@@ -60,7 +63,7 @@ def type
end
end
class
Attribute
<
Struct
.
new
(
:coder
,
:value
,
:state
)
class
Attribute
<
Struct
.
new
(
:coder
,
:value
,
:state
)
# :nodoc:
def
unserialized_value
state
==
:serialized
?
unserialize
:
value
end
...
...
activerecord/lib/active_record/attribute_methods/write.rb
浏览文件 @
c57064a8
...
...
@@ -20,8 +20,9 @@ def define_method_attribute=(attr_name)
end
end
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings
# for fixnum and float columns are turned into +nil+.
# Updates the attribute identified by <tt>attr_name</tt> with the
# specified +value+. Empty strings for fixnum and float columns are
# turned into +nil+.
def
write_attribute
(
attr_name
,
value
)
attr_name
=
attr_name
.
to_s
attr_name
=
self
.
class
.
primary_key
if
attr_name
==
'id'
&&
self
.
class
.
primary_key
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录