You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -173,7 +174,7 @@ By default, attributes are read directly from the model property of the same nam
173
174
```ruby
174
175
classMovieSerializer
175
176
includeFastJsonapi::ObjectSerializer
176
-
177
+
177
178
attribute :name
178
179
end
179
180
```
@@ -183,9 +184,9 @@ Custom attributes that must be serialized but do not exist on the model can be d
183
184
```ruby
184
185
classMovieSerializer
185
186
includeFastJsonapi::ObjectSerializer
186
-
187
+
187
188
attributes :name, :year
188
-
189
+
189
190
attribute :name_with_yeardo |object|
190
191
"#{object.name} (#{object.year})"
191
192
end
@@ -197,33 +198,69 @@ The block syntax can also be used to override the property on the object:
197
198
```ruby
198
199
classMovieSerializer
199
200
includeFastJsonapi::ObjectSerializer
200
-
201
+
201
202
attribute :namedo |object|
202
203
"#{object.name} Part 2"
203
204
end
204
205
end
205
206
```
206
207
208
+
### Links Per Object
209
+
Links are defined in FastJsonapi using the `link` method. By default, link are read directly from the model property of the same name.In this example, `public_url` is expected to be a property of the object being serialized.
210
+
211
+
You can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.
212
+
213
+
You can also use a block to define a url as shown in `custom_url`. You can access params in these blocks as well as shown in `personalized_url`
Custom attributes and relationships that only receive the resource are still possible by defining
309
+
the block to only receive one argument.
310
+
237
311
### Customizable Options
238
312
239
313
Option | Purpose | Example
240
314
------------ | ------------- | -------------
241
315
set_type | Type name of Object | ```set_type :movie ```
242
316
set_id | ID of Object | ```set_id :owner_id ```
243
-
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours```
317
+
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds```
244
318
id_method_name | Set custom method name to get ID of an object | ```has_many :locations, id_method_name: :place_ids ```
245
319
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
246
320
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
247
-
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor```
321
+
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor``` or ```has_many :actors, serializer: MyApp::Api::V1::ActorSerializer```
322
+
polymorphic | Allows different record types for a polymorphic association | ```has_many :targets, polymorphic: true```
323
+
polymorphic | Sets custom record types for each object class in a polymorphic association | ```has_many :targets, polymorphic: { Person => :person, Group => :group }```
0 commit comments