nesquena/rabl — Issue #174: Allow to add root node
Author: blakink
Created: 2012-02-23 13:39:55+00:00
State: open

Sometimes, I need to add nodes to root when am rendering collections or objects.. and it's always tricky.

Let's take an example. I need to render the following hash, to be able to paginate my data :

{ total: 250,
page: 3,
data: [
{ id: 1, name: "Foo", ...},
{ id: 2, name: "Bar", ...}
]
}

I searched a bit how to get such thing, and i only found the following trick :

object false
node(:total) { @items.total_count }
node(:page) { @page }

child(@items => :data) do
attributes :id, :name
end

Is it possible to add a cleaner way ?

collection @items => :data

attributes :id, :name

root_node total: @items.total_count
root_node page: @page