Skip to content

Commit cd7482e

Browse files
authored
Merge pull request #613 from affinity/moberegger/fix-inline-partial-locals-be
2 parents 58283a0 + b608b17 commit cd7482e

4 files changed

Lines changed: 50 additions & 13 deletions

File tree

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ json.array! @comments do |comment|
100100
end
101101
end
102102

103-
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
103+
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Blow" }} ]
104104
```
105105

106106
## Array Attributes
@@ -209,6 +209,13 @@ the partial.
209209
json.partial! 'comments/comments', comments: @message.comments
210210
```
211211

212+
You can also render an object to a partial inline under a key.
213+
214+
```ruby
215+
json.post @post, partial: 'posts/post', as: :post
216+
# => { "post": { "title": "Hello World!", "author": { "name": "David" } } }
217+
```
218+
212219
It's also possible to render collections of partials:
213220

214221
```ruby
@@ -219,9 +226,24 @@ json.partial! 'posts/post', collection: @posts, as: :post
219226

220227
# or
221228
json.partial! partial: 'posts/post', collection: @posts, as: :post
229+
```
222230

223-
# or
231+
You can also render to a collection of partials inline under a key.
232+
233+
```ruby
224234
json.comments @post.comments, partial: 'comments/comment', as: :comment
235+
# => { "comments": [{ "content": "Hello everyone!" }, { "content": "To you my good sir!" }] }
236+
```
237+
238+
You can also provide other locals to the partial you're rendering to.
239+
240+
```ruby
241+
# Provide the `include_body` local to the partial when rendering a single object
242+
json.post @post, partial: 'posts/post', as: :post, include_body: true
243+
244+
# Provide a local to the partial when rendering a collection.
245+
# Each item in the collection will render with `include_author: true`.
246+
json.comments @post.comments, partial: 'comments/comment', as: :comment, include_author: true
225247
```
226248

227249
The `as: :some_symbol` is used with partials. It will take care of mapping the passed in object to a variable for the
@@ -321,7 +343,7 @@ This will include both records as part of the cache key and updating either of t
321343
## Formatting Keys
322344

323345
Keys can be auto formatted using `key_format!`, this can be used to convert
324-
keynames from the standard ruby_format to camelCase:
346+
key names from the standard ruby_format to camelCase:
325347

326348
```ruby
327349
json.key_format! camelize: :lower

lib/jbuilder/jbuilder_template.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,11 @@ def _render_partial_with_options(options)
175175
_array
176176
end
177177
else
178-
_render_partial options
178+
options[:locals][:json] = self
179+
@context.render options
179180
end
180181
end
181182

182-
def _render_partial(options)
183-
options[:locals][:json] = self
184-
@context.render options
185-
end
186-
187183
def _cache_fragment_for(key, options, &block)
188184
key = _cache_key(key, options)
189185
_read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block)
@@ -242,7 +238,7 @@ def _set_inline_partial(name, object, options)
242238
end
243239
else
244240
_scope do
245-
options[:locals] = { options[:as] => object }
241+
options[options[:as]] = object
246242
_render_partial_with_options options
247243
end
248244
end

test/jbuilder_template_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class JbuilderTemplateTest < ActiveSupport::TestCase
55
POST_PARTIAL = <<-JBUILDER
66
json.extract! post, :id, :body
7+
json.title post.title if local_assigns.fetch(:include_title, false)
78
json.author do
89
first_name, last_name = post.author_name.split(nil, 2)
910
json.first_name first_name
@@ -30,7 +31,7 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
3031
}
3132

3233
AUTHORS = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
33-
POSTS = (1..10).collect { |i| Post.new(i, "Post ##{i}", AUTHORS.next) }
34+
POSTS = (1..10).collect { |i| Post.new(i, "Title #{i}", "Post ##{i}", AUTHORS.next) }
3435

3536
setup { Rails.cache.clear }
3637

@@ -141,6 +142,18 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
141142
assert_equal [], render('json.array! @posts, partial: "post", as: :post', posts: nil)
142143
end
143144

145+
test "single partial under key" do
146+
result = render('json.post @post, partial: "post", as: :post', post: POSTS.first)
147+
assert_equal "Post #1", result["post"]["body"]
148+
assert_equal "Heinemeier Hansson", result["post"]["author"]["last_name"]
149+
assert_equal "David", result["post"]["author"]["first_name"]
150+
end
151+
152+
test 'single partial under key with local' do
153+
result = render('json.post @post, partial: "post", as: :post, include_title: true', post: POSTS.first)
154+
assert_equal "Title 1", result["post"]["title"]
155+
end
156+
144157
test "array of partials under key" do
145158
result = render('json.posts @posts, partial: "post", as: :post', posts: POSTS)
146159
assert_equal 10, result["posts"].count
@@ -149,13 +162,19 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
149162
assert_equal "Pavel", result["posts"][5]["author"]["first_name"]
150163
end
151164

165+
test "array of partials under key with local" do
166+
result = render('json.posts @posts, partial: "post", as: :post, include_title: true', posts: POSTS)
167+
assert_equal "Title 1", result["posts"][0]["title"]
168+
assert_equal "Title 2", result["posts"][1]["title"]
169+
end
170+
152171
test "empty array of partials under key from nil collection" do
153172
Jbuilder::CollectionRenderer.expects(:new).never
154173
result = render('json.posts @posts, partial: "post", as: :post', posts: nil)
155174
assert_equal [], result["posts"]
156175
end
157176

158-
test "empty array of partials under key from an empy collection" do
177+
test "empty array of partials under key from an empty collection" do
159178
Jbuilder::CollectionRenderer.expects(:new).never
160179
result = render('json.posts @posts, partial: "post", as: :post', posts: [])
161180
assert_equal [], result["posts"]

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class << Rails
2626

2727
Jbuilder::CollectionRenderer.collection_cache = Rails.cache
2828

29-
class Post < Struct.new(:id, :body, :author_name)
29+
class Post < Struct.new(:id, :title, :body, :author_name)
3030
def cache_key
3131
"post-#{id}"
3232
end

0 commit comments

Comments
 (0)