Skip to content

Commit fa41aeb

Browse files
committed
Merge remote-tracking branch 'origin/callback_rework' into callback_rework
2 parents 1dd728a + c25e754 commit fa41aeb

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,49 @@ class AuthorResource < JSONAPI::Resource
330330
end
331331
```
332332

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+
class BaseResource < JSONAPI::Resource
341+
before_create :authorize_create
342+
343+
def authorize_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+
333376
### Controllers
334377

335378
`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

Comments
 (0)