-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Namespace separator setting #1609
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
Conversation
##### jsonapi_namespace_separator | ||
|
||
Sets separator string for namespaced models to render `type` attribute. Default value is `--`. | ||
|
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.
An alternative name or perhaps alias would be dashed_namespace_separator
(which might also make a nice PR into Rails, to make it easier to configure the replacement string from /
to --
)
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.
Not sure I understand. It's supposed to be a setting for any custom separator, not only dasherized. Why should it be called dashed_namespace_separator
? The point, that I didn't use model_name
from rails. Just plain and simple class name processing.
@youroff Awesome, thanks for this! |
Also, don't you mind if I remove this test in this PR? UPD: Disregard this. Probably type_test should be considered as integration. |
Just ran into some ambiguity in KeyTransform names. Currently it's:
First off, I suggest to rename
What do you think, guys? |
serializer.object.class.model_name.plural | ||
type = serializer.object.class.to_s.split('::') | ||
type.map! { |t| t.underscore.downcase } | ||
type = type.join ActiveModelSerializers.config.jsonapi_namespace_separator |
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 was thinking this would be a good use-case for gsub, but it looks like that is would be marginally slower and a little harder to read, so just sharing in case it's of interest
Benchmark.realtime { 1000.times { 'AdminUser::Person::THING'.gsub(/[a-zA-Z]+(::|\z)/) do |sub| sub.underscore.downcase.sub('/','--') end } }
# => 0.07714433899673168
>> Benchmark.realtime { 1000.times { 'AdminUser::Person::THING'.split('::').map! {|t| t.underscore.downcase }.join('--') } }
# => 0.07239663798827678
@youroff I think I'm ok to merge this. Do you feel done with it? |
There were some not committed additions. Please check last commit in this PR. And probably I need to rebase as there's some conflict already. |
@bf4 Yup, I'll prepare it today |
968bbf3
to
9eb2c30
Compare
documentation Type transform setting and transformer, name fixed in tests
9eb2c30
to
8555c92
Compare
Well, I think this is ready, though CI is failing, not sure why. Any hints? |
It says spec coverage is not high enough |
Oh, right, the test for transforms is definitely needed. Will add that soon. |
FYI, I think KeyTransform will soon become https://github.com/remear/active_model_serializers/blob/transforms/lib/active_model_serializers/key_transform.rb via #1645. With the changes in #1645, I'm having a hard time seeing the reason for the transform method for KeyTransform. If it's something you still want, could you share some more reasoning. |
serializer.object.class.model_name.plural | ||
type = serializer.object.class.to_s.split('::') | ||
type.map! { |t| KeyTransform.transform(t, ActiveModelSerializers.config.jsonapi_type_transform) } | ||
type = type.join ActiveModelSerializers.config.jsonapi_namespace_separator |
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.
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.
that's what i'm thinking
I agree, looks good. So should I wait until that PR merged?
So it may be good idea to underscore string first to make it suitable to cross-conversion in any direction. |
And I'm not sure what exactly is unaltered. Is there some assumption what is the default format provided by backend? Shouldn't we rely on some particular case per adaptor? Maybe custom lambda in config would do the trick for some weird cases if there are any? |
|
I'm not sure underscored is an assumption we want to make. Therefore, |
Yeah, I just meant that Rails will default to outputting snake case, which we wouldn't alter |
Ok, then if we don't have any assumption on what is provided, then we need to underscore everything before conversion, as I mentioned earlier, if provided dasherized string it won't be converted into camel case correctly. PS: Disregard proposal on snake case, seems like I remember it wrong, wiki says it's underscored. |
Sure, that seems necessary. For those following along, the problem is: [1] pry(main)> value = 'some-value'
=> "some-value"
[2] pry(main)> value.underscore
=> "some_value"
[3] pry(main)> value.camelize
=> "Some-value"
[4] pry(main)> value.underscore.camelize
=> "SomeValue" |
So I remove changes to KeyTransform and wait until that PR is merged to rebase and finish this one? |
I'm adding the underscore changes to KeyTransform now. It would be great if you could look them over in a little bit. We'll then merge in #1645 and this can be rebased to work with those changes. |
Hmm.. I think there are couple of potential problems, not severe but they can be easily fixed and code will be shorter. So as far as I see, we don't have configuration validation, so if we call transform through |
A key (tee hee) reason for the current structure is so users can add custom transforms. Seems like this approach would hinder that ability. We can continue discussing this using #1645 as a base, which I'll be merging shortly. |
value comes from a config, so it under control, no? there shouldn't be anything going into the Unless you mean |
Yeah, I meant that user can mess things up himself. That's why I said it wasn't really severe. For custom transforms I would use lambda in config. But I'm ok with either way, just thought it could be nicer. |
And actually, with proposed implementation it can be extended too, quite easily. We just call super, unless value is String and type is new custom type and provide custom implementation otherwise. |
There are some breaking changes introduced here: |
@youroff could you rebase? :-) |
Merged into master and fixed tests. Submitted merge-result for review before merge into master is pushed #1791 |
Purpose
Introduces a global setting to control namespace separator for generating
type
attribute when usingjson-api
.Cool::Author
becomescool--authors
with the default value--
.Using custom separator:
This will make for
cool/authors
which is supported by Ember by default.Changes
ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier.type_for
Caveats
As a proposal, we may need to introduce type_transform setting, like key_transform. To provide ways to chose between undersored and dasherized types.
Related GitHub issues
Reimplementation of:
#1216