Skip to content

Commit

Permalink
Enable JSONAPI#to_hash meta option
Browse files Browse the repository at this point in the history
Fixes trailblazer#196

Signed-off-by: Alex Coles <alex@alexbcoles.com>
  • Loading branch information
myabc committed Dec 10, 2016
1 parent 1df3430 commit 8e8b8ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/roar/json/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def collection_representer # FIXME: cache.
def to_hash(options={})
hash = super(to_a: options) # [{data: {..}, data: {..}}]
collection = hash["to_a"]
meta = options.fetch('meta', {})

document = {data: []}
included = []
Expand All @@ -45,6 +46,8 @@ def to_hash(options={})

Fragment::Links.(document, Renderer::Links.new.(hash, {}), options)
Fragment::Included.(document, included, options)
Fragment::Meta.(document, meta, options)

document
end
end)
Expand All @@ -69,10 +72,6 @@ def link(name, options={}, &block)
for_collection.link(name, &block)
end

def meta(&block)
representable_attrs[:meta_representer] = Class.new(Roar::Decorator, &block)
end

def has_one(name, options={}, &block)
# every nested representer is a full-blown JSONAPI representer.
dfn = nil
Expand Down Expand Up @@ -135,6 +134,10 @@ module Fragment
Links = ->(document, links, options) do
document[:links] = links if links.any?
end

Meta = ->(document, meta, options) do
document[:meta] = meta if meta.any?
end
end

# {:include=>[:id, :title, :author, :included],
Expand Down Expand Up @@ -164,7 +167,7 @@ def to_hash(options={})
res = super(Options::Include.(options, self))

links = Renderer::Links.new.call(res, options)
# meta = render_meta(options)
meta = options.fetch('meta', {})

relationships = render_relationships(res)
included = render_included(res)
Expand All @@ -180,6 +183,7 @@ def to_hash(options={})

Fragment::Links.(data, links, options)
Fragment::Included.(document, included, options)
Fragment::Meta.(document, meta, options)

document
end
Expand Down Expand Up @@ -217,15 +221,6 @@ def render_included(hash)
end.flatten
end

def render_meta(options)
# TODO: this will call collection.page etc, directly on the collection. we could allow using a "meta"
# object to hold this data.
# `meta call_meta: true` or something
return {"meta" => options["meta"]} if options["meta"]
return {} unless representer = representable_attrs[:meta_representer]
{"meta" => representer.new(represented).extend(Representable::Hash).to_hash}
end

def render_relationships(res)
(res["relationships"] || []).each do |name, hash|
if hash.is_a?(::Hash)
Expand Down
10 changes: 10 additions & 0 deletions test/jsonapi/collection_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ class JsonapiCollectionRenderTest < MiniTest::Spec
)
end

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

it 'does not render meta information if meta option is empty' do
hash = decorator.to_hash('meta' => {})
hash[:meta].must_be_nil
end

describe "Fetching Resources (empty collection)" do
let(:document) {
{
Expand Down
10 changes: 10 additions & 0 deletions test/jsonapi/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ class JsonapiRenderTest < MiniTest::Spec
)
end

it 'renders meta information if meta option supplied' do
hash = decorator.to_hash('meta' => { 'copyright' => 'Nick Sutterer' })
hash[:meta].must_equal('copyright' => 'Nick Sutterer')
end

it 'does not render meta information if meta option is empty' do
hash = decorator.to_hash('meta' => {})
hash[:meta].must_be_nil
end

describe "Single Resource Object" do
class DocumentSingleResourceObjectDecorator < Roar::Decorator
include Roar::JSON::JSONAPI
Expand Down

0 comments on commit 8e8b8ba

Please sign in to comment.