Skip to content

Commit

Permalink
Fix Locals need to be shallow cloned.
Browse files Browse the repository at this point in the history
Resolves #20. `prop_template` prebuilds an array of options before
handling it off to handle_collection to build. I recall this being
somewhat related to faster collection rendering. Unfortunately,
the trade off is that we have to be cognizant of how we mutate deeply
nested options as those can be the same object. In this case, I missed
a required clone on locals.
  • Loading branch information
jho406 committed Jul 19, 2024
1 parent 8eaa418 commit 155d99b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/props_template/extensions/partial_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def refine_options(options, item = nil)

partial, rest = [*options[:partial]]
rest = (rest || {}).clone
locals = rest[:locals] || {}
locals = (rest[:locals] || {}).clone
rest[:locals] = locals

if item
Expand Down
16 changes: 16 additions & 0 deletions spec/extensions/partial_render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,22 @@
])
end

it "renders an array with cloned :locals" do
json = render(<<~PROPS)
emails = [
{value: '[email protected]'},
{value: '[email protected]'},
]
json.array! emails, partial: ['simple_as', locals: {}] do
end
PROPS

expect(json).to eql_json([
{foo: "[email protected]"},
{foo: "[email protected]"}
])
end

it "renders an an empty array" do
json = render(<<~PROPS)
json.array! [], {partial: 'simple'} do
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/_simple_as.json.props
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.foo simple_as[:value]

0 comments on commit 155d99b

Please sign in to comment.