-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fields doc #1808
Fields doc #1808
Changes from 1 commit
d56c6c5
6e6b6f7
ac9cdbf
a870649
9915190
98a162d
a733190
7df9cf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[Back to Guides](../README.md) | ||
|
||
# Fields | ||
|
||
If for any reason, you need to restrict the fields returned, you should use `fields` option. | ||
|
||
For example, if you have a serializer like this | ||
|
||
```ruby | ||
class UserSerializer < ActiveModel::Serializer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should begin recommending |
||
attributes :access_token, :first_name, :last_name | ||
end | ||
``` | ||
|
||
and in a specific controller, you want to return `access_token` only, `fields` will help you: | ||
|
||
```ruby | ||
class AnonymousController < ApplicationController | ||
def create | ||
render json: User.create(activation_state: 'anonymous'), fields: [:access_token], status: 201 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for the other comment. This snippet is only valid for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def create
if user = User.create(activation_state: 'anonymous')
# using the :json adapter
render json: user, fields: [:access_token], adapter: :json, status: :created
# using the :json_api adapter
render json: user, fields: { users: [:access_token] }, adapter: :json_api, status: :created
# using the registered jsonapi renderer
render jsonapi: user, fields: { users: [:access_token] }, status: :created @groyoh question: when I serialize a collection with JSONAPI I might have render_options[:fields] = {items: [:id, :title, :description], locations: [:name]}
render_options[:include] = [:location] but I'm not sure how this would look with the :json adapter (lazy me did not review code or check for tests) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bf4 There is no way of doing that. The attributes and json adapter only allow to specify fields for the first level. |
||
end | ||
end | ||
``` | ||
|
||
Note that this is only valid for the `json` and `attributes` adapter. For the `json_api` adapter, you would use | ||
|
||
```ruby | ||
render json: @user, fields: { users: [:access_token] } | ||
``` | ||
|
||
Where `users` is the JSONAPI type. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,21 @@ See [ARCHITECTURE](../ARCHITECTURE.md) for more information. | |
|
||
#### fields | ||
|
||
PR please :) | ||
If you are using `json` or `attributes` adapter | ||
```ruby | ||
render json: @user, fields: [:access_token] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only valid for the render json: @user, fields: { users: [:access_token] } Where Could you add the missing code snippet with a brief description (stating for which adapter each snippet is valid) or add a description above your code that states that it only valid for |
||
``` | ||
|
||
or if you your adapter is `json_api`, then you should change as below | ||
|
||
```ruby | ||
render json: @user, fields: { users: [:access_token] } | ||
``` | ||
|
||
where `users` is your JSONAPI type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. link to Fields doc is sufficient |
||
|
||
|
||
See [Fields](fields.md) for more information. | ||
|
||
#### adapter | ||
|
||
|
@@ -83,7 +97,7 @@ PR please :) | |
|
||
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower``` | ||
|
||
See [Key Transforms](key_transforms.md) for more informaiton. | ||
See [Key Transforms](key_transforms.md) for more information. | ||
|
||
#### meta | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to go in the 'unreleased' section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 isn't it already the case? Or was it updated already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebase against master will show this is not in 0.10.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh true!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luizkowalski could you rebased and update the changelog please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to rebase from master but it says its up to date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, I didn't understood what should be changed on changelog, sorry. this is becoming confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest master has a v0.10.1 section. The version of the CHANGELOG in your PR has the newest release section as
v0.10.0 (2016-05-17)
. I believe other maintainers are asking that you rebase from rails-api/active_model_serializers master to get the latest CHANGELOG and ensure your CHANGELOG entry remains under themaster (unreleased)
section.