-
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
FlattenJson adapter no longer inherits Json adapter, renamed to Attributes #1117
Changes from all commits
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ AMS does this through two components: **serializers** and **adapters**. | |
Serializers describe _which_ attributes and relationships should be serialized. | ||
Adapters describe _how_ attributes and relationships should be serialized. | ||
|
||
By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format). | ||
By default AMS will use the **Attributes Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format). | ||
Check how to change the adapter in the sections bellow. | ||
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. One thing that I don't like about calling the adapter 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. yup, I'm not sure about the name as well, but have no suggestions so far 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.
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.
|
||
|
||
# RELEASE CANDIDATE, PLEASE READ | ||
|
@@ -66,7 +66,7 @@ ActiveModel::Serializer.config.adapter = :json_api | |
You won't need to implement an adapter unless you wish to use a new format or | ||
media type with AMS. | ||
|
||
If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson: | ||
If you want to have a root key on your responses you should use the Json adapter, instead of the default Attributes: | ||
|
||
```ruby | ||
ActiveModel::Serializer.config.adapter = :json | ||
|
@@ -137,7 +137,7 @@ render json: @post, meta: { total: 10 }, meta_key: "custom_meta" | |
``` | ||
|
||
`meta` will only be included in your response if you are using an Adapter that supports `root`, | ||
as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`. | ||
as JsonAPI and Json adapters, the default adapter (Attributes) doesn't have `root`. | ||
|
||
### Using a serializer without `render` | ||
|
||
|
@@ -190,7 +190,7 @@ end | |
|
||
### Built in Adapters | ||
|
||
#### FlattenJSON | ||
#### Attributes | ||
|
||
It's the default adapter, it generates a json response without a root key. | ||
Doesn't follow any specifc convention. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Adapter | ||
def serializable_hash(options = nil) | ||
options ||= {} | ||
if serializer.respond_to?(:each) | ||
result = serializer.map { |s| Attributes.new(s).serializable_hash(options) } | ||
else | ||
hash = {} | ||
|
||
core = cache_check(serializer) do | ||
serializer.attributes(options) | ||
end | ||
|
||
serializer.associations.each do |association| | ||
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 see that all this came from the json adapter. it'll be interesting to see how this plays in to the nested associations stuff. |
||
serializer = association.serializer | ||
association_options = association.options | ||
|
||
if serializer.respond_to?(:each) | ||
array_serializer = serializer | ||
hash[association.key] = array_serializer.map do |item| | ||
cache_check(item) do | ||
item.attributes(association_options) | ||
end | ||
end | ||
else | ||
hash[association.key] = | ||
if serializer && serializer.object | ||
cache_check(serializer) do | ||
serializer.attributes(options) | ||
end | ||
elsif association_options[:virtual_value] | ||
association_options[:virtual_value] | ||
end | ||
end | ||
end | ||
result = core.merge hash | ||
end | ||
result | ||
end | ||
|
||
def fragment_cache(cached_hash, non_cached_hash) | ||
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash) | ||
end | ||
|
||
private | ||
|
||
# no-op: Attributes adapter does not include meta data, because it does not support root. | ||
def include_meta(json) | ||
json | ||
end | ||
end |
This file was deleted.
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.
reminder to my self to note that one literally cannot use a Serializer alone to get a hash or JSON. It is the job of the adapter to compose the attributes-to-be-serialized of each object into the hash or JSON document.