Skip to content

Commit

Permalink
Optimize the code added in fa99de0
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 15, 2010
1 parent c5537c1 commit f055bc0
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions actionpack/lib/action_view/render/partials.rb
Expand Up @@ -241,30 +241,40 @@ def render_collection
end

def collection_with_template(template = @template)
segments, locals, as, template = [], @locals, @options[:as] || @template.variable_name, @template
segments, locals, template = [], @locals, @template

counter_name = template.counter_name
locals[counter_name] = -1
if @options[:as]
as = @options[:as]
counter = "#{as}_counter".to_sym
else
as = template.variable_name
counter = template.counter_name
end

locals[counter] = -1

@collection.each do |object|
locals[counter_name] += 1
locals["#{as.to_s}_counter".to_sym] = locals[counter_name] if as
locals[counter] += 1
locals[as] = object

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

segments
end

def collection_without_template(collection_paths = @collection_paths)
segments, locals, as = [], @locals, @options[:as]
index, template = -1, nil
segments, locals = [], @locals
index, template = -1, nil

if @options[:as]
as = @options[:as]
counter = "#{as}_counter"
end

@collection.each_with_index do |object, i|
template = find_template(collection_paths[i])
locals[template.counter_name] = (index += 1)
locals[as || template.variable_name] = object
locals[counter || template.counter_name] = (index += 1)

segments << template.render(@view, locals)
end
Expand Down

0 comments on commit f055bc0

Please sign in to comment.