-
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
1506 document passing arbitrary options to serializer #1545
Merged
bf4
merged 7 commits into
rails-api:master
from
hiimtaylorjones:1506-document-passing-arbitrary-options-to-serializer
Mar 7, 2016
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5587699
rough draft
hiimtaylorjones 6a8da1f
adding ruby tag to code blocks
hiimtaylorjones e9f2596
addressing pull request comments
hiimtaylorjones da55cd1
addressing comments / concerns
hiimtaylorjones 32c6bcc
edits and rearranging logic
hiimtaylorjones 60a207b
wrong link
hiimtaylorjones 48f8a89
a little bit more trimming
hiimtaylorjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
[Back to Guides](../README.md) | ||
|
||
# Passing Arbitrary Options To A Serializer | ||
|
||
Let's say you have a basic Post Controller: | ||
|
||
```ruby | ||
class PostController < ApplicationController | ||
def dashboard | ||
render json: @posts | ||
end | ||
end | ||
``` | ||
|
||
Odds are, your serializer will look something like this: | ||
|
||
```ruby | ||
class PostSerializer < ActiveModel::Serializer | ||
attributes :id, :title, :body | ||
end | ||
``` | ||
|
||
This works all fine and well, but maybe you passing in some arbitrary options | ||
into the serializer. These options can be anything that isn't already reserved for use by | ||
ActiveModelSerializers' adapter options. | ||
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. should link to 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. Perhaps we can simplify the above to
Docs for serialization_scope are stuck in #1252 |
||
|
||
Here's an example: | ||
|
||
```ruby | ||
# posts_controller.rb | ||
class PostController < ApplicationController | ||
def dashboard | ||
render json: @posts, user_id: 12 | ||
end | ||
end | ||
|
||
# post_serializer.rb | ||
class PostSerializer < ActiveModel::Serializer | ||
attributes :id, :title, :body | ||
|
||
def comments_by_me | ||
Comments.where(user_id: instance_options[:user_id], post_id: object.id) | ||
end | ||
end | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
wondering if this should be in
howto/
. I'm thinkingserializers.md#other
(instance_options) andrendering.md#adapter_opts
(passing arbitrary options) ingeneral/
should reference this file located inhowto/
.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.
Yeah I think this should be under the How to 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.
I agree with that as well. on it.