Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
94dabf9b
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,发现更多精彩内容 >>
提交
94dabf9b
编写于
7月 28, 2009
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generate methods for all suffixes
上级
c2b075be
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
12 addition
and
32 deletion
+12
-32
activerecord/lib/active_record/attribute_methods.rb
activerecord/lib/active_record/attribute_methods.rb
+4
-11
activerecord/lib/active_record/attribute_methods/read.rb
activerecord/lib/active_record/attribute_methods/read.rb
+8
-3
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+0
-4
activerecord/test/cases/attribute_methods_test.rb
activerecord/test/cases/attribute_methods_test.rb
+0
-14
未找到文件。
activerecord/lib/active_record/attribute_methods.rb
浏览文件 @
94dabf9b
...
...
@@ -65,8 +65,7 @@ def generated_methods?
def
define_attribute_methods
return
if
generated_methods?
columns_hash
.
keys
.
each
do
|
name
|
# TODO: Generate for all defined suffixes
[
""
,
"="
,
"?"
].
each
do
|
suffix
|
attribute_method_suffixes
.
each
do
|
suffix
|
method_name
=
"
#{
name
}#{
suffix
}
"
unless
instance_method_already_implemented?
(
method_name
)
generate_method
=
"define_attribute_method
#{
suffix
}
"
...
...
@@ -81,14 +80,10 @@ def define_attribute_methods
end
# Checks whether the method is defined in the model or any of its subclasses
# that also derive from Active Record. Raises DangerousAttributeError if the
# method is defined by Active Record though.
# that also derive from Active Record.
def
instance_method_already_implemented?
(
method_name
)
method_name
=
method_name
.
to_s
return
true
if
method_name
=~
/^id(=$|\?$|$)/
# TODO: Check against all defined suffixes
@_defined_class_methods
||=
ancestors
.
first
(
ancestors
.
index
(
ActiveRecord
::
Base
)).
sum
([])
{
|
m
|
m
.
public_instance_methods
(
false
)
|
m
.
private_instance_methods
(
false
)
|
m
.
protected_instance_methods
(
false
)
}.
map
{
|
m
|
m
.
to_s
}.
to_set
@@_defined_activerecord_methods
||=
(
ActiveRecord
::
Base
.
public_instance_methods
(
false
)
|
ActiveRecord
::
Base
.
private_instance_methods
(
false
)
|
ActiveRecord
::
Base
.
protected_instance_methods
(
false
)).
map
{
|
m
|
m
.
to_s
}.
to_set
raise
DangerousAttributeError
,
"
#{
method_name
}
is defined by ActiveRecord"
if
@@_defined_activerecord_methods
.
include?
(
method_name
)
@_defined_class_methods
||=
ancestors
.
first
(
ancestors
.
index
(
ActiveRecord
::
Base
)).
sum
([])
{
|
m
|
m
.
public_instance_methods
(
false
)
|
m
.
private_instance_methods
(
false
)
|
m
.
protected_instance_methods
(
false
)
}.
map
(
&
:to_s
).
to_set
@_defined_class_methods
.
include?
(
method_name
)
end
...
...
@@ -124,9 +119,7 @@ def attribute_method_suffixes
# Evaluate the definition for an attribute related method
def
evaluate_attribute_method
(
attr_name
,
method_definition
,
method_name
)
unless
method_name
.
to_s
==
primary_key
.
to_s
generated_methods
<<
method_name
end
generated_methods
<<
method_name
begin
class_eval
(
method_definition
,
__FILE__
,
__LINE__
)
...
...
activerecord/lib/active_record/attribute_methods/read.rb
浏览文件 @
94dabf9b
...
...
@@ -3,9 +3,9 @@ module AttributeMethods
module
Read
extend
ActiveSupport
::
Concern
#
included do
#
attribute_method_suffix ""
#
end
included
do
attribute_method_suffix
""
end
module
ClassMethods
protected
...
...
@@ -74,6 +74,11 @@ def unserialize_attribute(attr_name)
"
#{
attr_name
}
was supposed to be a
#{
self
.
class
.
serialized_attributes
[
attr_name
]
}
, but was a
#{
unserialized_object
.
class
.
to_s
}
"
end
end
private
def
attribute
(
attribute_name
)
read_attribute
(
attribute_name
)
end
end
end
end
activerecord/lib/active_record/base.rb
浏览文件 @
94dabf9b
...
...
@@ -144,10 +144,6 @@ class ReadOnlyRecord < ActiveRecordError
class
Rollback
<
ActiveRecordError
end
# Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods).
class
DangerousAttributeError
<
ActiveRecordError
end
# Raised when you've tried to access a column which wasn't loaded by your finder.
# Typically this is because <tt>:select</tt> has been specified.
class
MissingAttributeError
<
NoMethodError
...
...
activerecord/test/cases/attribute_methods_test.rb
浏览文件 @
94dabf9b
...
...
@@ -74,10 +74,6 @@ def test_kernel_methods_not_implemented_in_activerecord
end
end
def
test_primary_key_implemented
assert
Class
.
new
(
ActiveRecord
::
Base
).
instance_method_already_implemented?
(
'id'
)
end
def
test_defined_kernel_methods_implemented_in_model
%w(test name display y)
.
each
do
|
method
|
klass
=
Class
.
new
ActiveRecord
::
Base
...
...
@@ -96,16 +92,6 @@ def test_defined_kernel_methods_implemented_in_model_abstract_subclass
end
end
def
test_raises_dangerous_attribute_error_when_defining_activerecord_method_in_model
%w(save create_or_update)
.
each
do
|
method
|
klass
=
Class
.
new
ActiveRecord
::
Base
klass
.
class_eval
"def
#{
method
}
() 'defined
#{
method
}
' end"
assert_raise
ActiveRecord
::
DangerousAttributeError
do
klass
.
instance_method_already_implemented?
(
method
)
end
end
end
def
test_only_time_related_columns_are_meant_to_be_cached_by_default
expected
=
%w(datetime timestamp time date)
.
sort
assert_equal
expected
,
ActiveRecord
::
Base
.
attribute_types_cached_by_default
.
map
(
&
:to_s
).
sort
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录