-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Outside Controller Documentation Use #1083
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
Changes from all commits
d1e46ec
ba84653
0167dc4
39a7610
f7171c6
3597db0
53a2a0d
4cf0f9a
74dc33b
1cdb32d
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,42 @@ | ||||||||||||||||||||||||||||||||||||||||||
## Using AMS Outside Of A Controller | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
### Serializing a resource | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
In AMS versions 0.10 or later, serializing resources outside of the controller context is fairly simple: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
```ruby | ||||||||||||||||||||||||||||||||||||||||||
# Create our resource | ||||||||||||||||||||||||||||||||||||||||||
post = Post.create(title: "Sample post", body: "I love Active Model Serializers!") | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
# Optional options parameters | ||||||||||||||||||||||||||||||||||||||||||
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. TODO (in future PRs):
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. Agreed. |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
# Create a serializable resource instance | ||||||||||||||||||||||||||||||||||||||||||
serializable_resource = ActiveModel::SerializableResource.new(post, options) | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
# Convert your resource into json | ||||||||||||||||||||||||||||||||||||||||||
model_json = serializable_resource.as_json | ||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
### Retrieving a Resource's Active Model Serializer | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
If you want to retrieve a serializer for a specific resource, you can do the following: | ||||||||||||||||||||||||||||||||||||||||||
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'm not sure what the use case is, right now, for getting a serializer class or instance. The only thing I can think of is to test that the serializer works as expected, which still requires and adapter... If you can think of one, I think it would be good to add 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. Besides that, I'm 💯 LGTM 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'm not really sure. I mainly added this because it was one of the requests of the initial issue. Still might be worth having for the moment. |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
```ruby | ||||||||||||||||||||||||||||||||||||||||||
# Create our resource | ||||||||||||||||||||||||||||||||||||||||||
post = Post.create(title: "Another Example", body: "So much fun.") | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
# Optional options parameters | ||||||||||||||||||||||||||||||||||||||||||
options = {} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
# Retrieve the default serializer for posts | ||||||||||||||||||||||||||||||||||||||||||
serializer = ActiveModel::Serializer.serializer_for(post, 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. Great job here! you could also call 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. Thanks! I'll be sure to include that option as well! 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. Just to clarify, this is the serializer class i.e. ref: active_model_serializers/lib/active_model/serializable_resource.rb Lines 46 to 65 in 64168cb
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. Agreed, the comment "Create the serializer" might be misleading. What about "Retrieve the default serializer for |
||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
You could also retrieve the serializer via: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
```ruby | ||||||||||||||||||||||||||||||||||||||||||
ActiveModel::SerializableResource.new(post, options).serializer | ||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
Both approaches will return an instance, if any, of the resource's serializer. |
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.
TODO (in future PRs):
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.
(thumbsup)