mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Ensure the adapters honor a custom root option and include meta when required
This commit is contained in:
@@ -48,8 +48,8 @@ module ActiveModel
|
||||
assert_equal expected, adapter.as_json
|
||||
end
|
||||
|
||||
def test_meta_is_not_used_on_arrays
|
||||
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
|
||||
def test_meta_is_not_present_on_arrays_without_root
|
||||
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
expected = [{
|
||||
id: 1,
|
||||
@@ -67,11 +67,38 @@ module ActiveModel
|
||||
assert_equal expected, adapter.as_json
|
||||
end
|
||||
|
||||
def test_meta_is_present_on_arrays_with_root
|
||||
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
||||
expected = {
|
||||
'blog' => [{
|
||||
id: 1,
|
||||
name: "AMS Hints",
|
||||
writer: {
|
||||
id: 2,
|
||||
name: "Steve"
|
||||
},
|
||||
articles: [{
|
||||
id: 3,
|
||||
title: "AMS",
|
||||
body: nil
|
||||
}]
|
||||
}],
|
||||
'haha_meta' => {
|
||||
total: 10
|
||||
}
|
||||
}
|
||||
assert_equal expected, adapter.as_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_adapter(options)
|
||||
serializer = AlternateBlogSerializer.new(@blog, options)
|
||||
ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
adapter_opts, serializer_opts =
|
||||
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||
|
||||
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
||||
ActiveModel::Serializer::Adapter::Json.new(serializer, adapter_opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user