Skip to content

Commit

Permalink
pull preloading behavior in to the collection renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 28, 2020
1 parent 72e0d04 commit 49adb7f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 46 deletions.
26 changes: 25 additions & 1 deletion actionview/lib/action_view/renderer/collection_renderer.rb
Expand Up @@ -70,6 +70,24 @@ def each_with_info
end
end

class PreloadCollectionIterator < SameCollectionIterator # :nodoc:
def initialize(collection, path, variables, relation)
super(collection, path, variables)
relation.skip_preloading! unless relation.loaded?
@relation = relation
end

def from_collection(collection)
self.class.new(collection, @path, @variables, @relation)
end

def each_with_info
return super unless block_given?
@relation.preload_associations(@collection)
super
end
end

class MixedCollectionIterator < CollectionIterator # :nodoc:
def initialize(collection, paths)
super(collection)
Expand All @@ -84,7 +102,13 @@ def each_with_info

def render_collection_with_partial(collection, partial, context, block)
iter_vars = retrieve_variable(partial)
collection = SameCollectionIterator.new(collection, partial, iter_vars)

collection = if collection.respond_to?(:preload_associations)
PreloadCollectionIterator.new(collection, partial, iter_vars, collection)
else
SameCollectionIterator.new(collection, partial, iter_vars)
end


template = find_template(partial, @locals.keys + iter_vars)

Expand Down
3 changes: 0 additions & 3 deletions actionview/test/activerecord/multifetch_cache_test.rb
@@ -1,9 +1,6 @@
# frozen_string_literal: true

require "active_record_unit"
require "active_record/railties/collection_cache_association_loading"

ActionView::CollectionRenderer.prepend(ActiveRecord::Railties::CollectionCacheAssociationLoading)

class MultifetchCacheTest < ActiveRecordTestCase
fixtures :topics, :replies
Expand Down
7 changes: 0 additions & 7 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -221,13 +221,6 @@ 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::CollectionRenderer.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
Expand Down

This file was deleted.

0 comments on commit 49adb7f

Please sign in to comment.