Support lowerCamel key format

This commit is contained in:
Kyle Fritz 2014-03-30 11:53:24 -04:00
parent 2e31a14125
commit 00c54baae3
8 changed files with 109 additions and 3 deletions

View File

@ -226,6 +226,23 @@ def default_serializer_options
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
If you find that your project is already relying on the old rails to_json

View File

@ -20,8 +20,9 @@ module ActiveModel
@meta = options[@meta_key]
@each_serializer = options[:each_serializer]
@resource_name = options[:resource_name]
@key_format = options[:key_format]
end
attr_accessor :object, :scope, :root, :meta_key, :meta
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
def json_key
if root.nil?
@ -33,7 +34,7 @@ module ActiveModel
def serializer_for(item)
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
def serializable_object

View File

@ -38,6 +38,11 @@ end
WARN
end
def format_keys(format)
@key_format = format
end
attr_reader :key_format
if RUBY_VERSION >= '2.0'
def serializer_for(resource)
if resource.respond_to?(:to_ary)
@ -110,6 +115,7 @@ end
@wrap_in_array = options[:_wrap_in_array]
@only = Array(options[:only]) if options[:only]
@except = Array(options[:except]) if options[:except]
@key_format = options[:key_format]
end
attr_accessor :object, :scope, :root, :meta_key, :meta
@ -190,10 +196,35 @@ 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={})
return @wrap_in_array ? [] : nil if @object.nil?
hash = attributes
hash.merge! associations
hash = convert_keys(hash) if key_format.present?
@wrap_in_array ? [hash] : hash
end
alias_method :serializable_hash, :serializable_object

View File

@ -15,6 +15,7 @@ module ActiveModel
@options = options
self.embed = options.fetch(:embed) { CONFIG.embed }
@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
@key = options[:key]
@embedded_key = options[:root] || name
@ -24,7 +25,7 @@ module ActiveModel
end
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_objects? embed_objects
alias embed_in_root? embed_in_root

11
test/fixtures/poro.rb vendored
View File

@ -35,6 +35,9 @@ end
class Comment < Model
end
class Blog < Model
end
###
## Serializers
###
@ -62,3 +65,11 @@ end
class CommentSerializer < ActiveModel::Serializer
attributes :content
end
class BlogSerializer < ActiveModel::Serializer
attributes :name, :display_name
end
class BlogLowerCamelSerializer < BlogSerializer
format_keys :lower_camel
end

View 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

View File

@ -67,6 +67,7 @@ module ActiveModel
def test_apply_config_to_associations
CONFIG.embed = :ids
CONFIG.embed_in_root = true
CONFIG.key_format = :lower_camel
association = PostSerializer._associations[:comments]
old_association = association.dup
@ -76,6 +77,7 @@ module ActiveModel
assert association.embed_ids?
assert !association.embed_objects?
assert association.embed_in_root
assert_equal :lower_camel, association.key_format
ensure
PostSerializer._associations[:comments] = old_association
CONFIG.clear

View 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