Skip to content

Commit 7b2e1b7

Browse files
Add headings to README
1 parent 9aa3dd9 commit 7b2e1b7

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

README.md

+38-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ manipulating giant hash structures. This is particularly helpful when the
55
generation process is fraught with conditionals and loops. Here's a simple
66
example:
77

8-
``` ruby
8+
```ruby
99
# app/views/messages/show.json.jbuilder
1010

1111
json.content format_content(@message.content)
@@ -31,7 +31,7 @@ end
3131

3232
This will build the following structure:
3333

34-
``` javascript
34+
```javascript
3535
{
3636
"content": "<p>This is <i>serious</i> monkey business</p>",
3737
"created_at": "2011-10-29T20:45:28-05:00",
@@ -57,20 +57,23 @@ This will build the following structure:
5757
}
5858
```
5959

60+
## Dynamically Defined Attributes
61+
6062
To define attribute and structure names dynamically, use the `set!` method:
6163

62-
``` ruby
64+
```ruby
6365
json.set! :author do
6466
json.set! :name, 'David'
6567
end
6668

6769
# => {"author": { "name": "David" }}
6870
```
6971

72+
## Merging Existing Hash or Array
7073

7174
To merge existing hash or array to current context:
7275

73-
``` ruby
76+
```ruby
7477
hash = { author: { name: "David" } }
7578
json.post do
7679
json.title "Merge HOWTO"
@@ -80,9 +83,11 @@ end
8083
# => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
8184
```
8285

83-
Top level arrays can be handled directly. Useful for index and other collection actions.
86+
## Top Level Arrays
87+
88+
Top level arrays can be handled directly. Useful for index and other collection actions.
8489

85-
``` ruby
90+
```ruby
8691
# @comments = @post.comments
8792

8893
json.array! @comments do |comment|
@@ -98,16 +103,20 @@ end
98103
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
99104
```
100105

106+
## Array Attributes
107+
101108
You can also extract attributes from array directly.
102109

103-
``` ruby
110+
```ruby
104111
# @people = People.all
105112

106113
json.array! @people, :id, :name
107114

108115
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
109116
```
110117

118+
## Plain Arrays
119+
111120
To make a plain array without keys, construct and pass in a standard Ruby array.
112121

113122
```ruby
@@ -118,6 +127,8 @@ json.people my_array
118127
# => "people": [ "David", "Jamie" ]
119128
```
120129

130+
## Child Objects
131+
121132
You don't always have or need a collection when building an array.
122133

123134
```ruby
@@ -135,9 +146,11 @@ end
135146
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
136147
```
137148

138-
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
149+
## Nested Jbuilder Objects
150+
151+
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
139152

140-
``` ruby
153+
```ruby
141154
class Person
142155
# ... Class Definition ... #
143156
def to_builder
@@ -163,11 +176,13 @@ company.to_builder.target!
163176
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
164177
```
165178

179+
## Rails Integration
180+
166181
You can either use Jbuilder stand-alone or directly as an ActionView template
167182
language. When required in Rails, you can create views à la show.json.jbuilder
168183
(the json is already yielded):
169184

170-
``` ruby
185+
```ruby
171186
# Any helpers available to views are available to the builder
172187
json.content format_content(@message.content)
173188
json.(@message, :created_at, :updated_at)
@@ -183,6 +198,8 @@ if current_user.admin?
183198
end
184199
```
185200

201+
## Partials
202+
186203
You can use partials as well. The following will render the file
187204
`views/comments/_comments.json.jbuilder`, and set a local variable
188205
`comments` with all this message's comments, which you can use inside
@@ -215,15 +232,15 @@ then the object is passed to the partial as the variable `some_symbol`.
215232
Be sure not to confuse the `as:` option to mean nesting of the partial. For example:
216233

217234
```ruby
218-
# Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
219-
# Note, `comment` attributes are "inlined".
220-
json.partial! @comment, as: :comment
235+
# Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
236+
# Note, `comment` attributes are "inlined".
237+
json.partial! @comment, as: :comment
221238
```
222239

223240
is quite different from:
224241

225242
```ruby
226-
# comment attributes are nested under a "comment" property
243+
# comment attributes are nested under a "comment" property
227244
json.comment do
228245
json.partial! "/comments/comment.json.jbuilder", comment: @comment
229246
end
@@ -239,10 +256,11 @@ json.partial! 'sub_template', locals: { user: user }
239256
json.partial! 'sub_template', user: user
240257
```
241258

259+
## Null Values
242260

243261
You can explicitly make Jbuilder object return null if you want:
244262

245-
``` ruby
263+
```ruby
246264
json.extract! @post, :id, :title, :content, :published_at
247265
json.author do
248266
if @post.anonymous?
@@ -305,7 +323,7 @@ This will include both records as part of the cache key and updating either of t
305323
Keys can be auto formatted using `key_format!`, this can be used to convert
306324
keynames from the standard ruby_format to camelCase:
307325

308-
``` ruby
326+
```ruby
309327
json.key_format! camelize: :lower
310328
json.first_name 'David'
311329

@@ -315,15 +333,15 @@ json.first_name 'David'
315333
You can set this globally with the class method `key_format` (from inside your
316334
environment.rb for example):
317335

318-
``` ruby
336+
```ruby
319337
Jbuilder.key_format camelize: :lower
320338
```
321339

322340
By default, key format is not applied to keys of hashes that are
323341
passed to methods like `set!`, `array!` or `merge!`. You can opt into
324342
deeply transforming these as well:
325343

326-
``` ruby
344+
```ruby
327345
json.key_format! camelize: :lower
328346
json.deep_format_keys!
329347
json.settings([{some_value: "abc"}])
@@ -334,7 +352,7 @@ json.settings([{some_value: "abc"}])
334352
You can set this globally with the class method `deep_format_keys` (from inside your
335353
environment.rb for example):
336354

337-
``` ruby
355+
```ruby
338356
Jbuilder.deep_format_keys true
339357
```
340358

@@ -350,4 +368,5 @@ features and discuss issues.
350368
See [CONTRIBUTING](CONTRIBUTING.md).
351369

352370
## License
371+
353372
Jbuilder is released under the [MIT License](http://www.opensource.org/licenses/MIT).

0 commit comments

Comments
 (0)