Skip to content

Commit

Permalink
Add documentation on how to use helper methods in serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
manojmj92 authored and shishirmk committed Mar 29, 2019
1 parent a160d67 commit e0228da
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Fast JSON API serialized 250 records in 3.01 ms
* [Conditional Attributes](#conditional-attributes)
* [Conditional Relationships](#conditional-relationships)
* [Sparse Fieldsets](#sparse-fieldsets)
* [Using helper methods](#using-helper-methods)
* [Contributing](#contributing)


Expand Down Expand Up @@ -449,6 +450,68 @@ serializer = MovieSerializer.new(movie, { fields: { movie: [:name] } })
serializer.serializable_hash
```

### Using helper methods

You can mix-in code from another ruby module into your serializer class to reuse functions across your app.

Since a serializer is evaluated in a the context of a `class` rather than an `instance` of a class, you need to make sure that your methods act as `class` methods when mixed in.


##### Using ActiveSupport::Concern

``` ruby

module AvatarHelper
extend ActiveSupport::Concern

class_methods do
def avatar_url(user)
user.image.url
end
end
end

class UserSerializer
include FastJsonapi::ObjectSerializer

include AvatarHelper # mixes in your helper method as class method

set_type :user

attributes :name, :email

attribute :avatar do |user|
avatar_url(user)
end
end

```

##### Using Plain Old Ruby

``` ruby
module AvatarHelper
def avatar_url(user)
user.image.url
end
end

class UserSerializer
include FastJsonapi::ObjectSerializer

extend AvatarHelper # mixes in your helper method as class method

set_type :user

attributes :name, :email

attribute :avatar do |user|
avatar_url(user)
end
end

```

### Customizable Options

Option | Purpose | Example
Expand Down

0 comments on commit e0228da

Please sign in to comment.