Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a52c6989
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
a52c6989
编写于
10月 10, 2018
作者:
R
Ryuta Kamizono
提交者:
GitHub
10月 10, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #34122 from kamipo/generate_relation_methods
Generate delegation methods to named scope in the definition time
上级
6c69a960
136b738c
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
59 addition
and
5 deletion
+59
-5
activerecord/lib/active_record/relation/delegation.rb
activerecord/lib/active_record/relation/delegation.rb
+30
-0
activerecord/lib/active_record/scoping/named.rb
activerecord/lib/active_record/scoping/named.rb
+2
-0
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+1
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+10
-0
activerecord/test/models/account.rb
activerecord/test/models/account.rb
+10
-3
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+6
-2
未找到文件。
activerecord/lib/active_record/relation/delegation.rb
浏览文件 @
a52c6989
...
@@ -17,6 +17,7 @@ def initialize_relation_delegate_cache
...
@@ -17,6 +17,7 @@ def initialize_relation_delegate_cache
delegate
=
Class
.
new
(
klass
)
{
delegate
=
Class
.
new
(
klass
)
{
include
ClassSpecificRelation
include
ClassSpecificRelation
}
}
include_relation_methods
(
delegate
)
mangled_name
=
klass
.
name
.
gsub
(
"::"
,
"_"
)
mangled_name
=
klass
.
name
.
gsub
(
"::"
,
"_"
)
const_set
mangled_name
,
delegate
const_set
mangled_name
,
delegate
private_constant
mangled_name
private_constant
mangled_name
...
@@ -29,6 +30,35 @@ def inherited(child_class)
...
@@ -29,6 +30,35 @@ def inherited(child_class)
child_class
.
initialize_relation_delegate_cache
child_class
.
initialize_relation_delegate_cache
super
super
end
end
protected
def
include_relation_methods
(
delegate
)
superclass
.
include_relation_methods
(
delegate
)
unless
base_class?
delegate
.
include
generated_relation_methods
end
private
def
generated_relation_methods
@generated_relation_methods
||=
Module
.
new
.
tap
do
|
mod
|
mod_name
=
"GeneratedRelationMethods"
const_set
mod_name
,
mod
private_constant
mod_name
end
end
def
generate_relation_method
(
method
)
if
/\A[a-zA-Z_]\w*[!?]?\z/
.
match?
(
method
)
generated_relation_methods
.
module_eval
<<-
RUBY
,
__FILE__
,
__LINE__
+
1
def
#{
method
}
(*args, &block)
scoping { klass.
#{
method
}
(*args, &block) }
end
RUBY
else
generated_relation_methods
.
send
(
:define_method
,
method
)
do
|*
args
,
&
block
|
scoping
{
klass
.
public_send
(
method
,
*
args
,
&
block
)
}
end
end
end
end
end
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
...
...
activerecord/lib/active_record/scoping/named.rb
浏览文件 @
a52c6989
...
@@ -191,6 +191,8 @@ def scope(name, body, &block)
...
@@ -191,6 +191,8 @@ def scope(name, body, &block)
scope
scope
end
end
end
end
generate_relation_method
(
name
)
end
end
private
private
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
a52c6989
...
@@ -2433,6 +2433,7 @@ def test_replace_returns_target
...
@@ -2433,6 +2433,7 @@ def test_replace_returns_target
def
test_collection_association_with_private_kernel_method
def
test_collection_association_with_private_kernel_method
firm
=
companies
(
:first_firm
)
firm
=
companies
(
:first_firm
)
assert_equal
[
accounts
(
:signals37
)],
firm
.
accounts
.
open
assert_equal
[
accounts
(
:signals37
)],
firm
.
accounts
.
open
assert_equal
[
accounts
(
:signals37
)],
firm
.
accounts
.
available
end
end
def
test_association_with_or_doesnt_set_inverse_instance_key
def
test_association_with_or_doesnt_set_inverse_instance_key
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
a52c6989
...
@@ -1812,6 +1812,16 @@ def test_relation_join_method
...
@@ -1812,6 +1812,16 @@ def test_relation_join_method
assert_equal
"Thank you for the welcome,Thank you again for the welcome"
,
Post
.
first
.
comments
.
join
(
","
)
assert_equal
"Thank you for the welcome,Thank you again for the welcome"
,
Post
.
first
.
comments
.
join
(
","
)
end
end
def
test_relation_with_private_kernel_method
accounts
=
Account
.
all
assert_equal
[
accounts
(
:signals37
)],
accounts
.
open
assert_equal
[
accounts
(
:signals37
)],
accounts
.
available
sub_accounts
=
SubAccount
.
all
assert_equal
[
accounts
(
:signals37
)],
sub_accounts
.
open
assert_equal
[
accounts
(
:signals37
)],
sub_accounts
.
available
end
test
"#skip_query_cache!"
do
test
"#skip_query_cache!"
do
Post
.
cache
do
Post
.
cache
do
assert_queries
(
1
)
do
assert_queries
(
1
)
do
...
...
activerecord/test/models/account.rb
浏览文件 @
a52c6989
...
@@ -11,9 +11,8 @@ def self.destroyed_account_ids
...
@@ -11,9 +11,8 @@ def self.destroyed_account_ids
end
end
# Test private kernel method through collection proxy using has_many.
# Test private kernel method through collection proxy using has_many.
def
self
.
open
scope
:open
,
->
{
where
(
"firm_name = ?"
,
"37signals"
)
}
where
(
"firm_name = ?"
,
"37signals"
)
scope
:available
,
->
{
open
}
end
before_destroy
do
|
account
|
before_destroy
do
|
account
|
if
account
.
firm
if
account
.
firm
...
@@ -32,3 +31,11 @@ def private_method
...
@@ -32,3 +31,11 @@ def private_method
"Sir, yes sir!"
"Sir, yes sir!"
end
end
end
end
class
SubAccount
<
Account
def
self
.
instantiate_instance_of
(
klass
,
attributes
,
column_types
=
{},
&
block
)
klass
=
superclass
super
end
private_class_method
:instantiate_instance_of
end
activerecord/test/models/post.rb
浏览文件 @
a52c6989
...
@@ -297,8 +297,6 @@ class SubConditionalStiPost < ConditionalStiPost
...
@@ -297,8 +297,6 @@ class SubConditionalStiPost < ConditionalStiPost
class
FakeKlass
class
FakeKlass
extend
ActiveRecord
::
Delegation
::
DelegateCache
extend
ActiveRecord
::
Delegation
::
DelegateCache
inherited
self
class
<<
self
class
<<
self
def
connection
def
connection
Post
.
connection
Post
.
connection
...
@@ -335,5 +333,11 @@ def arel_table
...
@@ -335,5 +333,11 @@ def arel_table
def
predicate_builder
def
predicate_builder
Post
.
predicate_builder
Post
.
predicate_builder
end
end
def
base_class?
true
end
end
end
inherited
self
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录