josevalim/active_model_serializers — pull request #123 by dgeb (2012-08-29 15:14:09+00:00)
Title: Conditional includes (closes #111)

As discussed in #111, this pull request provides both high and low level implementations to conditionally include attributes and associations.

As @wycats requested, `include_associations!` can now be simply overridden as follows:

```ruby
def include_associations!
include! :comments
include! :user if scope.admin?
end
```

In addition, the inclusion of individual attributes and associations can be conditionally controlled by overriding `include_[attribute_or_association_name]?`. For example:

```ruby
class PersonSerializer < ActiveModel::Serializer
attributes :name, :secret
has_many :comments
has_many :posts

def include_secret?
scope.admin?
end

def include_posts?
scope.author?
end
end
```