-
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
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 |
---|---|---|
|
@@ -20,11 +20,13 @@ def as_json | |
|
||
def type_for(serializer) | ||
return serializer._type if serializer._type | ||
if ActiveModelSerializers.config.jsonapi_resource_type == :singular | ||
serializer.object.class.model_name.singular | ||
else | ||
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 commentThe 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 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. that's what i'm thinking |
||
if ActiveModelSerializers.config.jsonapi_resource_type == :plural | ||
type = type.pluralize | ||
end | ||
type | ||
end | ||
|
||
def id_for(serializer) | ||
|
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 usemodel_name
from rails. Just plain and simple class name processing.