Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
aa85bdba
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,发现更多精彩内容 >>
提交
aa85bdba
编写于
12月 03, 2013
作者:
L
Lauro Caetano
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use a whitelist to delegate methods to array
上级
e87c3da2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
75 deletion
+38
-75
activerecord/lib/active_record/relation/delegation.rb
activerecord/lib/active_record/relation/delegation.rb
+6
-18
activerecord/test/cases/relation/delegation_test.rb
activerecord/test/cases/relation/delegation_test.rb
+32
-57
未找到文件。
activerecord/lib/active_record/relation/delegation.rb
浏览文件 @
aa85bdba
...
@@ -36,7 +36,11 @@ def inherited(child_class)
...
@@ -36,7 +36,11 @@ def inherited(child_class)
# may vary depending on the klass of a relation, so we create a subclass of Relation
# may vary depending on the klass of a relation, so we create a subclass of Relation
# for each different klass, and the delegations are compiled into that subclass only.
# for each different klass, and the delegations are compiled into that subclass only.
delegate
:to_xml
,
:to_yaml
,
:length
,
:collect
,
:map
,
:each
,
:all?
,
:include?
,
:to_ary
,
:to
=>
:to_a
delegate
:&
,
:
+
,
:[]
,
:all?
,
:collect
,
:detect
,
:each
,
:each_cons
,
:each_with_index
,
:flat_map
,
:group_by
,
:include?
,
:length
,
:map
,
:none?
,
:one?
,
:reverse
,
:sample
,
:second
,
:sort
,
:sort_by
,
:to_ary
,
:to_set
,
:to_xml
,
:to_yaml
,
:to
=>
:to_a
delegate
:table_name
,
:quoted_table_name
,
:primary_key
,
:quoted_primary_key
,
delegate
:table_name
,
:quoted_table_name
,
:primary_key
,
:quoted_primary_key
,
:connection
,
:columns_hash
,
:to
=>
:klass
:connection
,
:columns_hash
,
:to
=>
:klass
...
@@ -84,9 +88,6 @@ def method_missing(method, *args, &block)
...
@@ -84,9 +88,6 @@ def method_missing(method, *args, &block)
if
@klass
.
respond_to?
(
method
)
if
@klass
.
respond_to?
(
method
)
self
.
class
.
delegate_to_scoped_klass
(
method
)
self
.
class
.
delegate_to_scoped_klass
(
method
)
scoping
{
@klass
.
send
(
method
,
*
args
,
&
block
)
}
scoping
{
@klass
.
send
(
method
,
*
args
,
&
block
)
}
elsif
array_delegable?
(
method
)
self
.
class
.
delegate
method
,
:to
=>
:to_a
to_a
.
send
(
method
,
*
args
,
&
block
)
elsif
arel
.
respond_to?
(
method
)
elsif
arel
.
respond_to?
(
method
)
self
.
class
.
delegate
method
,
:to
=>
:arel
self
.
class
.
delegate
method
,
:to
=>
:arel
arel
.
send
(
method
,
*
args
,
&
block
)
arel
.
send
(
method
,
*
args
,
&
block
)
...
@@ -109,28 +110,15 @@ def relation_class_for(klass)
...
@@ -109,28 +110,15 @@ def relation_class_for(klass)
end
end
def
respond_to?
(
method
,
include_private
=
false
)
def
respond_to?
(
method
,
include_private
=
false
)
super
||
array_delegable?
(
method
)
||
super
||
@klass
.
respond_to?
(
method
,
include_private
)
||
@klass
.
respond_to?
(
method
,
include_private
)
||
arel
.
respond_to?
(
method
,
include_private
)
arel
.
respond_to?
(
method
,
include_private
)
end
end
protected
protected
def
array_delegable?
(
method
)
defined
=
Array
.
method_defined?
(
method
)
if
defined
&&
method
.
to_s
.
ends_with?
(
'!'
)
ActiveSupport
::
Deprecation
.
warn
(
"Association will no longer delegate
#{
method
}
to #to_a as of Rails 4.2. You instead must first call #to_a on the association to expose the array to be acted on."
)
end
defined
end
def
method_missing
(
method
,
*
args
,
&
block
)
def
method_missing
(
method
,
*
args
,
&
block
)
if
@klass
.
respond_to?
(
method
)
if
@klass
.
respond_to?
(
method
)
scoping
{
@klass
.
send
(
method
,
*
args
,
&
block
)
}
scoping
{
@klass
.
send
(
method
,
*
args
,
&
block
)
}
elsif
array_delegable?
(
method
)
to_a
.
send
(
method
,
*
args
,
&
block
)
elsif
arel
.
respond_to?
(
method
)
elsif
arel
.
respond_to?
(
method
)
arel
.
send
(
method
,
*
args
,
&
block
)
arel
.
send
(
method
,
*
args
,
&
block
)
else
else
...
...
activerecord/test/cases/relation/delegation_test.rb
浏览文件 @
aa85bdba
...
@@ -6,22 +6,21 @@ module ActiveRecord
...
@@ -6,22 +6,21 @@ module ActiveRecord
class
DelegationTest
<
ActiveRecord
::
TestCase
class
DelegationTest
<
ActiveRecord
::
TestCase
fixtures
:posts
fixtures
:posts
def
assert_responds
(
target
,
method
)
def
call_method
(
target
,
method
)
assert
target
.
respond_to?
(
method
)
method_arity
=
target
.
to_a
.
method
(
method
).
arity
assert_nothing_raised
do
method_arity
=
target
.
to_a
.
method
(
method
).
arity
if
method_arity
.
zero?
if
method_arity
.
zero?
target
.
send
(
method
)
elsif
method_arity
<
0
if
method
==
:shuffle!
target
.
send
(
method
)
target
.
send
(
method
)
elsif
method_arity
<
0
if
method
==
:shuffle!
target
.
send
(
method
)
else
target
.
send
(
method
,
1
)
end
else
else
raise
NotImplementedError
target
.
send
(
method
,
1
)
end
end
elsif
method_arity
==
1
target
.
send
(
method
,
1
)
else
raise
NotImplementedError
end
end
end
end
end
end
...
@@ -31,31 +30,20 @@ def target
...
@@ -31,31 +30,20 @@ def target
Post
.
first
.
comments
Post
.
first
.
comments
end
end
[
:map
,
:collect
].
each
do
|
method
|
[
:&
,
:
+
,
:[]
,
:all?
,
:collect
,
:detect
,
:each
,
:each_cons
,
test
"#
#{
method
}
is delegated"
do
:each_with_index
,
:flat_map
,
:group_by
,
:include?
,
:length
,
assert_responds
(
target
,
method
)
:map
,
:none?
,
:one?
,
:reverse
,
:sample
,
:second
,
:sort
,
:sort_by
,
assert_equal
(
target
.
pluck
(
:body
),
target
.
send
(
method
)
{
|
post
|
post
.
body
})
:to_ary
,
:to_set
,
:to_xml
,
:to_yaml
].
each
do
|
method
|
end
test
"association delegates
#{
method
}
to Array"
do
assert_respond_to
target
,
method
test
"#
#{
method
}
! is not delegated"
do
assert_deprecated
do
assert_responds
(
target
,
"
#{
method
}
!"
)
end
end
end
end
end
[
:compact!
,
:flatten!
,
:reject!
,
:reverse!
,
:rotate!
,
[
:compact!
,
:flatten!
,
:reject!
,
:reverse!
,
:rotate!
,
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
].
each
do
|
method
|
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
,
:delete_if
,
test
"#
#{
method
}
delegation is deprecated"
do
:keep_if
,
:pop
,
:shift
,
:delete_at
,
:compact
].
each
do
|
method
|
assert_deprecated
do
test
"
#{
method
}
is not delegated to Array"
do
assert_responds
(
target
,
method
)
assert_raises
(
NoMethodError
)
{
call_method
(
target
,
method
)
}
end
end
end
[
:select!
,
:uniq!
].
each
do
|
method
|
test
"#
#{
method
}
is implemented"
do
assert_responds
(
target
,
method
)
end
end
end
end
end
end
...
@@ -64,36 +52,23 @@ class DelegationRelationTest < DelegationTest
...
@@ -64,36 +52,23 @@ class DelegationRelationTest < DelegationTest
fixtures
:comments
fixtures
:comments
def
target
def
target
Comment
.
where
(
body:
'Normal type'
)
Comment
.
all
end
end
[
:map
,
:collect
].
each
do
|
method
|
[
:&
,
:
+
,
:[]
,
:all?
,
:collect
,
:detect
,
:each
,
:each_cons
,
test
"#
#{
method
}
is delegated"
do
:each_with_index
,
:flat_map
,
:group_by
,
:include?
,
:length
,
assert_responds
(
target
,
method
)
:map
,
:none?
,
:one?
,
:reverse
,
:sample
,
:second
,
:sort
,
:sort_by
,
assert_equal
(
target
.
pluck
(
:body
),
target
.
send
(
method
)
{
|
post
|
post
.
body
})
:to_ary
,
:to_set
,
:to_xml
,
:to_yaml
].
each
do
|
method
|
end
test
"relation delegates
#{
method
}
to Array"
do
assert_respond_to
target
,
method
test
"#
#{
method
}
! is not delegated"
do
assert_deprecated
do
assert_responds
(
target
,
"
#{
method
}
!"
)
end
end
end
end
end
[
:compact!
,
:flatten!
,
:reject!
,
:reverse!
,
:rotate!
,
[
:compact!
,
:flatten!
,
:reject!
,
:reverse!
,
:rotate!
,
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
].
each
do
|
method
|
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
,
:delete_if
,
test
"#
#{
method
}
delegation is deprecated"
do
:keep_if
,
:pop
,
:shift
,
:delete_at
,
:compact
].
each
do
|
method
|
assert_deprecated
do
test
"
#{
method
}
is not delegated to Array"
do
assert_responds
(
target
,
method
)
assert_raises
(
NoMethodError
)
{
call_method
(
target
,
method
)
}
end
end
end
[
:select!
,
:uniq!
].
each
do
|
method
|
test
"#
#{
method
}
triggers an immutable error"
do
assert_raises
ActiveRecord
::
ImmutableRelation
do
assert_responds
(
target
,
method
)
end
end
end
end
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录