mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Support lowerCamel key format
This commit is contained in:
parent
2e31a14125
commit
00c54baae3
17
README.md
17
README.md
@ -226,6 +226,23 @@ def default_serializer_options
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Changing the Key Format
|
||||||
|
|
||||||
|
You can specify that serializers use the lower-camel key format at the config, class or instance level.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.key_format = :lower_camel
|
||||||
|
end
|
||||||
|
|
||||||
|
class BlogLowerCamelSerializer < ActiveModel::Serializer
|
||||||
|
format_keys :lower_camel
|
||||||
|
end
|
||||||
|
|
||||||
|
BlogSerializer.new(object, key_format: :lower_camel)
|
||||||
|
```
|
||||||
|
|
||||||
## Getting the old version
|
## Getting the old version
|
||||||
|
|
||||||
If you find that your project is already relying on the old rails to_json
|
If you find that your project is already relying on the old rails to_json
|
||||||
|
|||||||
@ -20,8 +20,9 @@ module ActiveModel
|
|||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
@resource_name = options[:resource_name]
|
@resource_name = options[:resource_name]
|
||||||
|
@key_format = options[:key_format]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta
|
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
@ -33,7 +34,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializer_for(item)
|
def serializer_for(item)
|
||||||
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
serializer_class.new(item, scope: scope)
|
serializer_class.new(item, scope: scope, key_format: key_format)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
|
|||||||
@ -38,6 +38,11 @@ end
|
|||||||
WARN
|
WARN
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def format_keys(format)
|
||||||
|
@key_format = format
|
||||||
|
end
|
||||||
|
attr_reader :key_format
|
||||||
|
|
||||||
if RUBY_VERSION >= '2.0'
|
if RUBY_VERSION >= '2.0'
|
||||||
def serializer_for(resource)
|
def serializer_for(resource)
|
||||||
if resource.respond_to?(:to_ary)
|
if resource.respond_to?(:to_ary)
|
||||||
@ -110,6 +115,7 @@ end
|
|||||||
@wrap_in_array = options[:_wrap_in_array]
|
@wrap_in_array = options[:_wrap_in_array]
|
||||||
@only = Array(options[:only]) if options[:only]
|
@only = Array(options[:only]) if options[:only]
|
||||||
@except = Array(options[:except]) if options[:except]
|
@except = Array(options[:except]) if options[:except]
|
||||||
|
@key_format = options[:key_format]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
@ -190,10 +196,35 @@ end
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def key_format
|
||||||
|
@key_format || self.class.key_format || CONFIG.key_format
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_key(key)
|
||||||
|
if key_format == :lower_camel
|
||||||
|
key.to_s.camelize(:lower)
|
||||||
|
else
|
||||||
|
key
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert_keys(hash)
|
||||||
|
Hash[hash.map do |k,v|
|
||||||
|
key = if k.is_a?(Symbol)
|
||||||
|
format_key(k).to_sym
|
||||||
|
else
|
||||||
|
format_key(k)
|
||||||
|
end
|
||||||
|
|
||||||
|
[key ,v]
|
||||||
|
end]
|
||||||
|
end
|
||||||
|
|
||||||
def serializable_object(options={})
|
def serializable_object(options={})
|
||||||
return @wrap_in_array ? [] : nil if @object.nil?
|
return @wrap_in_array ? [] : nil if @object.nil?
|
||||||
hash = attributes
|
hash = attributes
|
||||||
hash.merge! associations
|
hash.merge! associations
|
||||||
|
hash = convert_keys(hash) if key_format.present?
|
||||||
@wrap_in_array ? [hash] : hash
|
@wrap_in_array ? [hash] : hash
|
||||||
end
|
end
|
||||||
alias_method :serializable_hash, :serializable_object
|
alias_method :serializable_hash, :serializable_object
|
||||||
|
|||||||
@ -15,6 +15,7 @@ module ActiveModel
|
|||||||
@options = options
|
@options = options
|
||||||
self.embed = options.fetch(:embed) { CONFIG.embed }
|
self.embed = options.fetch(:embed) { CONFIG.embed }
|
||||||
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
|
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
|
||||||
|
@key_format = options.fetch(:key_format) { CONFIG.key_format }
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@key = options[:key]
|
@key = options[:key]
|
||||||
@embedded_key = options[:root] || name
|
@embedded_key = options[:root] || name
|
||||||
@ -24,7 +25,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :embed_ids, :embed_objects
|
attr_reader :name, :embed_ids, :embed_objects
|
||||||
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options
|
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options, :key_format
|
||||||
alias embed_ids? embed_ids
|
alias embed_ids? embed_ids
|
||||||
alias embed_objects? embed_objects
|
alias embed_objects? embed_objects
|
||||||
alias embed_in_root? embed_in_root
|
alias embed_in_root? embed_in_root
|
||||||
|
|||||||
11
test/fixtures/poro.rb
vendored
11
test/fixtures/poro.rb
vendored
@ -35,6 +35,9 @@ end
|
|||||||
class Comment < Model
|
class Comment < Model
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Blog < Model
|
||||||
|
end
|
||||||
|
|
||||||
###
|
###
|
||||||
## Serializers
|
## Serializers
|
||||||
###
|
###
|
||||||
@ -62,3 +65,11 @@ end
|
|||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
attributes :content
|
attributes :content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class BlogSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name, :display_name
|
||||||
|
end
|
||||||
|
|
||||||
|
class BlogLowerCamelSerializer < BlogSerializer
|
||||||
|
format_keys :lower_camel
|
||||||
|
end
|
||||||
|
|||||||
18
test/unit/active_model/array_serializer/key_format_test.rb
Normal file
18
test/unit/active_model/array_serializer/key_format_test.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
class KeyFormatTest < Minitest::Test
|
||||||
|
def test_array_serializer_pass_options_to_items_serializers
|
||||||
|
array = [Blog.new({ name: 'Name 1', display_name: 'Display Name 1'}),
|
||||||
|
Blog.new({ name: 'Name 2', display_name: 'Display Name 2'})]
|
||||||
|
serializer = ArraySerializer.new(array, key_format: :lower_camel)
|
||||||
|
|
||||||
|
expected = [{ name: 'Name 1', displayName: 'Display Name 1' },
|
||||||
|
{ name: 'Name 2', displayName: 'Display Name 2' }]
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -67,6 +67,7 @@ module ActiveModel
|
|||||||
def test_apply_config_to_associations
|
def test_apply_config_to_associations
|
||||||
CONFIG.embed = :ids
|
CONFIG.embed = :ids
|
||||||
CONFIG.embed_in_root = true
|
CONFIG.embed_in_root = true
|
||||||
|
CONFIG.key_format = :lower_camel
|
||||||
|
|
||||||
association = PostSerializer._associations[:comments]
|
association = PostSerializer._associations[:comments]
|
||||||
old_association = association.dup
|
old_association = association.dup
|
||||||
@ -76,6 +77,7 @@ module ActiveModel
|
|||||||
assert association.embed_ids?
|
assert association.embed_ids?
|
||||||
assert !association.embed_objects?
|
assert !association.embed_objects?
|
||||||
assert association.embed_in_root
|
assert association.embed_in_root
|
||||||
|
assert_equal :lower_camel, association.key_format
|
||||||
ensure
|
ensure
|
||||||
PostSerializer._associations[:comments] = old_association
|
PostSerializer._associations[:comments] = old_association
|
||||||
CONFIG.clear
|
CONFIG.clear
|
||||||
|
|||||||
25
test/unit/active_model/serializer/key_format_test.rb
Normal file
25
test/unit/active_model/serializer/key_format_test.rb
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class KeyFormatTest < Minitest::Test
|
||||||
|
def test_lower_camel_format_option
|
||||||
|
object = Blog.new({ name: 'Name 1', display_name: 'Display Name 1'})
|
||||||
|
serializer = BlogSerializer.new(object, key_format: :lower_camel)
|
||||||
|
|
||||||
|
expected = { name: 'Name 1', displayName: 'Display Name 1' }
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_object
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_lower_camel_format_serializer
|
||||||
|
object = Blog.new({ name: 'Name 1', display_name: 'Display Name 1'})
|
||||||
|
serializer = BlogLowerCamelSerializer.new(object)
|
||||||
|
|
||||||
|
expected = { name: 'Name 1', displayName: 'Display Name 1' }
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user