Skip to content

Commit

Permalink
Fix #876 - include meta when using json adapter with custom root
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbranson committed Jun 3, 2015
1 parent 35fb9de commit d8cf11e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 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
12 changes: 12 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 Down Expand Up @@ -161,6 +166,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

0 comments on commit d8cf11e

Please sign in to comment.