Skip to content

Commit

Permalink
partial! accepts locals option via :locals key now
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Feb 23, 2015
1 parent 5080d61 commit 4c15a08
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 4c15a08

Please sign in to comment.