rails/jbuilder — pull request #137 by tejanium (2013-06-20 15:11:27+00:00)
Title: whitelist and blacklist keys on Jbuilder#array!

As addition to #array! method, it would be great if we can whitelist and blacklist predefined keys.

``` ruby
# @people = People.all
json.people @people, except: [:id] do |person|
json.id person.id
json.name person.name
end

# => [ { name": "David" }, { "name": "Jamie" } ]

# @people = People.all
json.people @people, only: [:id] do |person|
json.id person.id
json.name person.name
end

# => [ { "id": 1 }, { "id": 2 } ]
```

**Why?**

This similar with [Google Partial Response](https://developers.google.com/youtube/2.0/developers_guide_protocol_partial)

This enable client to manually select or deselect fields that they only need.

We can have code like this in our jbuilder file

``` ruby
# @people = People.all
json.people @people, except: params[:except].split(',') do |person|
json.id person.id
json.name person.name
json.email person.email
end

# localhost:3000/people?except=id,name
# => [ { "email": "foo@bar.com" }, { "email": "foo@baz.com" } ]
```
or

``` ruby
# @people = People.all
json.people @people, except: params[:fields].split(',') do |person|
json.id person.id
json.name person.name
json.email person.email
end

# localhost:3000/people?fields=name, email
# => [ { "name": "foo bar", "email": "foo@bar.com" }, { "name": "foo baz", "email": "foo@baz.com" } ]
```