-
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
move model_name into serializer, so it can get overwritten by serializer, similar to 'root' #1169
Conversation
…zer, similar to 'root'
There has been a lot of refactoring going on with the JsonApi adapter, and your concern about
Note: there's a PR (#1122) that somehow solves 1. and 2., but in an inconsistent way (the override works by simply defining a |
@beauby , regarding point 2) that an overridden type attribute should not be subject of inflection: I think the adapter design makes this a bit more difficult, and I can imagine situations where you'd want plural for JSON API, but singular for some legacy API. |
def object_model_name | ||
object.class.model_name | ||
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.
I like the encapsulation.. would like to think a bit about adding another method, and also the name of this method.. or should we just use serializer.json_key
and let you overwrite 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.
using the 'serializer.json_key' is what I suggested in the description of the pull request as an alternative (I had this code already done before I realised this). Using 'serializer.json_key' would probably make more sense, even though the json api adapter then needs to pluralise it.
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 IMO I don't think the serializer should have any methods on it relating to a specific 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.
👍
I worked on #1029 a bit last night and should have a new draft out today which will work better without diving into sub-hashes. It'll also allow the user to define arbitrary (and separate) transformers for the I agree with @beauby that if the user is overriding something that it should not be subject to inflection. I think the correct call here is to have two more methods on the serializer which return That would give the user the most flexibility. For the majority case, they'd just override |
As it currently stands the serializer is too coupled to the adapter and needs to be broken apart, but I think the solution above will work for now. |
@jfelchner sounds good, looking forward to seeing this in master :) |
I'm actually no quite sure why json_api uses
serializer.object.class.model_name
directly, instead ofserializer.json_key
. Thenresource_identifier_type_for
could be implemented like this:The
json_key
is further used in json api adapter, line 11: https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json_api.rb#L11 - shouldn't this be unified?