Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Fast JSON API serialized 250 records in 3.01 ms
* [Params](#params)
* [Conditional Attributes](#conditional-attributes)
* [Conditional Relationships](#conditional-relationships)
* [Conditional Relationships Subset](#conditional-relationships-subset)
* [Sparse Fieldsets](#sparse-fieldsets)
* [Using helper methods](#using-helper-methods)
* [Contributing](#contributing)
Expand Down Expand Up @@ -435,6 +436,25 @@ serializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }
serializer.serializable_hash
```

### Conditional Relationships Subset

Conditional relationships subset can be defined by passing a new `block` to implement a custom behaviour to get the related objects. The record and any params passed to the serializer are available inside the `block` as the first and second parameters, respectively.

```ruby
class MovieSerializer
include FastJsonapi::ObjectSerializer

# Only specified actors will be serialized if the :actors_id key of params is true
has_many(:actors, options: {if: Proc.new { |record| params && params[:actors_id] }}) {|record, params|
Actor.join(:movies).where(actor_id: params[:actors_id], movie_id: record.id)
}
end

# ...
serializer = MovieSerializer.new(movie, { params: { actors_id: [42,1337] }})
serializer.serializable_h
```

### Sparse Fieldsets

Attributes and relationships can be selectively returned per record type by using the `fields` option.
Expand Down