Skip to content
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

Preserve path when string is camelized #1987

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/active_model_serializers/key_transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ActiveModelSerializers
module KeyTransform
module_function

SLASH_SYMBOL = '/'.freeze
# Transforms values to UpperCamelCase or PascalCase.
#
# @example:
Expand All @@ -14,7 +15,7 @@ def camel(value)
when Array then value.map { |item| camel(item) }
when Hash then value.deep_transform_keys! { |key| camel(key) }
when Symbol then camel(value.to_s).to_sym
when String then value.underscore.camelize
when String then camelize(value, :upper)
else value
end
end
Expand All @@ -29,7 +30,7 @@ def camel_lower(value)
when Array then value.map { |item| camel_lower(item) }
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
when Symbol then camel_lower(value.to_s).to_sym
when String then value.underscore.camelize(:lower)
when String then camelize(value, :lower)
else value
end
end
Expand Down Expand Up @@ -70,5 +71,9 @@ def underscore(value)
def unaltered(value)
value
end

def camelize(value, first_letter)
value.split(SLASH_SYMBOL).map { |ch| ch.underscore.camelize(first_letter) }.join(SLASH_SYMBOL)
end
end
end
8 changes: 8 additions & 0 deletions test/active_model_serializers/key_transform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def test_camel
expected: [
{ SomeValue: 'value' }
]
},
{
value: 'scope/some_value',
expected: 'Scope/SomeValue'
}
]
scenarios.each do |s|
Expand Down Expand Up @@ -142,6 +146,10 @@ def test_camel_lower
expected: [
{ someValue: 'value' }
]
},
{
value: 'scope/some_value',
expected: 'scope/someValue'
}
]
scenarios.each do |s|
Expand Down