From 5bb1ad59b1fea70c82d0a3b654ef81a0a31c715c Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Mon, 28 Jan 2019 14:55:12 -0800
Subject: Pull `@template` in to a local variable

This gets the PartialRenderer to be a bit closer to the
TemplateRenderer.  TemplateRenderer already keeps its template in a
local variable.
---
 .../lib/action_view/renderer/abstract_renderer.rb  |  2 --
 .../lib/action_view/renderer/partial_renderer.rb   | 31 +++++++++++-----------
 .../partial_renderer/collection_caching.rb         | 16 +++++------
 .../collection_cache_association_loading.rb        |  4 +--
 4 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/actionview/lib/action_view/renderer/abstract_renderer.rb b/actionview/lib/action_view/renderer/abstract_renderer.rb
index 37f07eabc2..c2a88868a8 100644
--- a/actionview/lib/action_view/renderer/abstract_renderer.rb
+++ b/actionview/lib/action_view/renderer/abstract_renderer.rb
@@ -38,8 +38,6 @@ module ActionView
       end
 
       def instrument(name, **options) # :doc:
-        options[:identifier] ||= (@template && @template.identifier) || @path
-
         ActiveSupport::Notifications.instrument("render_#{name}.action_view", options) do |payload|
           yield payload
         end
diff --git a/actionview/lib/action_view/renderer/partial_renderer.rb b/actionview/lib/action_view/renderer/partial_renderer.rb
index 42c6a97682..478400e016 100644
--- a/actionview/lib/action_view/renderer/partial_renderer.rb
+++ b/actionview/lib/action_view/renderer/partial_renderer.rb
@@ -296,41 +296,42 @@ module ActionView
 
     def render(context, options, block)
       setup(context, options, block)
-      @template = find_partial
+      template = find_partial
 
       @lookup_context.rendered_format ||= begin
-        if @template && @template.formats.present?
-          @template.formats.first
+        if template && template.formats.first
+          template.formats.first
         else
           formats.first
         end
       end
 
       if @collection
-        render_collection(context)
+        render_collection(context, template)
       else
-        render_partial(context)
+        render_partial(context, template)
       end
     end
 
     private
 
-      def render_collection(view)
-        instrument(:collection, count: @collection.size) do |payload|
+      def render_collection(view, template)
+        identifier = (template && template.identifier) || @path
+        instrument(:collection, identifier: identifier, count: @collection.size) do |payload|
           return nil if @collection.blank?
 
           if @options.key?(:spacer_template)
             spacer = find_template(@options[:spacer_template], @locals.keys).render(view, @locals)
           end
 
-          cache_collection_render(payload, view) do
-            @template ? collection_with_template(view) : collection_without_template(view)
+          cache_collection_render(payload, view, template) do
+            template ? collection_with_template(view, template) : collection_without_template(view)
           end.join(spacer).html_safe
         end
       end
 
-      def render_partial(view)
-        instrument(:partial) do |payload|
+      def render_partial(view, template)
+        instrument(:partial, identifier: template.identifier) do |payload|
           locals, block = @locals, @block
           object, as = @object, @variable
 
@@ -341,12 +342,12 @@ module ActionView
           object = locals[as] if object.nil? # Respect object when object is false
           locals[as] = object if @has_object
 
-          content = @template.render(view, locals) do |*name|
+          content = template.render(view, locals) do |*name|
             view._layout_for(*name, &block)
           end
 
           content = layout.render(view, locals) { content } if layout
-          payload[:cache_hit] = view.view_renderer.cache_hits[@template.virtual_path]
+          payload[:cache_hit] = view.view_renderer.cache_hits[template.virtual_path]
           content
         end
       end
@@ -422,8 +423,8 @@ module ActionView
         @lookup_context.find_template(path, prefixes, true, locals, @details)
       end
 
-      def collection_with_template(view)
-        locals, template = @locals, @template
+      def collection_with_template(view, template)
+        locals = @locals
         as, counter, iteration = @variable, @variable_counter, @variable_iteration
 
         if layout = @options[:layout]
diff --git a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb
index 2d4a171726..b2c4fe90f4 100644
--- a/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb
+++ b/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb
@@ -11,13 +11,13 @@ module ActionView
     end
 
     private
-      def cache_collection_render(instrumentation_payload, view)
+      def cache_collection_render(instrumentation_payload, view, template)
         return yield unless @options[:cached]
 
         # Result is a hash with the key represents the
         # key used for cache lookup and the value is the item
         # on which the partial is being rendered
-        keyed_collection = collection_by_cache_keys(view)
+        keyed_collection = collection_by_cache_keys(view, template)
 
         # Pull all partials from cache
         # Result is a hash, key matches the entry in
@@ -51,21 +51,21 @@ module ActionView
         @options[:cached].respond_to?(:call)
       end
 
-      def collection_by_cache_keys(view)
+      def collection_by_cache_keys(view, template)
         seed = callable_cache_key? ? @options[:cached] : ->(i) { i }
 
         @collection.each_with_object({}) do |item, hash|
-          hash[expanded_cache_key(seed.call(item), view)] = item
+          hash[expanded_cache_key(seed.call(item), view, template)] = item
         end
       end
 
-      def expanded_cache_key(key, view)
-        key = view.combined_fragment_cache_key(view.cache_fragment_name(key, virtual_path: @template.virtual_path, digest_path: digest_path(view)))
+      def expanded_cache_key(key, view, template)
+        key = view.combined_fragment_cache_key(view.cache_fragment_name(key, virtual_path: template.virtual_path, digest_path: digest_path(view, template)))
         key.frozen? ? key.dup : key # #read_multi & #write may require mutability, Dalli 2.6.0.
       end
 
-      def digest_path(view)
-        @digest_path ||= view.digest_path_from_virtual(@template.virtual_path)
+      def digest_path(view, template)
+        @digest_path ||= view.digest_path_from_virtual(template.virtual_path)
       end
 
       # `order_by` is an enumerable object containing keys of the cache,
diff --git a/activerecord/lib/active_record/railties/collection_cache_association_loading.rb b/activerecord/lib/active_record/railties/collection_cache_association_loading.rb
index 4ef19f0929..dfaac4eefb 100644
--- a/activerecord/lib/active_record/railties/collection_cache_association_loading.rb
+++ b/activerecord/lib/active_record/railties/collection_cache_association_loading.rb
@@ -20,12 +20,12 @@ module ActiveRecord
         end
       end
 
-      def collection_without_template(_)
+      def collection_without_template(*)
         @relation.preload_associations(@collection) if @relation
         super
       end
 
-      def collection_with_template(_)
+      def collection_with_template(*)
         @relation.preload_associations(@collection) if @relation
         super
       end
-- 
cgit v1.2.3