Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ef10fc21
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 搜索 >>
提交
ef10fc21
编写于
10月 22, 2019
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #37523 from kamipo/refactor_association_scoping
Refactor `association.scoping` not to rely on `klass.all`
上级
bc5c2c0d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
25 addition
and
18 deletion
+25
-18
activerecord/lib/active_record/association_relation.rb
activerecord/lib/active_record/association_relation.rb
+6
-6
activerecord/lib/active_record/associations/association.rb
activerecord/lib/active_record/associations/association.rb
+7
-11
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+9
-0
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+3
-1
未找到文件。
activerecord/lib/active_record/association_relation.rb
浏览文件 @
ef10fc21
...
...
@@ -17,23 +17,23 @@ def ==(other)
def
build
(
attributes
=
nil
,
&
block
)
block
=
_deprecated_scope_block
(
"new"
,
&
block
)
@association
.
enable_scoping
do
scoping
{
@association
.
build
(
attributes
,
&
block
)
}
@association
.
scoping
(
self
)
do
@association
.
build
(
attributes
,
&
block
)
end
end
alias
new
build
def
create
(
attributes
=
nil
,
&
block
)
block
=
_deprecated_scope_block
(
"create"
,
&
block
)
@association
.
enable_scoping
do
scoping
{
@association
.
create
(
attributes
,
&
block
)
}
@association
.
scoping
(
self
)
do
@association
.
create
(
attributes
,
&
block
)
end
end
def
create!
(
attributes
=
nil
,
&
block
)
block
=
_deprecated_scope_block
(
"create!"
,
&
block
)
@association
.
enable_scoping
do
scoping
{
@association
.
create!
(
attributes
,
&
block
)
}
@association
.
scoping
(
self
)
do
@association
.
create!
(
attributes
,
&
block
)
end
end
...
...
activerecord/lib/active_record/associations/association.rb
浏览文件 @
ef10fc21
...
...
@@ -43,7 +43,7 @@ def initialize(owner, reflection)
reflection
.
check_validity!
@owner
,
@reflection
=
owner
,
reflection
@
enable_scoping
=
false
@
_scope
=
nil
reset
reset_scope
...
...
@@ -96,7 +96,7 @@ def target=(target)
end
def
scope
target_scope
.
merge!
(
association_scope
)
@_scope
&
.
spawn
||
target_scope
.
merge!
(
association_scope
)
end
def
reset_scope
...
...
@@ -196,11 +196,11 @@ def create!(attributes = {}, &block)
_create_record
(
attributes
,
true
,
&
block
)
end
def
enable_scoping
@
enable_scoping
=
true
yield
def
scoping
(
relation
,
&
block
)
@
_scope
=
relation
relation
.
scoping
(
&
block
)
ensure
@
enable_scoping
=
false
@
_scope
=
nil
end
private
...
...
@@ -233,11 +233,7 @@ def association_scope
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope)
def
target_scope
AssociationRelation
.
create
(
klass
,
self
).
merge!
(
scope_for_association
)
end
def
scope_for_association
@enable_scoping
?
klass
.
all
:
klass
.
scope_for_association
AssociationRelation
.
create
(
klass
,
self
).
merge!
(
klass
.
scope_for_association
)
end
def
scope_for_create
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
ef10fc21
...
...
@@ -254,6 +254,15 @@ def test_build_and_create_from_association_should_respect_unscope_over_default_s
bulb
=
car
.
bulbs
.
unscope
(
where: :name
).
create!
assert_nil
bulb
.
name
bulb
=
car
.
awesome_bulbs
.
unscope
(
where: :frickinawesome
).
build
assert_equal
false
,
bulb
.
frickinawesome
bulb
=
car
.
awesome_bulbs
.
unscope
(
where: :frickinawesome
).
create
assert_equal
false
,
bulb
.
frickinawesome
bulb
=
car
.
awesome_bulbs
.
unscope
(
where: :frickinawesome
).
create!
assert_equal
false
,
bulb
.
frickinawesome
end
def
test_build_from_association_should_respect_scope
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
ef10fc21
...
...
@@ -688,8 +688,10 @@ def test_associate_with_create
def
test_through_record_is_built_when_created_with_where
assert_difference
(
"posts(:thinking).readers.count"
,
1
)
do
posts
(
:thinking
).
people
.
where
(
first_name:
"Jeb"
).
create
posts
(
:thinking
).
people
.
where
(
readers:
{
skimmer:
true
}).
create
(
first_name:
"Jeb"
)
end
reader
=
posts
(
:thinking
).
readers
.
last
assert_equal
true
,
reader
.
skimmer
end
def
test_associate_with_create_and_no_options
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录