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
Copy file name to clipboardExpand all lines: README.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -330,6 +330,49 @@ class AuthorResource < JSONAPI::Resource
330
330
end
331
331
```
332
332
333
+
#### Callbacks
334
+
335
+
`ActiveSupport::Callbacks` is used to provide callback functionality, so the behavior is very similar to what you may be used to from `ActiveRecord`.
336
+
337
+
For example, you might use a callback to perform authorization on your resource before an action.
338
+
339
+
```ruby
340
+
classBaseResource < JSONAPI::Resource
341
+
before_create :authorize_create
342
+
343
+
defauthorize_create
344
+
# ...
345
+
end
346
+
end
347
+
```
348
+
349
+
The types of supported callbacks are:
350
+
-`before`
351
+
-`after`
352
+
-`around`
353
+
354
+
##### `JSONAPI::Resource` Callbacks
355
+
356
+
Callbacks can be defined for the following `JSONAPI::Resource` events:
357
+
358
+
-`:create`
359
+
-`:update`
360
+
-`:remove`
361
+
-`:save`
362
+
-`:create_has_many_link`
363
+
-`:replace_has_many_links`
364
+
-`:create_has_one_link`
365
+
-`:replace_has_one_link`
366
+
-`:remove_has_many_link`
367
+
-`:remove_has_one_link`
368
+
-`:replace_fields`
369
+
370
+
##### `JSONAPI::OperationsProcessor` Callbacks
371
+
372
+
Callbacks can also be defined for `JSONAPI::OperationsProcessor` events:
373
+
-`:operations`: The set of operations.
374
+
-`:operation`: The individual operations.
375
+
333
376
### Controllers
334
377
335
378
`JSONAPI::Resources` provides a class, `ResourceController`, that can be used as the base class for your controllers. `ResourceController` supports `index`, `show`, `create`, `update`, and `destroy` methods. Just deriving your controller from `ResourceController` will give you a fully functional controller.
0 commit comments