Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created February 2, 2011 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvandijk/807545 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/807545 to your computer and use it in GitHub Desktop.
A tokamak view with nested collections https://github.com/caelum/tokamak
collection(@activities) do |collection|
collection.members(:root => "activities") do |member, activity|
# ...
# COMMENTS
if comments = @comments.try(:[], activity.id)
member.members(:root => "comments", :collection => comments) do |member, comment|
member.values { |comment_values|
comment_values.timestamp comment.created_at.to_i
comment_values.content comment.object.content
comment_values.actor {
comment_values.type comment.actor_type.downcase
comment_values.id comment.actor_id
}
}
end
end
# LIKES
if likes = @likes.try(:[], activity.id)
member.members(:root => "likes", :collection => likes) do |member, like|
member.values { |like_values|
like_values.timestamp like.created_at.to_i
like_values.actor {
like_values.type like.actor_type.downcase
like_values.id like.actor_id
}
}
end
end
# ...
end
{ "activities":
[
{
"comments": [
{
"timestamp": 1296039053,
"content" : "Alias omnis tenetur assumenda eligendi nisi.",
"actor": {
"id": 2,
"type": "user"
}
}
],
"likes": [
{
"timestamp": 1296039053,
"actor": {
"id": 2,
"type": "user"
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment