Skip to content

Commit

Permalink
Ensure meta: option only renders at top-level
Browse files Browse the repository at this point in the history
Top-level meta information should not be rendered on individual
resource objects in the primary data array.

Also tighten up wording in tests and documentation.

Closes #23

Signed-off-by: Alex Coles <alex@alexbcoles.com>
  • Loading branch information
myabc committed Mar 17, 2017
1 parent 0ae3cd2 commit 0063d6d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/roar/json/json_api/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def to_json(options = {})
# @option options [Hash{Symbol=>[Array<String>]}] fields
# fields to returned on a per-type basis.
# @option options [Hash{#to_s}=>Object] meta
# additional meta information to be rendered in the document.
# extra toplevel meta information to be rendered in the document.
# @option options [Hash{Symbol=>Symbol}] user_options
# additional arbitary options to be passed to the Representer.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/roar/json/json_api/resource_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module JSONAPI
module ResourceCollection
# @see Document#to_hash
def to_hash(options = {})
document = super(to_a: options, user_options: options[:user_options]) # [{data: {..}, data: {..}}]
single_options = options.reject { |key, _| [:meta, :user_options].include?(key) }
document = super(to_a: single_options, user_options: options[:user_options]) # [{data: {..}, data: {..}}]

links = Renderer::Links.new.(document, options)
meta = render_meta(options)
Expand Down
10 changes: 8 additions & 2 deletions test/jsonapi/collection_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,18 @@ class JsonapiCollectionRenderTest < MiniTest::Spec
hash['links'].must_equal('self' => '//articles?page=2&per_page=10')
end

it 'renders additional meta information if meta option supplied' do
it 'renders extra toplevel meta information if meta option supplied' do
hash = decorator.to_hash(meta: { page: 2, total: 9 })
hash['meta'].must_equal('count' => 3, page: 2, total: 9)
end

it 'does not render additional meta information if meta option is empty' do
it 'does not render extra meta information on resource objects' do
hash = decorator.to_hash(meta: { page: 2, total: 9 })
refute hash['data'].first['meta'].key?(:page)
refute hash['data'].first['meta'].key?(:total)
end

it 'does not render extra toplevel meta information if meta option is empty' do
hash = decorator.to_hash(meta: {})
hash['meta'][:page].must_be_nil
hash['meta'][:total].must_be_nil
Expand Down
4 changes: 2 additions & 2 deletions test/jsonapi/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class JsonapiRenderTest < MiniTest::Spec
}))
end

it 'renders additional meta information if meta option supplied' do
it 'renders extra toplevel meta information if meta option supplied' do
hash = decorator.to_hash(meta: {
'copyright' => 'Nick Sutterer', 'reviewers' => []
})
Expand All @@ -157,7 +157,7 @@ class JsonapiRenderTest < MiniTest::Spec
hash['meta']['reviewer-initials'].must_equal('C.B.')
end

it 'does not render additonal meta information if meta option is empty' do
it 'does not render extra toplevel meta information if meta option is empty' do
hash = decorator.to_hash(meta: {})
hash['meta']['copyright'].must_be_nil
hash['meta']['reviewers'].must_equal(['Christian Bernstein'])
Expand Down

0 comments on commit 0063d6d

Please sign in to comment.