-
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
Refactor JSONAPI adapter #1103
Refactor JSONAPI adapter #1103
Conversation
object.class.model_name.singular | ||
end | ||
end | ||
|
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.
These two methods clearly did not belong there, as they are JSONAPI specific.
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'm happy we finally did it!
6fdd377
to
549e52c
Compare
549e52c
to
c4faafd
Compare
if serializer.respond_to?(:each) | ||
serializer.map { |s| resource_identifier(s) } | ||
else | ||
if options[:virtual_value] |
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.
After giving it a thought, why would anyone ever use a virtual value for a relationship with json_api? The only value this can take is nil
or a Resource Identifier/array of Resource Identifiers, which means the virtual attribute has to be either of those, which means the use case is something like:
PostSerializer < ActiveModel::Serializer
attributes :id, :name
belongs_to :author
def author
{ type: 'authors', id: '3' }
end
end
which in turn implies that one can't really include the corresponding resource.
Hence, I vote for removing the virtual values for relationships with json_api adapter.
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.
Maybe we could do something similar to JsonAdapter, that if found a instance of an model object it will query that object and use it as the relationship. I know that it might be an edge case, but would be nice to cover, taking into account the level o abstraction we are working with when trying to cover everyone using AMS 😁
dfa49b5
to
bae4951
Compare
5ff8288
to
f8c553a
Compare
Rebased. |
@@ -9,7 +9,13 @@ def initialize(serializer, options = {}) | |||
super | |||
@hash = { data: [] } | |||
|
|||
if fields = options.delete(:fields) | |||
@options[:include] ||= [] | |||
if @options[:include].is_a?(String) |
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.
Can you explain more about this. It's not related to type
and id
, and I also don't understood the why of String
verification + split
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.
nvm, understood 😁
LGTM, I'm merging it |
Move `id` and `json_api_type` methods from `Serializer` to `JsonApi`.
Oh, could you please @beauby open a PR with a new 'how to' article explaning how to override |
id
and json_api_type
methods from Serializer
to JsonApi
.
@joaomdmoura With this PR, |
The article size don't bother me and indeed it can even be a note, just want to make it easier to be found 😄 but I'll check it on #1122 |
id
andjson_api_type
methods fromSerializer
toJsonApi