Skip to content

Conversation

@ienders
Copy link
Contributor

@ienders ienders commented Oct 30, 2024

This enables Hash Shorthand / punning on your final template statement (which I think is a good thing).

This was a somewhat tricky error to track down -- but this cropped up for us as I was adding Sorbet strict typing to our view layer. An example repro on Ruby 3.1+ would be:

example/test.json.jbuilder:

foobar = "hello" json.partial! "example/partial", foobar: 

And:

example/_partial.json.jbuilder:

json.baz foobar 

This would result in {"baz": "{}"} rather than {"baz": "hello" }.

Ultimately a Jbuilder template compiles to something like:

 def __source_json_jbuilder___2759381185109067391_7700(local_assigns, output_buffer) @virtual_path = "source";;__already_defined = defined?(json); json||=JbuilderTemplate.new(self); content = "hello" json.partial! "partial", content: json.target! unless (__already_defined && __already_defined != "method") end 

In cases where the final statement could be construed as still open (spanning newlines), this string munging can take the result of the json.target! and pull it into local_assigns.

@rafaelfranca rafaelfranca merged commit d4c8827 into rails:main Apr 4, 2025
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants