Skip to content

Commit

Permalink
support for top-level links limited to jsonapi adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Aug 6, 2015
1 parent 516d89b commit b6c382f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ test/version_tmp
tmp
*.swp
.ruby-version
tags
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
* adds cache support to attributes and associations [@joaomdmoura]
* uses model name to determine the type [@lsylvester]
* remove root key option and split JSON adapter [@joaomdmoura]
* adds FlattenJSON as default adapter [@joaomdmoura]
* adds FlattenJSON as default adapter [@joaomdmoura]
* adds support for `links` at top level of JsonApi adapter [@leandrocp]
14 changes: 1 addition & 13 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def serializable_hash(options = nil)

def as_json(options = nil)
hash = serializable_hash(options)
unless self.class == FlattenJson
include_meta(hash)
include_links(hash)
end
include_meta(hash) unless self.class == FlattenJson
hash
end

Expand Down Expand Up @@ -89,10 +86,6 @@ def meta_key
serializer.meta_key || "meta"
end

def links
serializer.links if serializer.respond_to?(:links)
end

def root
serializer.json_key.to_sym if serializer.json_key
end
Expand All @@ -101,11 +94,6 @@ def include_meta(json)
json[meta_key] = meta if meta
json
end

def include_links(json)
json["links"] = links if links
json
end
end
end
end
5 changes: 5 additions & 0 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def serializable_hash(options = nil)
@hash[:data] = attributes_for_serializer(serializer, options)
add_resource_relationships(@hash[:data], serializer)
end
@hash[:links] = attributes_for_top_level_links(serializer) if serializer.links
@hash
end

Expand Down Expand Up @@ -157,6 +158,10 @@ def add_resource_relationships(attrs, serializer, options = {})
end
end
end

def attributes_for_top_level_links(serializer)
serializer.links
end
end
end
end
Expand Down
85 changes: 26 additions & 59 deletions test/serializers/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,51 @@ def setup
end

def test_links_is_present_with_root
serializer = AlternateBlogSerializer.new(@blog, :links => {"self" => "/blogs/1"})
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
expected = {
blog: {
id: 1,
title: "AMS Hints"
data: {
id: "1",
type: "blogs",
attributes: {
title: "AMS Hints"
}
},
"links" => {
"self" => "/blogs/1"
links: {
self: "/blogs/1"
}
}
assert_equal expected, adapter.as_json
end

def test_links_is_not_included_when_root_is_missing
# load_adapter uses FlattenJson Adapter
adapter = load_adapter(links: {"self" => "/blogs/1"})
def test_links_is_not_present_when_not_declared
serializer = AlternateBlogSerializer.new(@blog)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
expected = {
id: 1,
title: "AMS Hints"
data: {
id: "1",
type: "blogs",
attributes: {
title: "AMS Hints"
}
}
}
assert_equal expected, adapter.as_json
end

def test_links_is_not_present_on_arrays_without_root
serializer = ArraySerializer.new([@blog], links: {"self" => "/blogs/1"})
# FlattenJSON doesn't have support to root
def test_links_is_not_present_on_flattenjson_adapter
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
expected = [{
id: 1,
name: "AMS Hints",
writer: {
id: 2,
name: "Steve"
},
articles: [{
id: 3,
title: "AMS",
body: nil
}]
}]
expected = {:id=>1, :title=>"AMS Hints"}
assert_equal expected, adapter.as_json
end

def test_links_is_present_on_arrays_with_root
serializer = ArraySerializer.new([@blog], links: {"self" => "/blogs/1"})
# JSON adapter adds root by default
def test_links_is_not_present_on_json_adapter
serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"})
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
expected = {
blogs: [{
id: 1,
name: "AMS Hints",
writer: {
id: 2,
name: "Steve"
},
articles: [{
id: 3,
title: "AMS",
body: nil
}]
}],
"links" => {
"self" => "/blogs/1"
}
}
expected = {:blog=>{:id=>1, :title=>"AMS Hints"}}
assert_equal expected, adapter.as_json
end

private

def load_adapter(options)
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::FlattenJson.new(serializer, adapter_opts)
end
end
end
end

0 comments on commit b6c382f

Please sign in to comment.