Skip to content

Commit f055bc0

Browse files
committed
Optimize the code added in fa99de0
1 parent c5537c1 commit f055bc0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

actionpack/lib/action_view/render/partials.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,40 @@ def render_collection
241241
end
242242

243243
def collection_with_template(template = @template)
244-
segments, locals, as, template = [], @locals, @options[:as] || @template.variable_name, @template
244+
segments, locals, template = [], @locals, @template
245245

246-
counter_name = template.counter_name
247-
locals[counter_name] = -1
246+
if @options[:as]
247+
as = @options[:as]
248+
counter = "#{as}_counter".to_sym
249+
else
250+
as = template.variable_name
251+
counter = template.counter_name
252+
end
253+
254+
locals[counter] = -1
248255

249256
@collection.each do |object|
250-
locals[counter_name] += 1
251-
locals["#{as.to_s}_counter".to_sym] = locals[counter_name] if as
257+
locals[counter] += 1
252258
locals[as] = object
253-
254259
segments << template.render(@view, locals)
255260
end
256261

257262
segments
258263
end
259264

260265
def collection_without_template(collection_paths = @collection_paths)
261-
segments, locals, as = [], @locals, @options[:as]
262-
index, template = -1, nil
266+
segments, locals = [], @locals
267+
index, template = -1, nil
268+
269+
if @options[:as]
270+
as = @options[:as]
271+
counter = "#{as}_counter"
272+
end
263273

264274
@collection.each_with_index do |object, i|
265275
template = find_template(collection_paths[i])
266-
locals[template.counter_name] = (index += 1)
267276
locals[as || template.variable_name] = object
277+
locals[counter || template.counter_name] = (index += 1)
268278

269279
segments << template.render(@view, locals)
270280
end

0 commit comments

Comments
 (0)