mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
parent
1844c162f1
commit
37e4f1c30c
@ -1,25 +0,0 @@
|
|||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
class Adapter
|
|
||||||
class JsonApi < Adapter
|
|
||||||
class Links
|
|
||||||
def initialize(links = {})
|
|
||||||
@links = links
|
|
||||||
end
|
|
||||||
|
|
||||||
def serializable_hash
|
|
||||||
@links
|
|
||||||
end
|
|
||||||
|
|
||||||
def update(links = {})
|
|
||||||
@links.update(links)
|
|
||||||
end
|
|
||||||
|
|
||||||
def present?
|
|
||||||
!@links.empty?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -47,12 +47,11 @@ module ActionController
|
|||||||
|
|
||||||
def render_array_using_implicit_serializer_and_links
|
def render_array_using_implicit_serializer_and_links
|
||||||
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
||||||
|
|
||||||
@profiles = [
|
@profiles = [
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
]
|
]
|
||||||
|
|
||||||
render json: @profiles, links: { self: "http://example.com/api/profiles/1" }
|
render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -272,15 +271,15 @@ module ActionController
|
|||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
id: assigns(:profiles).first.id.to_s,
|
id: assigns(:profiles).first.id.to_s,
|
||||||
type: "profiles",
|
type: 'profiles',
|
||||||
attributes: {
|
attributes: {
|
||||||
name: "Name 1",
|
name: 'Name 1',
|
||||||
description: "Description 1"
|
description: 'Description 1'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
links: {
|
links: {
|
||||||
self: "http://example.com/api/profiles/1"
|
self: 'http://example.com/api/profiles/1'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -47,6 +47,15 @@ module ActiveModel
|
|||||||
assert_equal(expected, hash[:links])
|
assert_equal(expected, hash[:links])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_nil_toplevel_links
|
||||||
|
hash = ActiveModel::SerializableResource.new(
|
||||||
|
@post,
|
||||||
|
adapter: :json_api,
|
||||||
|
links: nil
|
||||||
|
).serializable_hash
|
||||||
|
assert_equal(nil, hash[:links])
|
||||||
|
end
|
||||||
|
|
||||||
def test_resource_links
|
def test_resource_links
|
||||||
hash = serializable(@author, adapter: :json_api).serializable_hash
|
hash = serializable(@author, adapter: :json_api).serializable_hash
|
||||||
expected = {
|
expected = {
|
||||||
|
|||||||
@ -1,101 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
class Adapter
|
|
||||||
class JsonApi
|
|
||||||
class TopLevelLinksTest < Minitest::Test
|
|
||||||
URI = 'http://example.com'
|
|
||||||
|
|
||||||
def setup
|
|
||||||
ActionController::Base.cache_store.clear
|
|
||||||
@blog = Blog.new(id: 1,
|
|
||||||
name: 'AMS Hints',
|
|
||||||
writer: Author.new(id: 2, name: "Steve"),
|
|
||||||
articles: [Post.new(id: 3, title: "AMS")])
|
|
||||||
end
|
|
||||||
|
|
||||||
def load_adapter(paginated_collection, options = {})
|
|
||||||
options = options.merge(adapter: :json_api)
|
|
||||||
ActiveModel::SerializableResource.new(paginated_collection, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_links_is_not_present_when_not_defined
|
|
||||||
adapter = load_adapter(@blog)
|
|
||||||
|
|
||||||
expected = {
|
|
||||||
:data => {
|
|
||||||
:id => "1",
|
|
||||||
:type => "blogs",
|
|
||||||
:attributes => {
|
|
||||||
:name => "AMS Hints"
|
|
||||||
},
|
|
||||||
:relationships => {
|
|
||||||
:writer=> {:data => {:type => "authors", :id => "2"}},
|
|
||||||
:articles => {:data => [{:type => "posts", :id => "3"}]}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
|
|
||||||
assert_equal expected, adapter.serializable_hash(@options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_links_is_present_when_defined
|
|
||||||
adapter = load_adapter(@blog, {links: links})
|
|
||||||
|
|
||||||
expected = {
|
|
||||||
:data => {
|
|
||||||
:id => "1",
|
|
||||||
:type => "blogs",
|
|
||||||
:attributes => {
|
|
||||||
:name => "AMS Hints"
|
|
||||||
},
|
|
||||||
:relationships => {
|
|
||||||
:writer=> {:data => {:type => "authors", :id => "2"}},
|
|
||||||
:articles => {:data => [{:type => "posts", :id => "3"}]}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
:links => {:self => "http://example.com/blogs/1"}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_equal expected, adapter.serializable_hash(@options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def links
|
|
||||||
{
|
|
||||||
self: "#{URI}/blogs/1"
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# def test_links_is_not_present_when_not_declared
|
|
||||||
# serializer = AlternateBlogSerializer.new(@blog)
|
|
||||||
# adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
|
||||||
# expected = {
|
|
||||||
# data: {
|
|
||||||
# id: "1",
|
|
||||||
# type: "blogs",
|
|
||||||
# attributes: {
|
|
||||||
# title: "AMS Hints"
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
# assert_equal expected, adapter.as_json
|
|
||||||
# end
|
|
||||||
|
|
||||||
# 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, :title=>"AMS Hints"}
|
|
||||||
# assert_equal expected, adapter.as_json
|
|
||||||
# end
|
|
||||||
|
|
||||||
# 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 = {:blog=>{:id=>1, :title=>"AMS Hints"}}
|
|
||||||
# assert_equal expected, adapter.as_json
|
|
||||||
# end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
Reference in New Issue
Block a user