Skip to content

Commit

Permalink
Ensure the adapters honor a custom root option and include meta when …
Browse files Browse the repository at this point in the history
…required
  • Loading branch information
chrisbranson committed Jun 4, 2015
1 parent 35fb9de commit c4a76c2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def meta_key
end

def root
serializer.json_key
@options.fetch(:root) { serializer.json_key }
end

def include_meta(json)
Expand Down
6 changes: 2 additions & 4 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def serializable_hash(options = {})
@result = @core.merge @hash
end

if root = options.fetch(:root, serializer.json_key)
@result = { root => @result }
end
@result = { root => @result } if root
@result
end
end
Expand All @@ -49,4 +47,4 @@ def fragment_cache(cached_hash, non_cached_hash)
end
end
end
end
end
39 changes: 39 additions & 0 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def render_using_custom_root
render json: @profile, root: "custom_root"
end

def render_using_custom_root_and_meta
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root", meta: { total: 10 }
end

def render_using_default_adapter_root
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
# JSON-API adapter sets root by default
Expand All @@ -28,6 +33,14 @@ def render_using_custom_root_in_adapter_with_a_default
render json: @profile, root: "profile", adapter: :json_api
end

def render_array_using_custom_root_and_meta
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
]
render json: array, root: "custom_root", meta: { total: 10 }
end

def render_array_using_implicit_serializer
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Expand Down Expand Up @@ -161,6 +174,13 @@ def test_render_using_custom_root
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
end

def test_render_using_custom_root_and_meta
get :render_using_custom_root_and_meta

assert_equal 'application/json', @response.content_type
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"},"meta":{"total":10}}', @response.body
end

def test_render_using_default_root
get :render_using_default_adapter_root

Expand Down Expand Up @@ -197,6 +217,25 @@ def test_render_using_custom_root_in_adapter_with_a_default
assert_equal expected.to_json, @response.body
end

def test_render_array_using_custom_root_and_meta
get :render_array_using_custom_root_and_meta
assert_equal 'application/json', @response.content_type

expected = { custom_root: [
{
name: 'Name 1',
description: 'Description 1',
},
{
name: 'Name 2',
description: 'Description 2',
}],
meta: { total: 10 }
}

assert_equal expected.to_json, @response.body
end

def test_render_array_using_implicit_serializer
get :render_array_using_implicit_serializer
assert_equal 'application/json', @response.content_type
Expand Down
35 changes: 31 additions & 4 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def test_meta_key_is_used
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,
Expand All @@ -67,11 +67,38 @@ def test_meta_is_not_used_on_arrays
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
Expand Down

0 comments on commit c4a76c2

Please sign in to comment.