Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
00853808
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 搜索 >>
未验证
提交
00853808
编写于
3月 15, 2018
作者:
K
Kasper Timm Hansen
提交者:
GitHub
3月 15, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #31250 from lsylvester/only-preload-misses-on-multifetch-cache
Only preload misses on multifetch cache
上级
49c8f944
d04a32fe
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
101 addition
and
9 deletion
+101
-9
actionview/test/activerecord/multifetch_cache_test.rb
actionview/test/activerecord/multifetch_cache_test.rb
+35
-0
activerecord/lib/active_record/railtie.rb
activerecord/lib/active_record/railtie.rb
+8
-0
activerecord/lib/active_record/railties/collection_cache_association_loading.rb
...e_record/railties/collection_cache_association_loading.rb
+34
-0
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+13
-8
activerecord/lib/active_record/relation/query_methods.rb
activerecord/lib/active_record/relation/query_methods.rb
+5
-0
activerecord/test/cases/relation/mutation_test.rb
activerecord/test/cases/relation/mutation_test.rb
+6
-1
未找到文件。
actionview/test/activerecord/multifetch_cache_test.rb
0 → 100644
浏览文件 @
00853808
# frozen_string_literal: true
require
"active_record_unit"
require
"active_record/railties/collection_cache_association_loading"
ActionView
::
PartialRenderer
.
prepend
(
ActiveRecord
::
Railties
::
CollectionCacheAssociationLoading
)
class
MultifetchCacheTest
<
ActiveRecordTestCase
fixtures
:topics
,
:replies
def
setup
view_paths
=
ActionController
::
Base
.
view_paths
@view
=
Class
.
new
(
ActionView
::
Base
)
do
def
view_cache_dependencies
[]
end
def
combined_fragment_cache_key
(
key
)
[
:views
,
key
]
end
end
.
new
(
view_paths
,
{})
end
def
test_only_preloading_for_records_that_miss_the_cache
@view
.
render
partial:
"test/partial"
,
collection:
[
topics
(
:rails
)],
cached:
true
@topics
=
Topic
.
preload
(
:replies
)
@view
.
render
partial:
"test/partial"
,
collection:
@topics
,
cached:
true
assert_not
@topics
.
detect
{
|
topic
|
topic
.
id
==
topics
(
:rails
).
id
}.
replies
.
loaded?
assert
@topics
.
detect
{
|
topic
|
topic
.
id
!=
topics
(
:rails
).
id
}.
replies
.
loaded?
end
end
activerecord/lib/active_record/railtie.rb
浏览文件 @
00853808
...
...
@@ -157,6 +157,14 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
initializer
"active_record.collection_cache_association_loading"
do
require
"active_record/railties/collection_cache_association_loading"
ActiveSupport
.
on_load
(
:action_view
)
do
ActionView
::
PartialRenderer
.
prepend
(
ActiveRecord
::
Railties
::
CollectionCacheAssociationLoading
)
end
end
initializer
"active_record.set_reloader_hooks"
do
ActiveSupport
.
on_load
(
:active_record
)
do
ActiveSupport
::
Reloader
.
before_class_unload
do
...
...
activerecord/lib/active_record/railties/collection_cache_association_loading.rb
0 → 100644
浏览文件 @
00853808
# frozen_string_literal: true
module
ActiveRecord
module
Railties
# :nodoc:
module
CollectionCacheAssociationLoading
#:nodoc:
def
setup
(
context
,
options
,
block
)
@relation
=
relation_from_options
(
options
)
super
end
def
relation_from_options
(
cached:
nil
,
partial:
nil
,
collection:
nil
,
**
_
)
return
unless
cached
relation
=
partial
if
partial
.
is_a?
(
ActiveRecord
::
Relation
)
relation
||=
collection
if
collection
.
is_a?
(
ActiveRecord
::
Relation
)
if
relation
&&
!
relation
.
loaded?
relation
.
skip_preloading!
end
end
def
collection_without_template
@relation
.
preload_associations
(
@collection
)
if
@relation
super
end
def
collection_with_template
@relation
.
preload_associations
(
@collection
)
if
@relation
super
end
end
end
end
activerecord/lib/active_record/relation.rb
浏览文件 @
00853808
...
...
@@ -8,7 +8,8 @@ class Relation
:extending
,
:unscope
]
SINGLE_VALUE_METHODS
=
[
:limit
,
:offset
,
:lock
,
:readonly
,
:reordering
,
:reverse_order
,
:distinct
,
:create_with
,
:skip_query_cache
]
:reverse_order
,
:distinct
,
:create_with
,
:skip_query_cache
,
:skip_preloading
]
CLAUSE_METHODS
=
[
:where
,
:having
,
:from
]
INVALID_METHODS_FOR_DELETE_ALL
=
[
:distinct
,
:group
,
:having
]
...
...
@@ -546,6 +547,16 @@ def alias_tracker(joins = [], aliases = nil) # :nodoc:
ActiveRecord
::
Associations
::
AliasTracker
.
create
(
connection
,
table
.
name
,
joins
)
end
def
preload_associations
(
records
)
preload
=
preload_values
preload
+=
includes_values
unless
eager_loading?
preloader
=
nil
preload
.
each
do
|
associations
|
preloader
||=
build_preloader
preloader
.
preload
records
,
associations
end
end
protected
def
load_records
(
records
)
...
...
@@ -575,13 +586,7 @@ def exec_queries(&block)
klass
.
find_by_sql
(
arel
,
&
block
).
freeze
end
preload
=
preload_values
preload
+=
includes_values
unless
eager_loading?
preloader
=
nil
preload
.
each
do
|
associations
|
preloader
||=
build_preloader
preloader
.
preload
@records
,
associations
end
preload_associations
(
@records
)
unless
skip_preloading_value
@records
.
each
(
&
:readonly!
)
if
readonly_value
...
...
activerecord/lib/active_record/relation/query_methods.rb
浏览文件 @
00853808
...
...
@@ -899,6 +899,11 @@ def skip_query_cache! # :nodoc:
self
end
def
skip_preloading!
# :nodoc:
self
.
skip_preloading_value
=
true
self
end
# Returns the Arel object associated with the relation.
def
arel
(
aliases
=
nil
)
# :nodoc:
@arel
||=
build_arel
(
aliases
)
...
...
activerecord/test/cases/relation/mutation_test.rb
浏览文件 @
00853808
...
...
@@ -59,7 +59,7 @@ class RelationMutationTest < ActiveRecord::TestCase
assert_equal
[],
relation
.
extending_values
end
(
Relation
::
SINGLE_VALUE_METHODS
-
[
:lock
,
:reordering
,
:reverse_order
,
:create_with
,
:skip_query_cache
]).
each
do
|
method
|
(
Relation
::
SINGLE_VALUE_METHODS
-
[
:lock
,
:reordering
,
:reverse_order
,
:create_with
,
:skip_query_cache
,
:skip_preloading
]).
each
do
|
method
|
test
"#
#{
method
}
!"
do
assert
relation
.
public_send
(
"
#{
method
}
!"
,
:foo
).
equal?
(
relation
)
assert_equal
:foo
,
relation
.
public_send
(
"
#{
method
}
_value"
)
...
...
@@ -137,6 +137,11 @@ class RelationMutationTest < ActiveRecord::TestCase
assert
relation
.
skip_query_cache_value
end
test
"skip_preloading!"
do
relation
.
skip_preloading!
assert
relation
.
skip_preloading_value
end
private
def
relation
@relation
||=
Relation
.
new
(
FakeKlass
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录