From e98b51300ab1c58b790cbaf9a27bd277184579ac Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Jan 2019 14:22:32 -0800 Subject: Remove `@view` instance variable from the partial renderer Similar to 1853b0d0abf87dfdd4c3a277c3badb17ca19652e --- .../lib/action_view/renderer/partial_renderer.rb | 33 +++++++++++----------- .../partial_renderer/collection_caching.rb | 16 +++++------ 2 files changed, 24 insertions(+), 25 deletions(-) (limited to 'actionview/lib/action_view') diff --git a/actionview/lib/action_view/renderer/partial_renderer.rb b/actionview/lib/action_view/renderer/partial_renderer.rb index f175c30aa1..42c6a97682 100644 --- a/actionview/lib/action_view/renderer/partial_renderer.rb +++ b/actionview/lib/action_view/renderer/partial_renderer.rb @@ -307,31 +307,31 @@ module ActionView end if @collection - render_collection + render_collection(context) else - render_partial + render_partial(context) end end private - def render_collection + def render_collection(view) instrument(:collection, 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) + spacer = find_template(@options[:spacer_template], @locals.keys).render(view, @locals) end - cache_collection_render(payload) do - @template ? collection_with_template : collection_without_template + cache_collection_render(payload, view) do + @template ? collection_with_template(view) : collection_without_template(view) end.join(spacer).html_safe end end - def render_partial + def render_partial(view) instrument(:partial) do |payload| - view, locals, block = @view, @locals, @block + locals, block = @locals, @block object, as = @object, @variable if !block && (layout = @options[:layout]) @@ -359,7 +359,6 @@ module ActionView # set to that string. Otherwise, the +options[:partial]+ object must # respond to +to_partial_path+ in order to setup the path. def setup(context, options, block) - @view = context @options = options @block = block @@ -381,10 +380,10 @@ module ActionView @collection = collection_from_object || collection_from_options if @collection - paths = @collection_data = @collection.map { |o| partial_path(o) } + paths = @collection_data = @collection.map { |o| partial_path(o, context) } @path = paths.uniq.one? ? paths.first : nil else - @path = partial_path + @path = partial_path(@object, context) end end @@ -423,8 +422,8 @@ module ActionView @lookup_context.find_template(path, prefixes, true, locals, @details) end - def collection_with_template - view, locals, template = @view, @locals, @template + def collection_with_template(view) + locals, template = @locals, @template as, counter, iteration = @variable, @variable_counter, @variable_iteration if layout = @options[:layout] @@ -445,8 +444,8 @@ module ActionView end end - def collection_without_template - view, locals, collection_data = @view, @locals, @collection_data + def collection_without_template(view) + locals, collection_data = @locals, @collection_data cache = {} keys = @locals.keys @@ -474,7 +473,7 @@ module ActionView # # If +prefix_partial_path_with_controller_namespace+ is true, then this # method will prefix the partial paths with a namespace. - def partial_path(object = @object) + def partial_path(object, view) object = object.to_model if object.respond_to?(:to_model) path = if object.respond_to?(:to_partial_path) @@ -483,7 +482,7 @@ module ActionView raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.") end - if @view.prefix_partial_path_with_controller_namespace + if view.prefix_partial_path_with_controller_namespace prefixed_partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup) else path 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 5aa6f77902..2d4a171726 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) + def cache_collection_render(instrumentation_payload, view) 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 + keyed_collection = collection_by_cache_keys(view) # 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 + def collection_by_cache_keys(view) seed = callable_cache_key? ? @options[:cached] : ->(i) { i } @collection.each_with_object({}) do |item, hash| - hash[expanded_cache_key(seed.call(item))] = item + hash[expanded_cache_key(seed.call(item), view)] = item end end - def expanded_cache_key(key) - key = @view.combined_fragment_cache_key(@view.cache_fragment_name(key, virtual_path: @template.virtual_path, digest_path: digest_path)) + 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))) key.frozen? ? key.dup : key # #read_multi & #write may require mutability, Dalli 2.6.0. end - def digest_path - @digest_path ||= @view.digest_path_from_virtual(@template.virtual_path) + def digest_path(view) + @digest_path ||= view.digest_path_from_virtual(@template.virtual_path) end # `order_by` is an enumerable object containing keys of the cache, -- cgit v1.2.3