Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def set!(name, object = BLANK, *args)
private

def _render_partial_with_options(options)
options.reverse_merge! locals: {}
options.reverse_merge! locals: options.except(:partial, :as, :collection)
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
as = options[:as]

Expand Down
26 changes: 21 additions & 5 deletions test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(id, name)
ActionView::Template.register_template_handler :jbuilder, JbuilderHandler

PARTIALS = {
"_partial.json.jbuilder" => "foo ||= 'hello'; json.content foo",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed to be able to reproduce the undefined method error. This caused me to add

-      json.partial! "partial"
+      json.partial! "partial", foo: 'hello'

Happy to discuss if we'd rather go back to defining its own partial.

"_partial.json.jbuilder" => "json.content foo",
"_blog_post.json.jbuilder" => BLOG_POST_PARTIAL,
"racers/_racer.json.jbuilder" => RACER_PARTIAL,
"_collection.json.jbuilder" => COLLECTION_PARTIAL
Expand Down Expand Up @@ -123,23 +123,39 @@ def assert_collection_rendered(result, context = nil)

test "partial! renders partial" do
result = jbuild(<<-JBUILDER)
json.partial! "partial"
json.partial! "partial", foo: 'hello'
JBUILDER

assert_equal "hello", result["content"]
end

test "partial! + locals via :locals option" do
test "partial! + locals without :partial key with :locals key" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't mind some help making these descriptions less bulky.

result = jbuild(<<-JBUILDER)
json.partial! "partial", locals: { foo: "howdy" }
JBUILDER

assert_equal "howdy", result["content"]
end

test "partial! + locals without :locals key" do
test "partial! + locals with :partial key with :locals key" do
result = jbuild(<<-JBUILDER)
json.partial! "partial", foo: "goodbye"
json.partial! partial: "partial", locals: { foo: "goodbye" }
JBUILDER

assert_equal "goodbye", result["content"]
end

test "partial! + locals without :partial key without :locals key" do
result = jbuild(<<-JBUILDER)
json.partial! "partial", foo: "howdy"
JBUILDER

assert_equal "howdy", result["content"]
end

test "partial! + locals with :partial key without :locals key" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the spec that currently would fail with the above

-  "_partial.json.jbuilder"  => "foo ||= 'hello'; json.content foo",
+  "_partial.json.jbuilder"  => "json.content foo",

change.

result = jbuild(<<-JBUILDER)
json.partial! partial: "partial", foo: "goodbye"
JBUILDER

assert_equal "goodbye", result["content"]
Expand Down