mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Support key transformation for Attributes adapter (#1889)
The `:attributes` adapter is the default one, but it did not support key transformation. This was very surprising behavior, since the "Configuration Options" page in the guides didn't mention that this behavior was not supported by the attributes adapter. This commit adds key transform support to the attributes adapter, and adds documentation about the default transform for the attributes adapter (which is `:unaltered`). This commit also handles arrays when transforming keys, which was needed in the case where you're serializing a collection with the Attributes adapter. With the JSON adapter, it was always guaranteed to pass a hash to the KeyTransform functions because of the top-level key. Since there is no top-level key for the Attributes adapter, the return value could be an array.
This commit is contained in:
committed by
L. Preston Sego III
parent
49ee823a53
commit
2423ca4999
@@ -4,7 +4,9 @@ module ActiveModelSerializers
|
||||
def serializable_hash(options = nil)
|
||||
options = serialization_options(options)
|
||||
options[:fields] ||= instance_options[:fields]
|
||||
serializer.serializable_hash(instance_options, options, self)
|
||||
serialized_hash = serializer.serializable_hash(instance_options, options, self)
|
||||
|
||||
self.class.transform_key_casing!(serialized_hash, instance_options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,6 +11,7 @@ module ActiveModelSerializers
|
||||
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
||||
def camel(value)
|
||||
case 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
|
||||
@@ -25,6 +26,7 @@ module ActiveModelSerializers
|
||||
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
||||
def camel_lower(value)
|
||||
case 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)
|
||||
@@ -40,6 +42,7 @@ module ActiveModelSerializers
|
||||
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L185-L187 ActiveSupport::Inflector.dasherize}
|
||||
def dash(value)
|
||||
case value
|
||||
when Array then value.map { |item| dash(item) }
|
||||
when Hash then value.deep_transform_keys! { |key| dash(key) }
|
||||
when Symbol then dash(value.to_s).to_sym
|
||||
when String then value.underscore.dasherize
|
||||
@@ -55,6 +58,7 @@ module ActiveModelSerializers
|
||||
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L89-L98 ActiveSupport::Inflector.underscore}
|
||||
def underscore(value)
|
||||
case value
|
||||
when Array then value.map { |item| underscore(item) }
|
||||
when Hash then value.deep_transform_keys! { |key| underscore(key) }
|
||||
when Symbol then underscore(value.to_s).to_sym
|
||||
when String then value.underscore
|
||||
|
||||
Reference in New Issue
Block a user