-
Notifications
You must be signed in to change notification settings - Fork 1.4k
upgrade doc #1290
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
Closed
Closed
upgrade doc #1290
Changes from all commits
Commits
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 hidden or 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,93 @@ | ||
# How to upgrade from previous versions | ||
|
||
### Creating a Custom Base Adapter | ||
|
||
You can upgrade to ams ``.10`` by creating a custom Base Adapter to | ||
emulate the response that your application currently serves. | ||
|
||
###### Base Adapter | ||
|
||
A base adapter takes a ``ActiveModel::Serializer`` or | ||
``ActiveModel::Serializer::ArraySerializer`` instance, and creates a hash | ||
used for serialization in its ``serializable_hash`` method. A base adapter | ||
could look like this: | ||
|
||
```ruby | ||
class MyApp::BaseAdapter < ActiveModel::Serializer::Adapter::Base | ||
def serializable_hash(options=nil) | ||
options ||= {} | ||
|
||
if serializer.respond_to?(:each) | ||
serializable_hash_for_collection(options) | ||
else | ||
serializable_hash_for_single_resource(options) | ||
end | ||
end | ||
|
||
def serializable_hash_for_collection(options) | ||
serializer.map do |s| | ||
MyApp::BaseAdapter.new(s, instance_options).serializable_hash(options) | ||
end | ||
end | ||
|
||
def serializable_hash_for_single_resource(options) | ||
hash[:data] = serializer.attributes.merge(type: type) | ||
end | ||
|
||
private | ||
|
||
# serializer is the passed in serializer defined in the Base class. | ||
def type | ||
serializer.object.class.name.demodulize.underscore | ||
end | ||
end | ||
``` | ||
|
||
Now in your controller, you can use the adapter in the call to ``render``, | ||
e.g. | ||
|
||
```ruby | ||
class PostController < ActionController::Base | ||
def index | ||
render Post.all, adapter: MyApp::BaseAdapter | ||
end | ||
|
||
def show | ||
render Post.find(params[:id]), adapter: MyApp::BaseAdapter | ||
end | ||
end | ||
``` | ||
|
||
When testing for the JSON output in a request or controller spec, you can use | ||
something like the following in your tests: | ||
|
||
```ruby | ||
let(:post) { Post.create! } | ||
let(:get_json) do | ||
MyApp::BaseAdapter.new(PostSerializer.new(post)).to_json | ||
end | ||
|
||
let(:index_json) do | ||
MyApp::CollectionAdapter.new( | ||
ActiveModel::Serializer::ArraySerializer.new( | ||
[post] | ||
) | ||
).to_json | ||
end | ||
|
||
RSpec.describe PostController do | ||
it 'returns the expected json for index' do | ||
get :index, format: :json | ||
expect(response.body).to eq index_json | ||
end | ||
|
||
it 'returns the expected json for show' do | ||
get :show, id: post.id, format: :json | ||
expect(response.body).to eq get_json | ||
end | ||
end | ||
``` | ||
|
||
> After you upgrade to AMS ``.10``, you can now use the recommended JSON API | ||
> serialization for new endpoints, and if you choose, convert existing | ||
> endpoints to the new standard one by one. |
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.
typo:
;:
. why not try adding this code with tests?I'm thinking
And test like the other serializers and adapters
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.
Though after clicking comment, I realized the options definitions are wrong, should be:
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.
@rails-api/ams thoughts on naming, folder etc?
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.
anything specific? looks ok to me :-\ (except for the crazy long asserts)
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 API for the serializer itself changes (namely the
root
option). How should I address that?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:
sorry I must be blind, I don't see the typo.
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.
Code blind
Semicolon in place of colon
Way i wrote was ambiguous
B mobile phone