Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created February 14, 2012 01:15
Show Gist options
  • Save nesquena/1822267 to your computer and use it in GitHub Desktop.
Save nesquena/1822267 to your computer and use it in GitHub Desktop.
JBuilder and RABL Syntax Comparison
Jbuilder.encode do |json|
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do |json|
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format: :json)
end
if current_user.admin?
json.visitors calculate_visitors(@message)
end
json.comments @message.comments, :content, :created_at
json.attachments @message.attachments do |json, attachment|
json.filename attachment.filename
json.url url_for(attachment)
end
end
content format_content(@message.content)
created_at @message.created_at
updated_at @message.updated_at
author do
name @message.creator.name.familiar
email_address @message.creator.email_address_with_name
url url_for(@message.creator, format: :json)
end
if current_user.admin?
visitors calculate_visitors(@message)
end
comments @message.comments do |comment|
content comment.content
created_at comment.created_at
end
attachments @message.attachments do |attachment|
filename attachment.filename
url url_for(attachment)
end
object @message
node(:content) { format_content(@message.content) }
attributes :created_at, :updated_at
child @message.creator => :author do
node(:name) { |a| a.name.familiar }
node(:email_address) { |a| a.email_address_with_name }
node(:url) { |a| url_for(a, format: :json) }
end
if current_user.admin?
node(:visitors) { calculate_visitors(@message) }
end
child :comments do
attributes :content, :created_at
end
child :attachments do
attributes :filename
node(:url) { url_for(attachment) }
end
{
"content": "<p>This is <i>serious</i> monkey business",
"created_at": "2011-10-29T20:45:28-05:00",
"updated_at": "2011-10-29T20:45:28-05:00",
"author": {
"name": "David H.",
"email_address": "'David Heinemeier Hansson' <[email protected]>",
"url": "http://example.com/users/1-david.json"
},
"visitors": 15,
"comments": [
{ "content": "Hello everyone!", "created_at": "2011-10-29T20:45:28-05:00" },
{ "content": "To you my good sir!", "created_at": "2011-10-29T20:47:28-05:00" }
],
"attachment": [
{ "filename": "forecast.xls", "url": "http://example.com/downloads/forecast.xls" },
{ "filename": "presentation.pdf", "url": "http://example.com/downloads/presentation.pdf" }
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment