diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-16 14:28:39 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-16 14:54:22 -0300 |
commit | 9290fc5ce213836f666a97e0e458d98e69a920ac (patch) | |
tree | 613d7a4918cd94311aaae0cecf82d48c217c7c51 /actionview/lib/action_view | |
parent | 9830ebbeaf6f7faead9b4503ed9c9ab0a327df9a (diff) | |
download | rails-9290fc5ce213836f666a97e0e458d98e69a920ac.tar.gz rails-9290fc5ce213836f666a97e0e458d98e69a920ac.tar.bz2 rails-9290fc5ce213836f666a97e0e458d98e69a920ac.zip |
Build only one PartialIteration object for loop
Diffstat (limited to 'actionview/lib/action_view')
-rw-r--r-- | actionview/lib/action_view/renderer/partial_renderer.rb | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/actionview/lib/action_view/renderer/partial_renderer.rb b/actionview/lib/action_view/renderer/partial_renderer.rb index 6b3c49aa79..bc0215b6c6 100644 --- a/actionview/lib/action_view/renderer/partial_renderer.rb +++ b/actionview/lib/action_view/renderer/partial_renderer.rb @@ -4,9 +4,9 @@ module ActionView class PartialIteration # :nodoc: attr_reader :size, :index - def initialize(size, index) + def initialize(size) @size = size - @index = index + @index = 0 end def first? @@ -16,6 +16,10 @@ module ActionView def last? index == size - 1 end + + def iterate! + @index += 1 + end end # = Action View Partials @@ -412,16 +416,16 @@ module ActionView layout = find_template(layout, @template_keys) end - index = -1 - @collection.map do |object| - index += 1 + partial_interation = PartialIteration.new(@collection.size) + locals[iteration] = partial_interation + @collection.map do |object| locals[as] = object - locals[counter] = index - locals[iteration] = PartialIteration.new(@collection.size, index) + locals[counter] = partial_interation.index content = template.render(view, locals) content = layout.render(view, locals) { content } if layout + partial_interation.iterate! content end end @@ -431,17 +435,20 @@ module ActionView cache = {} keys = @locals.keys - index = -1 + partial_interation = PartialIteration.new(@collection.size) + @collection.map do |object| - index += 1 + index = partial_interation.index path, as, counter, iteration = collection_data[index] locals[as] = object locals[counter] = index - locals[iteration] = PartialIteration.new(@collection.size, index) + locals[iteration] = partial_interation template = (cache[path] ||= find_template(path, keys + [as, counter])) - template.render(view, locals) + content = template.render(view, locals) + partial_interation.iterate! + content end end |