Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bb44e5a8
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,发现更多精彩内容 >>
提交
bb44e5a8
编写于
12月 14, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use a separate module for 'external' attribute methods.
上级
a5589db0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
17 deletion
+26
-17
activerecord/lib/active_record/attribute_methods.rb
activerecord/lib/active_record/attribute_methods.rb
+14
-0
activerecord/lib/active_record/attribute_methods/primary_key.rb
...record/lib/active_record/attribute_methods/primary_key.rb
+3
-3
activerecord/lib/active_record/attribute_methods/read.rb
activerecord/lib/active_record/attribute_methods/read.rb
+9
-14
未找到文件。
activerecord/lib/active_record/attribute_methods.rb
浏览文件 @
bb44e5a8
...
...
@@ -33,6 +33,20 @@ def generated_attribute_methods
@generated_attribute_methods
||=
(
base_class
==
self
?
super
:
base_class
.
generated_attribute_methods
)
end
def
generated_external_attribute_methods
@generated_external_attribute_methods
||=
begin
if
base_class
==
self
# We will define the methods as instance methods, but will call them as singleton
# methods. This allows us to use method_defined? to check if the method exists,
# which is fast and won't give any false positives from the ancestors (because
# there are no ancestors).
Module
.
new
{
extend
self
}
else
base_class
.
generated_external_attribute_methods
end
end
end
def
undefine_attribute_methods
if
base_class
==
self
super
...
...
activerecord/lib/active_record/attribute_methods/primary_key.rb
浏览文件 @
bb44e5a8
...
...
@@ -30,10 +30,10 @@ def define_method_attribute(attr_name)
if
attr_name
==
primary_key
&&
attr_name
!=
'id'
generated_attribute_methods
.
send
(
:alias_method
,
:id
,
primary_key
)
generated_attribute_methods
.
module_eval
<<-
CODE
,
__FILE__
,
__LINE__
def
self.attribute_
id(v, attributes, attributes_cache, attr_name)
generated_
external_
attribute_methods
.
module_eval
<<-
CODE
,
__FILE__
,
__LINE__
def id(v, attributes, attributes_cache, attr_name)
attr_name = '
#{
primary_key
}
'
send(
:'attribute_
#{
attr_name
}
'
, attributes[attr_name], attributes, attributes_cache, attr_name)
send(
attr_name
, attributes[attr_name], attributes, attributes_cache, attr_name)
end
CODE
end
...
...
activerecord/lib/active_record/attribute_methods/read.rb
浏览文件 @
bb44e5a8
...
...
@@ -31,10 +31,8 @@ def cache_attribute?(attr_name)
def
undefine_attribute_methods
if
base_class
==
self
generated_attribute_methods
.
module_eval
do
public_methods
(
false
).
each
do
|
m
|
singleton_class
.
send
(
:undef_method
,
m
)
if
m
.
to_s
=~
/^attribute_/
end
generated_external_attribute_methods
.
module_eval
do
instance_methods
.
each
{
|
m
|
undef_method
(
m
)
}
end
end
...
...
@@ -52,22 +50,20 @@ def undefine_attribute_methods
# rename it to what we want.
def
define_method_attribute
(
attr_name
)
cast_code
=
attribute_cast_code
(
attr_name
)
internal
=
internal_attribute_access_code
(
attr_name
,
cast_code
)
external
=
external_attribute_access_code
(
attr_name
,
cast_code
)
generated_attribute_methods
.
module_eval
<<-
STR
,
__FILE__
,
__LINE__
+
1
def __temp__
#{
internal
}
#{
internal
_attribute_access_code
(
attr_name
,
cast_code
)
}
end
alias_method '
#{
attr_name
}
', :__temp__
undef_method :__temp__
STR
generated_
attribute_methods
.
singleton_clas
s
.
module_eval
<<-
STR
,
__FILE__
,
__LINE__
+
1
generated_
external_attribute_method
s
.
module_eval
<<-
STR
,
__FILE__
,
__LINE__
+
1
def __temp__(v, attributes, attributes_cache, attr_name)
#{
external
}
#{
external
_attribute_access_code
(
attr_name
,
cast_code
)
}
end
alias_method '
attribute_
#{
attr_name
}
', :__temp__
alias_method '
#{
attr_name
}
', :__temp__
undef_method :__temp__
STR
end
...
...
@@ -110,12 +106,11 @@ def attribute_cast_code(attr_name)
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def
read_attribute
(
attr_name
)
attr_name
=
attr_name
.
to_s
accessor
=
"attribute_
#{
attr_name
}
"
methods
=
self
.
class
.
generated_attribute_methods
methods
=
self
.
class
.
generated_external_attribute_methods
if
methods
.
respond_to?
(
accessor
)
if
methods
.
method_defined?
(
attr_name
)
if
@attributes
.
has_key?
(
attr_name
)
||
attr_name
==
'id'
methods
.
send
(
a
ccessor
,
@attributes
[
attr_name
],
@attributes
,
@attributes_cache
,
attr_name
)
methods
.
send
(
a
ttr_name
,
@attributes
[
attr_name
],
@attributes
,
@attributes_cache
,
attr_name
)
end
elsif
!
self
.
class
.
attribute_methods_generated?
# If we haven't generated the caster methods yet, do that and
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录