-
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
Changes from 3 commits
5587699
6a8da1f
e9f2596
da55cd1
32c6bcc
60a207b
48f8a89
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,45 @@ | ||
## 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 | ||
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.
|
||
into the serializer. Here's what you would do: | ||
|
||
### posts_controller.rb | ||
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 don't know what's your opinion on this but I usually find easier to easier to understand when having the full code snippet instead of just part of it. Also I usually tend to prefer having one snippet with comments to specify the filename instead of using h3 here. An example here would be: # post_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
has_many :comments_by_me
def comments_by_me
Comments.where(user_id: instance_options[:user_id], post_id: object.id)
end
end Maybe that's just my opinion and might not be true for everyone so let me know what you guys think. 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 really don't mind that at all. Definitely could flow a bit better. Going to do an A / B comparison and see what's up. |
||
|
||
```ruby | ||
... | ||
def dashboard | ||
render json: @posts, user_id: 12 | ||
end | ||
... | ||
``` | ||
|
||
### posts_serializer.rb | ||
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. Typo. Should be |
||
|
||
```ruby | ||
... | ||
def comments_by_me | ||
Comments.where(user_id: instance_options[:user_id], post_id: object.id) | ||
end | ||
... | ||
``` | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a line above this
[Back to Guides](../README.md)
and change the header level to h1# Passing
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.
On it.