rails-api/active_model_serializers — pull request #312 by remiprev (2013-05-23 18:53:17+00:00)
Title: Add support for `omit_nil_values` option

I found myself having to deal with a large number of arbitrary attributes in a serializable object and I want to be able to omit attributes with a `nil` value.

I could achieve this by adding a custom `include_?` method for each attribute and check that it’s not `nil`, but I thought it would be nice to have a `omit_nil_values` option to send to the serializer and it would just omit any attribute that’s `nil`.

It can be used in two ways:

```ruby
# Will omit any attribute that has a `nil` value
serializer = MySerializer.new(object, omit_nil_values: true)

# Will omit only these specific attributes if they have a `nil` value
serializer = MySerializer.new(object, omit_nil_values: [:foo, :bar])
```

Let me know if it’s something that could be included in `ActiveModel::Serializer`— I know it’s quite useful for me :)