Skip to content

Commit 5dfeb04

Browse files
committed
Add fragment proc for list names
This allows us to identify a fragment that depends on item context in a list
1 parent c307e80 commit 5dfeb04

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/props_template/extensions/partial_renderer.rb

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ def refine_options(options, item = nil)
152152
locals[as] = item
153153

154154
if (fragment_name = rest[:fragment])
155+
if item && ::Proc === fragment_name
156+
fragment_name = fragment_name.call(item)
157+
end
158+
155159
rest[:fragment] = fragment_name.to_s
156160
end
157161
end
@@ -163,3 +167,4 @@ def refine_options(options, item = nil)
163167
end
164168
end
165169
end
170+

spec/extensions/fragments_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,40 @@
170170
]
171171
})
172172
end
173+
174+
it "renders an array of partials with fragments using a proc to derive the fragment key" do
175+
json = render(<<~PROPS)
176+
klass = Struct.new(:email, :id)
177+
178+
users = [
179+
klass.new('[email protected]', 1),
180+
klass.new('[email protected]', 2)
181+
]
182+
183+
json.data do
184+
opts = {
185+
partial: ['person', fragment: -> (item) { item.email } ]
186+
}
187+
json.array! users, opts do
188+
end
189+
end
190+
191+
json.fragments json.fragments!
192+
PROPS
193+
194+
expect(json).to eql_json({
195+
data: [
196+
{
197+
198+
},
199+
{
200+
201+
}
202+
],
203+
fragments: [
204+
{type: "[email protected]", partial: "person", path: "data.0"},
205+
{type: "[email protected]", partial: "person", path: "data.1"}
206+
]
207+
})
208+
end
173209
end

0 commit comments

Comments
 (0)