-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add relationship level links and meta options
- Loading branch information
Showing
5 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class JsonApiHasManyLinksTest < ActionController::TestCase | ||
LinkTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_many :posts, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } }, data: false | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_many_association | ||
@tag = Tag.new(id: 1) | ||
@tag.posts = [] | ||
render json: @tag, adapter: :json_api, serializer: LinkTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_many_association | ||
get :render_resource_with_has_many_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
posts: { | ||
links: { | ||
related: 'http://test.host/tags/1/posts' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
|
||
class JsonApiHasManyLinksWithDataTest < ActionController::TestCase | ||
LinkTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_many :posts, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } } | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_many_association | ||
@tag = Tag.new(id: 1) | ||
@tag.posts = [] | ||
render json: @tag, adapter: :json_api, serializer: LinkTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_many_association | ||
get :render_resource_with_has_many_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
posts: { | ||
data: [], | ||
links: { | ||
related: 'http://test.host/tags/1/posts' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class JsonApiHasManyMetaTest < ActionController::TestCase | ||
MetaTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_many :posts, meta: -> (_object, _name) { { page: '1' } } | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_many_association | ||
@tag = Tag.new(id: 1) | ||
@tag.posts = [] | ||
render json: @tag, adapter: :json_api, serializer: MetaTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_many_association | ||
get :render_resource_with_has_many_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
posts: { | ||
data: [], | ||
meta: { | ||
page: '1' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class JsonApiHasOneLinksTest < ActionController::TestCase | ||
LinkTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_one :post, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } }, data: false | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_one_association | ||
@tag = Tag.new(id: 1) | ||
@tag.post = Post.new(id: 1) | ||
render json: @tag, adapter: :json_api, serializer: LinkTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_one_association | ||
get :render_resource_with_has_one_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
post: { | ||
links: { | ||
related: 'http://test.host/tags/1/post' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
|
||
class JsonApiHasOneLinksWithDataTest < ActionController::TestCase | ||
LinkTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_one :post, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } } | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_one_association | ||
@tag = Tag.new(id: 1) | ||
@tag.post = Post.new(id: 1) | ||
render json: @tag, adapter: :json_api, serializer: LinkTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_one_association | ||
get :render_resource_with_has_one_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
post: { | ||
data: { id: '1', type: 'posts' }, | ||
links: { | ||
related: 'http://test.host/tags/1/post' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class JsonApiHasOneMetaTest < ActionController::TestCase | ||
MetaTagSerializer = Class.new(ActiveModel::Serializer) do | ||
has_one :post, meta: -> (_object, _name) { { deleted: true } } | ||
end | ||
|
||
class MyController < ActionController::Base | ||
def render_resource_with_has_one_association | ||
@tag = Tag.new(id: 1) | ||
@tag.post = Post.new(id: 1) | ||
render json: @tag, adapter: :json_api, serializer: MetaTagSerializer | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_resource_with_has_one_association | ||
get :render_resource_with_has_one_association | ||
expected = { | ||
data: { | ||
id: '1', | ||
type: 'tags', | ||
relationships: { | ||
post: { | ||
data: { id: '1', type: 'posts' }, | ||
meta: { | ||
deleted: true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
assert_equal expected.to_json, response.body | ||
end | ||
end | ||
end | ||
end |