/
collection_renderer.rb
204 lines (162 loc) · 5.89 KB
/
collection_renderer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true
require "action_view/renderer/partial_renderer"
module ActionView
class PartialIteration
# The number of iterations that will be done by the partial.
attr_reader :size
# The current iteration of the partial.
attr_reader :index
def initialize(size)
@size = size
@index = 0
end
# Check if this is the first iteration of the partial.
def first?
index == 0
end
# Check if this is the last iteration of the partial.
def last?
index == size - 1
end
def iterate! # :nodoc:
@index += 1
end
end
class CollectionRenderer < PartialRenderer # :nodoc:
include ObjectRendering
class CollectionIterator # :nodoc:
include Enumerable
def initialize(collection)
@collection = collection
end
def each(&blk)
@collection.each(&blk)
end
def size
@collection.size
end
def length
@collection.respond_to?(:length) ? @collection.length : size
end
def preload!
# no-op
end
end
class SameCollectionIterator < CollectionIterator # :nodoc:
def initialize(collection, path, variables)
super(collection)
@path = path
@variables = variables
end
def from_collection(collection)
self.class.new(collection, @path, @variables)
end
def each_with_info
return enum_for(:each_with_info) unless block_given?
variables = [@path] + @variables
@collection.each { |o| yield(o, variables) }
end
end
class PreloadCollectionIterator < SameCollectionIterator # :nodoc:
def initialize(collection, path, variables, relation)
super(collection, path, variables)
relation.skip_preloading! unless relation.loaded?
@relation = relation
end
def from_collection(collection)
self.class.new(collection, @path, @variables, @relation)
end
def each_with_info
return super unless block_given?
preload!
super
end
def preload!
@relation.preload_associations(@collection)
end
end
class MixedCollectionIterator < CollectionIterator # :nodoc:
def initialize(collection, paths)
super(collection)
@paths = paths
end
def each_with_info
return enum_for(:each_with_info) unless block_given?
@collection.each_with_index { |o, i| yield(o, @paths[i]) }
end
end
def render_collection_with_partial(collection, partial, context, block)
iter_vars = retrieve_variable(partial)
collection = if collection.respond_to?(:preload_associations)
PreloadCollectionIterator.new(collection, partial, iter_vars, collection)
else
SameCollectionIterator.new(collection, partial, iter_vars)
end
template = find_template(partial, @locals.keys + iter_vars)
layout = if !block && (layout = @options[:layout])
find_template(layout.to_s, @locals.keys + iter_vars)
end
render_collection(collection, context, partial, template, layout, block)
end
def render_collection_derive_partial(collection, context, block)
paths = collection.map { |o| partial_path(o, context) }
if paths.uniq.length == 1
# Homogeneous
render_collection_with_partial(collection, paths.first, context, block)
else
if @options[:cached]
raise NotImplementedError, "render caching requires a template. Please specify a partial when rendering"
end
paths.map! { |path| retrieve_variable(path).unshift(path) }
collection = MixedCollectionIterator.new(collection, paths)
render_collection(collection, context, nil, nil, nil, block)
end
end
private
def retrieve_variable(path)
variable = local_variable(path)
[variable, :"#{variable}_counter", :"#{variable}_iteration"]
end
def render_collection(collection, view, path, template, layout, block)
identifier = (template && template.identifier) || path
ActiveSupport::Notifications.instrument(
"render_collection.action_view",
identifier: identifier,
layout: layout && layout.virtual_path,
count: collection.length
) do |payload|
spacer = if @options.key?(:spacer_template)
spacer_template = find_template(@options[:spacer_template], @locals.keys)
build_rendered_template(spacer_template.render(view, @locals), spacer_template)
else
RenderedTemplate::EMPTY_SPACER
end
collection_body = if template
cache_collection_render(payload, view, template, collection) do |filtered_collection|
collection_with_template(view, template, layout, filtered_collection)
end
else
collection_with_template(view, nil, layout, collection)
end
return RenderedCollection.empty(@lookup_context.formats.first) if collection_body.empty?
build_rendered_collection(collection_body, spacer)
end
end
def collection_with_template(view, template, layout, collection)
locals = @locals
cache = {}
partial_iteration = PartialIteration.new(collection.size)
collection.each_with_info.map do |object, (path, as, counter, iteration)|
index = partial_iteration.index
locals[as] = object
locals[counter] = index
locals[iteration] = partial_iteration
_template = (cache[path] ||= (template || find_template(path, @locals.keys + [as, counter, iteration])))
content = _template.render(view, locals, implicit_locals: [counter, iteration])
content = layout.render(view, locals) { content } if layout
partial_iteration.iterate!
build_rendered_template(content, _template)
end
end
end
end