Skip to content

Commit

Permalink
Aded docs on deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed May 4, 2019
1 parent 8795178 commit 99a2988
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -74,6 +74,7 @@ Or install it yourself as:
* [Filtering and sorting](#filtering-and-sorting)
* [Sorting using expressions](#sorting-using-expressions)
* [Pagination](#pagination)
* [Deserialization](#deserialization)

---

Expand Down Expand Up @@ -303,8 +304,38 @@ use the `jsonapi_pagination_meta` method:

{ pagination: pagination } if pagination.present?
end

```
### Deserialization

`JSONAPI::Deserialization` provides a helper to transform a `JSONAPI` document
into a flat dictionary that can be used to update an `ActiveRecord::Base` model.

Here's an example using the `jsonapi_deserialize` helper:

```ruby
class MyController < ActionController::Base
include JSONAPI::Deserialization

def update
model = MyModel.find(params[:id])

if model.update(jsonapi_deserialize(only: [:attr1, :rel_one]))
render jsonapi: model
else
render jsonapi_errors: model.errors, status: :unprocessable_entity
end
end
end
```

The `jsonapi_deserialize` helper accepts the following options:

* `only`: returns exclusively attributes/relationship data in the provided list
* `except`: returns exclusively attributes/relationship which are not in the list
* `polymorphic`: will add and detect the `_type` attribute and class to the
defined list of polymorphic relationships

## Development

After checking out the repo, run `bundle` to install dependencies.
Expand Down

0 comments on commit 99a2988

Please sign in to comment.