From 8861e4882205437235db0fc633662a6d05b7a0d5 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 17 Feb 2015 22:08:13 +0900 Subject: [PATCH 1/3] Comment lies :tongue: `partial! partial: 'name', locals: { foo: 'bar' }` is in fact not supported ATM --- lib/jbuilder/jbuilder_template.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 2ffe760..b380c6f 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -17,7 +17,7 @@ def initialize(context, *args, &block) def partial!(name_or_options, locals = {}) case name_or_options when ::Hash - # partial! partial: 'name', locals: { foo: 'bar' } + # partial! partial: 'name', foo: 'bar' options = name_or_options else # partial! 'name', foo: 'bar' From 5080d61a60f59b061b4e05d391a46b58a4c1c34c Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 17 Feb 2015 22:16:32 +0900 Subject: [PATCH 2/3] A test for partial! + locals without :locals key --- test/jbuilder_template_test.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/jbuilder_template_test.rb b/test/jbuilder_template_test.rb index 14e11b8..55a8c4d 100644 --- a/test/jbuilder_template_test.rb +++ b/test/jbuilder_template_test.rb @@ -40,7 +40,7 @@ class JbuilderTemplateTest < ActionView::TestCase def partials { - '_partial.json.jbuilder' => 'json.content "hello"', + '_partial.json.jbuilder' => 'foo ||= "hello"; json.content foo', '_blog_post.json.jbuilder' => BLOG_POST_PARTIAL, '_collection.json.jbuilder' => COLLECTION_PARTIAL } @@ -110,6 +110,14 @@ def assert_collection_rendered(json, context = nil) assert_equal 'hello', MultiJson.load(json)['content'] end + test 'partial! + locals without :locals key' do + json = render_jbuilder <<-JBUILDER + json.partial! 'partial', foo: 'goodbye' + JBUILDER + + assert_equal 'goodbye', MultiJson.load(json)['content'] + end + test 'partial! renders collections' do json = render_jbuilder <<-JBUILDER json.partial! 'blog_post', :collection => BLOG_POST_COLLECTION, :as => :blog_post From 4c15a088d8ca27c70568ce2210855ddde183a5f4 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 19 Feb 2015 22:14:13 +0900 Subject: [PATCH 3/3] partial! accepts locals option via :locals key now --- README.md | 11 +++++++++++ lib/jbuilder/jbuilder_template.rb | 7 ++++++- test/jbuilder_template_test.rb | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2743b4f..74de568 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,17 @@ json.partial! partial: 'posts/post', collection: @posts, as: :post json.comments @post.comments, partial: 'comment/comment', as: :comment ``` +You can pass any objects into partial templates with or without `:locals` option. + +```ruby +json.partial! 'sub_template', locals: {user: user} + +# or + +json.partial! 'sub_template', user: user +``` + + You can explicitly make Jbuilder object return null if you want: ``` ruby diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index b380c6f..1b09a1d 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -20,8 +20,13 @@ def partial!(name_or_options, locals = {}) # partial! partial: 'name', foo: 'bar' options = name_or_options else + # partial! 'name', locals: {foo: 'bar'} + if locals.one? && (locals.keys.first == :locals) + options = locals.merge(partial: name_or_options) + else + options = { partial: name_or_options, locals: locals } + end # partial! 'name', foo: 'bar' - options = { partial: name_or_options, locals: locals } as = locals.delete(:as) options[:as] = as if as.present? options[:collection] = locals[:collection] if locals.key?(:collection) diff --git a/test/jbuilder_template_test.rb b/test/jbuilder_template_test.rb index 55a8c4d..ef7b3e0 100644 --- a/test/jbuilder_template_test.rb +++ b/test/jbuilder_template_test.rb @@ -110,6 +110,14 @@ def assert_collection_rendered(json, context = nil) assert_equal 'hello', MultiJson.load(json)['content'] end + test 'partial! + locals via :locals option' do + json = render_jbuilder <<-JBUILDER + json.partial! 'partial', locals: {foo: 'howdy'} + JBUILDER + + assert_equal 'howdy', MultiJson.load(json)['content'] + end + test 'partial! + locals without :locals key' do json = render_jbuilder <<-JBUILDER json.partial! 'partial', foo: 'goodbye'