Could this RABL output be improved? #256

Closed
byalextran opened this Issue Jun 1, 2012 · 1 comment

Comments

Projects
None yet
2 participants

Woohoo! After many, many hours of trial and error, reading through the wiki/documentation and issues posted here, I finally wrangled RABL to output my desired JSON response.

The question I had was for the more experienced RABL users. Could I improve this implementation at all? And how could I re-use it across other collections?

Here's the RABL code and the resulting output.

object false

child Object.new => :users do
  node :count do
    @users.size
  end

  child @users => :items do
    attributes :first_name, :last_name, :email
  end
end
{
  "users": {
      "count": 2,
      "items": [
          {
              "first_name": "Arnaldo",
              "last_name": "Bernier",
              "email": "arnaldo.bernier@medhurst.biz"
          },
          {
              "first_name": "Juwan",
              "last_name": "Walsh",
              "email": "juwan.walsh@medhurst.biz"
          }
      ]
  }
}

Also, to get this to work properly I had to add config.include_json_root = false to the rabl_init.rb file.

So the goal here is a more generic method of outputting collections.

The type of collection is the root node (e.g., "users"), which then has a "count" node with the number of objects in the collection and an "items" array that contains the attributes for each of the objects in the collection.

So two questions.

  1. Could this be improved?
  2. How could I make it re-usable for other collections in other templates? For example, how could I use this template, but pass in the collection (users, events, etc.) and then have the template output the appropriate attributes for that collection.

Thanks for any input!

-Alex

Collaborator

databyte commented Jun 1, 2012

We're actually doing this in #216

It'll help with the layout of arbitrary nodes with multiple objects/collections and maybe we can also address include_json_root on a case-by-case basis within the new DSL if you don't always need to include/exclude it.

So it's a work in progress and I hope to make more progress on it with @nesquena soon.

Check that out and throw any notes you want over there.

databyte closed this Jun 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment