README.md in jbuilder-2.1.3 vs README.md in jbuilder-2.2.0

- old
+ new

@@ -14,30 +14,30 @@ massaging giant hash structures. This is particularly helpful when the generation process is fraught with conditionals and loops. Here's a simple example: ``` ruby -Jbuilder.encode do |json| - json.content format_content(@message.content) - json.(@message, :created_at, :updated_at) +# app/views/message/show.json.jbuilder - json.author do - json.name @message.creator.name.familiar - json.email_address @message.creator.email_address_with_name - json.url url_for(@message.creator, format: :json) - end +json.content format_content(@message.content) +json.(@message, :created_at, :updated_at) - if current_user.admin? - json.visitors calculate_visitors(@message) - end +json.author do + json.name @message.creator.name.familiar + json.email_address @message.creator.email_address_with_name + json.url url_for(@message.creator, format: :json) +end - json.comments @message.comments, :content, :created_at +if current_user.admin? + json.visitors calculate_visitors(@message) +end - json.attachments @message.attachments do |attachment| - json.filename attachment.filename - json.url url_for(attachment) - end +json.comments @message.comments, :content, :created_at + +json.attachments @message.attachments do |attachment| + json.filename attachment.filename + json.url url_for(attachment) end ``` This will build the following structure: @@ -78,22 +78,29 @@ ``` Top level arrays can be handled directly. Useful for index and other collection actions. ``` ruby -# @people = People.all -json.array! @people do |person| - json.name person.name - json.age calculate_age(person.birthday) +# @comments = @post.comments + +json.array! @comments do |comment| + next if comment.marked_as_spam_by?(current_user) + + json.body comment.body + json.author do + json.first_name comment.author.first_name + json.last_name comment.author.last_name + end end -# => [ { "name": "David", "age": 32 }, { "name": "Jamie", "age": 31 } ] +# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ] ``` You can also extract attributes from array directly. ``` ruby # @people = People.all + json.array! @people, :id, :name # => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] ```