mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
commit
87c47f8fdc
1
.gitignore
vendored
1
.gitignore
vendored
@ -19,3 +19,4 @@ test/version_tmp
|
||||
tmp
|
||||
*.swp
|
||||
.ruby-version
|
||||
.ruby-gemset
|
||||
|
||||
@ -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 `pagination links` at top level of JsonApi adapter [@bacarini]
|
||||
|
||||
@ -118,7 +118,7 @@ If you wish to use a serializer other than the default, you can explicitly pass
|
||||
render json: @posts, each_serializer: PostPreviewSerializer
|
||||
|
||||
# Or, you can explicitly provide the collection serializer as well
|
||||
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
|
||||
render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
|
||||
```
|
||||
|
||||
### Meta
|
||||
@ -272,6 +272,11 @@ And you can change the JSON key that the serializer should use for a particular
|
||||
|
||||
The `url` declaration describes which named routes to use while generating URLs
|
||||
for your JSON. Not every adapter will require URLs.
|
||||
## Pagination
|
||||
|
||||
Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
|
||||
|
||||
Although the others adapters does not have this feature, it is possible to implement pagination links to `JSON` adapter. For more information about it, please see in our docs [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md)
|
||||
|
||||
## Caching
|
||||
|
||||
|
||||
@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
|
||||
spec.add_development_dependency "bundler", "~> 1.6"
|
||||
spec.add_development_dependency "timecop", ">= 0.7"
|
||||
spec.add_development_dependency "rake"
|
||||
spec.add_development_dependency "kaminari"
|
||||
spec.add_development_dependency "will_paginate"
|
||||
end
|
||||
|
||||
@ -12,6 +12,7 @@ This is the documentation of AMS, it's focused on the **0.10.x version.**
|
||||
## How to
|
||||
|
||||
- [How to add root key](howto/add_root_key.md)
|
||||
- [How to add pagination links](howto/add_pagination_links.md)
|
||||
|
||||
## Getting Help
|
||||
|
||||
|
||||
112
docs/howto/add_pagination_links.md
Normal file
112
docs/howto/add_pagination_links.md
Normal file
@ -0,0 +1,112 @@
|
||||
# How to add pagination links
|
||||
|
||||
### JSON-API adapter
|
||||
|
||||
Pagination links will be included in your response automatically as long as the resource is paginated and if you are using a ```JSON-API``` adapter.
|
||||
|
||||
If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
|
||||
|
||||
###### Kaminari examples
|
||||
```ruby
|
||||
#array
|
||||
@posts = Kaminari.paginate_array([1, 2, 3]).page(3).per(1)
|
||||
render json: @posts
|
||||
|
||||
#active_record
|
||||
@posts = Post.page(3).per(1)
|
||||
render json: @posts
|
||||
```
|
||||
|
||||
###### WillPaginate examples
|
||||
|
||||
```ruby
|
||||
#array
|
||||
@posts = [1,2,3].paginate(page: 3, per_page: 1)
|
||||
render json: @posts
|
||||
|
||||
#active_record
|
||||
@posts = Post.page(3).per_page(1)
|
||||
render json: @posts
|
||||
```
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :json_api
|
||||
```
|
||||
|
||||
ex:
|
||||
```json
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"type": "articles",
|
||||
"id": "3",
|
||||
"attributes": {
|
||||
"title": "JSON API paints my bikeshed!",
|
||||
"body": "The shortest article. Ever.",
|
||||
"created": "2015-05-22T14:56:29.000Z",
|
||||
"updated": "2015-05-22T14:56:28.000Z"
|
||||
}
|
||||
}
|
||||
],
|
||||
"links": {
|
||||
"self": "http://example.com/articles?page[number]=3&page[size]=1",
|
||||
"first": "http://example.com/articles?page[number]=1&page[size]=1",
|
||||
"prev": "http://example.com/articles?page[number]=2&page[size]=1",
|
||||
"next": "http://example.com/articles?page[number]=4&page[size]=1",
|
||||
"last": "http://example.com/articles?page[number]=13&page[size]=1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
AMS pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
|
||||
|
||||
|
||||
### JSON adapter
|
||||
|
||||
If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
|
||||
|
||||
In your action specify a custom serializer.
|
||||
```ruby
|
||||
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
|
||||
```
|
||||
|
||||
And then, you could do something like the following class.
|
||||
```ruby
|
||||
class PaginatedSerializer < ActiveModel::Serializer::ArraySerializer
|
||||
def initialize(object, options={})
|
||||
meta_key = options[:meta_key] || :meta
|
||||
options[meta_key] ||= {}
|
||||
options[meta_key] = {
|
||||
current_page: object.current_page,
|
||||
next_page: object.next_page,
|
||||
prev_page: object.prev_page,
|
||||
total_pages: object.total_pages,
|
||||
total_count: object.total_count
|
||||
}
|
||||
super(object, options)
|
||||
end
|
||||
end
|
||||
```
|
||||
ex.
|
||||
```json
|
||||
{
|
||||
"articles": [
|
||||
{
|
||||
"id": 2,
|
||||
"title": "JSON API paints my bikeshed!",
|
||||
"body": "The shortest article. Ever."
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"current_page": 3,
|
||||
"next_page": 4,
|
||||
"prev_page": 2,
|
||||
"total_pages": 10,
|
||||
"total_count": 10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### FlattenJSON adapter
|
||||
|
||||
This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.
|
||||
@ -47,6 +47,7 @@ module ActionController
|
||||
|
||||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
||||
define_method renderer_method do |resource, options|
|
||||
options.fetch(:context) { options[:context] = request }
|
||||
serializable_resource = get_serializer(resource, options)
|
||||
super(serializable_resource, options)
|
||||
end
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
require 'active_model/serializer/adapter/json_api/fragment_cache'
|
||||
require 'active_model/serializer/adapter/json_api/pagination_links'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
@ -27,6 +28,8 @@ module ActiveModel
|
||||
@hash[:included] |= result[:included]
|
||||
end
|
||||
end
|
||||
|
||||
add_links(options)
|
||||
else
|
||||
@hash[:data] = attributes_for_serializer(serializer, options)
|
||||
add_resource_relationships(@hash[:data], serializer)
|
||||
@ -157,6 +160,23 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_links(options)
|
||||
links = @hash.fetch(:links) { {} }
|
||||
resources = serializer.instance_variable_get(:@resource)
|
||||
@hash[:links] = add_pagination_links(links, resources, options) if is_paginated?(resources)
|
||||
end
|
||||
|
||||
def add_pagination_links(links, resources, options)
|
||||
pagination_links = JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
|
||||
links.update(pagination_links)
|
||||
end
|
||||
|
||||
def is_paginated?(resource)
|
||||
resource.respond_to?(:current_page) &&
|
||||
resource.respond_to?(:total_pages) &&
|
||||
resource.respond_to?(:size)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi < Adapter
|
||||
class PaginationLinks
|
||||
FIRST_PAGE = 1
|
||||
|
||||
attr_reader :collection, :context
|
||||
|
||||
def initialize(collection, context)
|
||||
@collection = collection
|
||||
@context = context
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
pages_from.each_with_object({}) do |(key, value), hash|
|
||||
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
|
||||
|
||||
hash[key] = "#{url(options)}?#{params}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def pages_from
|
||||
return {} if collection.total_pages == FIRST_PAGE
|
||||
|
||||
{}.tap do |pages|
|
||||
pages[:self] = collection.current_page
|
||||
|
||||
unless collection.current_page == FIRST_PAGE
|
||||
pages[:first] = FIRST_PAGE
|
||||
pages[:prev] = collection.current_page - FIRST_PAGE
|
||||
end
|
||||
|
||||
unless collection.current_page == collection.total_pages
|
||||
pages[:next] = collection.current_page + FIRST_PAGE
|
||||
pages[:last] = collection.total_pages
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def url(options)
|
||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || original_url
|
||||
end
|
||||
|
||||
def original_url
|
||||
@original_url ||= context.original_url[/\A[^?]+/]
|
||||
end
|
||||
|
||||
def query_parameters
|
||||
@query_parameters ||= context.query_parameters
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
180
test/action_controller/json_api/linked_test.rb
Normal file
180
test/action_controller/json_api/linked_test.rb
Normal file
@ -0,0 +1,180 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class JsonApi
|
||||
class LinkedTest < ActionController::TestCase
|
||||
class LinkedTestController < ActionController::Base
|
||||
def setup_post
|
||||
ActionController::Base.cache_store.clear
|
||||
@role1 = Role.new(id: 1, name: 'admin')
|
||||
@role2 = Role.new(id: 2, name: 'colab')
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.posts = []
|
||||
@author.bio = nil
|
||||
@author.roles = [@role1, @role2]
|
||||
@role1.author = @author
|
||||
@role2.author = @author
|
||||
@author2 = Author.new(id: 2, name: 'Anonymous')
|
||||
@author2.posts = []
|
||||
@author2.bio = nil
|
||||
@author2.roles = []
|
||||
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@post.comments = [@first_comment, @second_comment]
|
||||
@post.author = @author
|
||||
@first_comment.post = @post
|
||||
@first_comment.author = @author2
|
||||
@second_comment.post = @post
|
||||
@second_comment.author = nil
|
||||
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
|
||||
@post2.author = @author
|
||||
@post2.comments = []
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@post.blog = @blog
|
||||
@post2.blog = @blog
|
||||
end
|
||||
|
||||
def render_resource_without_include
|
||||
setup_post
|
||||
render json: @post, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_include
|
||||
setup_post
|
||||
render json: @post, include: 'author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_include
|
||||
setup_post
|
||||
render json: @post, include: 'comments.author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_has_many_include
|
||||
setup_post
|
||||
render json: @post, include: ['author', 'author.roles'], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_missing_nested_has_many_include
|
||||
setup_post
|
||||
@post.author = @author2 # author2 has no roles.
|
||||
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_with_missing_nested_has_many_include
|
||||
setup_post
|
||||
@post.author = @author2
|
||||
render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_without_include
|
||||
setup_post
|
||||
render json: [@post], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_with_include
|
||||
setup_post
|
||||
render json: [@post], include: ['author', 'comments'], adapter: :json_api
|
||||
end
|
||||
end
|
||||
|
||||
tests LinkedTestController
|
||||
|
||||
def test_render_resource_without_include
|
||||
get :render_resource_without_include
|
||||
response = JSON.parse(@response.body)
|
||||
refute response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_resource_with_include
|
||||
get :render_resource_with_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert_equal 1, response['included'].size
|
||||
assert_equal 'Steve K.', response['included'].first['attributes']['name']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_has_many_include
|
||||
get :render_resource_with_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
expected_linked = [
|
||||
{
|
||||
"id" => "1",
|
||||
"type" => "authors",
|
||||
"attributes" => {
|
||||
"name" => "Steve K."
|
||||
},
|
||||
"relationships" => {
|
||||
"posts" => { "data" => [] },
|
||||
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
||||
"bio" => { "data" => nil }
|
||||
}
|
||||
}, {
|
||||
"id" => "1",
|
||||
"type" => "roles",
|
||||
"attributes" => {
|
||||
"name" => "admin",
|
||||
"description" => nil,
|
||||
"slug" => "admin-1"
|
||||
},
|
||||
"relationships" => {
|
||||
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||
}
|
||||
}, {
|
||||
"id" => "2",
|
||||
"type" => "roles",
|
||||
"attributes" => {
|
||||
"name" => "colab",
|
||||
"description" => nil,
|
||||
"slug" => "colab-2"
|
||||
},
|
||||
"relationships" => {
|
||||
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||
}
|
||||
}
|
||||
]
|
||||
assert_equal expected_linked, response['included']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_include
|
||||
get :render_resource_with_nested_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert_equal 1, response['included'].size
|
||||
assert_equal 'Anonymous', response['included'].first['attributes']['name']
|
||||
end
|
||||
|
||||
def test_render_collection_without_include
|
||||
get :render_collection_without_include
|
||||
response = JSON.parse(@response.body)
|
||||
refute response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_collection_with_include
|
||||
get :render_collection_with_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
||||
get :render_resource_with_missing_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
refute has_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def test_render_collection_with_missing_nested_has_many_include
|
||||
get :render_collection_with_missing_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert has_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def has_type?(collection, value)
|
||||
collection.detect { |i| i['type'] == value}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
116
test/action_controller/json_api/pagination_test.rb
Normal file
116
test/action_controller/json_api/pagination_test.rb
Normal file
@ -0,0 +1,116 @@
|
||||
require 'test_helper'
|
||||
require 'will_paginate/array'
|
||||
require 'kaminari'
|
||||
require 'kaminari/hooks'
|
||||
::Kaminari::Hooks.init
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class JsonApi
|
||||
class PaginationTest < ActionController::TestCase
|
||||
KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'
|
||||
WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'
|
||||
|
||||
class PaginationTestController < ActionController::Base
|
||||
def setup
|
||||
@array = [
|
||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
||||
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
||||
]
|
||||
end
|
||||
|
||||
def using_kaminari
|
||||
setup
|
||||
Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
|
||||
end
|
||||
|
||||
def using_will_paginate
|
||||
setup
|
||||
@array.paginate(page: params[:page][:number], per_page: params[:page][:size])
|
||||
end
|
||||
|
||||
def render_pagination_using_kaminari
|
||||
render json: using_kaminari, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_pagination_using_will_paginate
|
||||
render json: using_will_paginate, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_array_without_pagination_links
|
||||
setup
|
||||
render json: @array, adapter: :json_api
|
||||
end
|
||||
end
|
||||
|
||||
tests PaginationTestController
|
||||
|
||||
def test_render_pagination_links_with_will_paginate
|
||||
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
"first"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
"prev"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
|
||||
|
||||
get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_render_only_last_and_next_pagination_links
|
||||
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
||||
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
||||
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"}
|
||||
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_render_pagination_links_with_kaminari
|
||||
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
"next"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
"last"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
|
||||
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_render_only_prev_and_first_pagination_links
|
||||
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"}
|
||||
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_render_only_last_and_next_pagination_links_with_additional_params
|
||||
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
||||
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
||||
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"}
|
||||
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: "additional"
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_render_only_prev_and_first_pagination_links_with_additional_params
|
||||
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
||||
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
||||
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"}
|
||||
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: "additional"
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
end
|
||||
|
||||
def test_array_without_pagination_links
|
||||
get :render_array_without_pagination_links, page: { number: 2, size: 1 }
|
||||
response = JSON.parse(@response.body)
|
||||
refute response.key? 'links'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,179 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class JsonApiLinkedTest < ActionController::TestCase
|
||||
class JsonApiLinkedTestController < ActionController::Base
|
||||
def setup_post
|
||||
ActionController::Base.cache_store.clear
|
||||
@role1 = Role.new(id: 1, name: 'admin')
|
||||
@role2 = Role.new(id: 2, name: 'colab')
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.posts = []
|
||||
@author.bio = nil
|
||||
@author.roles = [@role1, @role2]
|
||||
@role1.author = @author
|
||||
@role2.author = @author
|
||||
@author2 = Author.new(id: 2, name: 'Anonymous')
|
||||
@author2.posts = []
|
||||
@author2.bio = nil
|
||||
@author2.roles = []
|
||||
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@post.comments = [@first_comment, @second_comment]
|
||||
@post.author = @author
|
||||
@first_comment.post = @post
|
||||
@first_comment.author = @author2
|
||||
@second_comment.post = @post
|
||||
@second_comment.author = nil
|
||||
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
|
||||
@post2.author = @author
|
||||
@post2.comments = []
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@post.blog = @blog
|
||||
@post2.blog = @blog
|
||||
end
|
||||
|
||||
def render_resource_without_include
|
||||
setup_post
|
||||
render json: @post, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_include
|
||||
setup_post
|
||||
render json: @post, include: 'author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_include
|
||||
setup_post
|
||||
render json: @post, include: 'comments.author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_has_many_include
|
||||
setup_post
|
||||
render json: @post, include: ['author', 'author.roles'], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_missing_nested_has_many_include
|
||||
setup_post
|
||||
@post.author = @author2 # author2 has no roles.
|
||||
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_with_missing_nested_has_many_include
|
||||
setup_post
|
||||
@post.author = @author2
|
||||
render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_without_include
|
||||
setup_post
|
||||
render json: [@post], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_with_include
|
||||
setup_post
|
||||
render json: [@post], include: ['author', 'comments'], adapter: :json_api
|
||||
end
|
||||
end
|
||||
|
||||
tests JsonApiLinkedTestController
|
||||
|
||||
def test_render_resource_without_include
|
||||
get :render_resource_without_include
|
||||
response = JSON.parse(@response.body)
|
||||
refute response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_resource_with_include
|
||||
get :render_resource_with_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert_equal 1, response['included'].size
|
||||
assert_equal 'Steve K.', response['included'].first['attributes']['name']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_has_many_include
|
||||
get :render_resource_with_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
expected_linked = [
|
||||
{
|
||||
"id" => "1",
|
||||
"type" => "authors",
|
||||
"attributes" => {
|
||||
"name" => "Steve K."
|
||||
},
|
||||
"relationships" => {
|
||||
"posts" => { "data" => [] },
|
||||
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
||||
"bio" => { "data" => nil }
|
||||
}
|
||||
}, {
|
||||
"id" => "1",
|
||||
"type" => "roles",
|
||||
"attributes" => {
|
||||
"name" => "admin",
|
||||
"description" => nil,
|
||||
"slug" => "admin-1"
|
||||
},
|
||||
"relationships" => {
|
||||
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||
}
|
||||
}, {
|
||||
"id" => "2",
|
||||
"type" => "roles",
|
||||
"attributes" => {
|
||||
"name" => "colab",
|
||||
"description" => nil,
|
||||
"slug" => "colab-2"
|
||||
},
|
||||
"relationships" => {
|
||||
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
||||
}
|
||||
}
|
||||
]
|
||||
assert_equal expected_linked, response['included']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_include
|
||||
get :render_resource_with_nested_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert_equal 1, response['included'].size
|
||||
assert_equal 'Anonymous', response['included'].first['attributes']['name']
|
||||
end
|
||||
|
||||
def test_render_collection_without_include
|
||||
get :render_collection_without_include
|
||||
response = JSON.parse(@response.body)
|
||||
refute response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_collection_with_include
|
||||
get :render_collection_with_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
||||
get :render_resource_with_missing_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
refute has_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def test_render_collection_with_missing_nested_has_many_include
|
||||
get :render_collection_with_missing_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert has_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def has_type?(collection, value)
|
||||
collection.detect { |i| i['type'] == value}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
115
test/adapter/json_api/pagination_links_test.rb
Normal file
115
test/adapter/json_api/pagination_links_test.rb
Normal file
@ -0,0 +1,115 @@
|
||||
require 'test_helper'
|
||||
require 'will_paginate/array'
|
||||
require 'kaminari'
|
||||
require 'kaminari/hooks'
|
||||
::Kaminari::Hooks.init
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi
|
||||
class PaginationLinksTest < Minitest::Test
|
||||
URI = 'http://example.com'
|
||||
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@array = [
|
||||
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
||||
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
||||
]
|
||||
end
|
||||
|
||||
def mock_request(query_parameters={}, original_url=URI)
|
||||
context = Minitest::Mock.new
|
||||
context.expect(:original_url, original_url )
|
||||
context.expect(:query_parameters, query_parameters)
|
||||
@options = {}
|
||||
@options[:context] = context
|
||||
end
|
||||
|
||||
def load_adapter(paginated_collection, options = {})
|
||||
options = options.merge(adapter: :json_api)
|
||||
ActiveModel::SerializableResource.new(paginated_collection, options)
|
||||
end
|
||||
|
||||
def using_kaminari
|
||||
Kaminari.paginate_array(@array).page(2).per(1)
|
||||
end
|
||||
|
||||
def using_will_paginate
|
||||
@array.paginate(page: 2, per_page: 1)
|
||||
end
|
||||
|
||||
def data
|
||||
{ data:[
|
||||
{ id:"1", type:"profiles", attributes:{name:"Name 1", description:"Description 1" } },
|
||||
{ id:"2", type:"profiles", attributes:{name:"Name 2", description:"Description 2" } },
|
||||
{ id:"3", type:"profiles", attributes:{name:"Name 3", description:"Description 3" } }
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def links
|
||||
{
|
||||
links:{
|
||||
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def expected_response_without_pagination_links
|
||||
data
|
||||
end
|
||||
|
||||
def expected_response_with_pagination_links
|
||||
{}.tap do |hash|
|
||||
hash[:data] = [data.values.flatten.second]
|
||||
hash.merge! links
|
||||
end
|
||||
end
|
||||
|
||||
def expected_response_with_pagination_links_and_additional_params
|
||||
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" }
|
||||
{}.tap do |hash|
|
||||
hash[:data] = [data.values.flatten.second]
|
||||
hash.merge! links: new_links
|
||||
end
|
||||
end
|
||||
|
||||
def test_pagination_links_using_kaminari
|
||||
adapter = load_adapter(using_kaminari)
|
||||
|
||||
mock_request
|
||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
||||
end
|
||||
|
||||
def test_pagination_links_using_will_paginate
|
||||
adapter = load_adapter(using_will_paginate)
|
||||
|
||||
mock_request
|
||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
||||
end
|
||||
|
||||
def test_pagination_links_with_additional_params
|
||||
adapter = load_adapter(using_will_paginate)
|
||||
|
||||
mock_request({ test: 'test' })
|
||||
assert_equal expected_response_with_pagination_links_and_additional_params,
|
||||
adapter.serializable_hash(@options)
|
||||
end
|
||||
|
||||
def test_not_showing_pagination_links
|
||||
adapter = load_adapter(@array)
|
||||
|
||||
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user