paginate-responder 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.travis.yml +2 -1
- data/Gemfile +0 -11
- data/README.md +4 -11
- data/Rakefile +1 -0
- data/lib/paginate-responder/adapter/base.rb +40 -0
- data/lib/paginate-responder/adapter/kaminari_adapter.rb +26 -0
- data/lib/paginate-responder/adapter/paginate_adapter.rb +23 -0
- data/lib/paginate-responder/paginator.rb +81 -0
- data/lib/paginate-responder/version.rb +2 -2
- data/lib/paginate-responder.rb +9 -0
- data/lib/responders/paginate_responder.rb +4 -38
- data/paginate-responder.gemspec +10 -3
- data/test/kaminari_responder_test.rb +23 -0
- data/test/paginate_responder_test.rb +6 -132
- data/test/pagination_tests.rb +137 -0
- data/test/test_helper.rb +6 -5
- metadata +107 -9
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -2,14 +2,3 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in paginate-responder.gemspec
|
4
4
|
gemspec
|
5
|
-
|
6
|
-
group :development do
|
7
|
-
gem 'minitest'
|
8
|
-
gem 'rake'
|
9
|
-
gem 'rack-link_headers'
|
10
|
-
end
|
11
|
-
|
12
|
-
group :test do
|
13
|
-
gem 'will_paginate'
|
14
|
-
gem 'actionpack'
|
15
|
-
end
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Paginate::Responder [![Build Status](https://travis-ci.org/jgraichen/paginate-responder.png?branch=master)](https://travis-ci.org/jgraichen/paginate-responder)
|
1
|
+
# Paginate::Responder [![Build Status](https://travis-ci.org/jgraichen/paginate-responder.png?branch=master)](https://travis-ci.org/jgraichen/paginate-responder) [![Code Climate](https://codeclimate.com/github/jgraichen/paginate-responder.png)](https://codeclimate.com/github/jgraichen/paginate-responder)
|
2
2
|
|
3
3
|
A Rails pagination responder with link header support.
|
4
4
|
|
@@ -16,8 +16,9 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install paginate-responder
|
18
18
|
|
19
|
-
You will also need a pagination gem
|
20
|
-
[will_paginate](mislav/will_paginate)
|
19
|
+
You will also need a pagination gem. `PaginateResponder` comes with adapters for
|
20
|
+
* [will_paginate](https://github.com/mislav/will_paginate) and
|
21
|
+
* [kaminari](https://github.com/amatsuda/kaminari).
|
21
22
|
|
22
23
|
## Usage
|
23
24
|
|
@@ -56,14 +57,6 @@ Also a `X-Total-Pages` header will be added with the total
|
|
56
57
|
number of pages if available. This allows applications
|
57
58
|
to display a progress bar or similar while fetching pages.
|
58
59
|
|
59
|
-
`PaginateResponder` should work with any pagination gem that
|
60
|
-
adds a `paginate` method to collections. Tests run with
|
61
|
-
[will_paginate](mislav/will_paginate).
|
62
|
-
|
63
|
-
The `total_pages` method on the collection will be used as
|
64
|
-
total page count. If not total page method is present or
|
65
|
-
nil is returned some link header may be missing.
|
66
|
-
|
67
60
|
## TODOs
|
68
61
|
|
69
62
|
* Documentation
|
data/Rakefile
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
module PaginateResponder::Adapter
|
2
|
+
class Base
|
3
|
+
attr_reader :resource
|
4
|
+
|
5
|
+
def initialize(resource)
|
6
|
+
@resource = resource
|
7
|
+
end
|
8
|
+
|
9
|
+
def paginate!(opts)
|
10
|
+
@resource = paginate(opts)
|
11
|
+
end
|
12
|
+
|
13
|
+
# If pagination for current resource is supported.
|
14
|
+
#
|
15
|
+
def suitable?
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return paginated resource.
|
20
|
+
# Option hash will contain at least <tt>:per_page</tt>
|
21
|
+
# and <tt>:page</tt>.
|
22
|
+
#
|
23
|
+
def paginate(opts)
|
24
|
+
resource
|
25
|
+
end
|
26
|
+
|
27
|
+
# Return number of total pages for current resource.
|
28
|
+
#
|
29
|
+
def total_pages
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return default values for items per page and maximum
|
34
|
+
# items per page.
|
35
|
+
#
|
36
|
+
def defaults
|
37
|
+
{ :per_page => 50, :max_per_page => 100 }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module PaginateResponder::Adapter
|
2
|
+
|
3
|
+
# Pagination adapter for kaminari.
|
4
|
+
#
|
5
|
+
class KaminariAdapter < Base
|
6
|
+
|
7
|
+
def suitable?
|
8
|
+
resource.respond_to?(:page)
|
9
|
+
end
|
10
|
+
|
11
|
+
def paginate(opts)
|
12
|
+
resource.page(opts[:page]).per(opts[:per_page])
|
13
|
+
end
|
14
|
+
|
15
|
+
def defaults
|
16
|
+
{
|
17
|
+
:per_page => Kaminari.config.default_per_page,
|
18
|
+
:max_per_page => Kaminari.config.max_per_page
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def total_pages
|
23
|
+
resource.num_pages if resource.respond_to? :num_pages
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PaginateResponder::Adapter
|
2
|
+
|
3
|
+
# Pagination adapter for will_paginate.
|
4
|
+
#
|
5
|
+
class PaginateAdapter < Base
|
6
|
+
|
7
|
+
def suitable?
|
8
|
+
resource.respond_to?(:paginate)
|
9
|
+
end
|
10
|
+
|
11
|
+
def paginate(opts)
|
12
|
+
resource.paginate :page => opts[:page], :per_page => opts[:per_page]
|
13
|
+
end
|
14
|
+
|
15
|
+
def defaults
|
16
|
+
{ :per_page => 50, :max_per_page => 50 }
|
17
|
+
end
|
18
|
+
|
19
|
+
def total_pages
|
20
|
+
resource.total_pages if resource.respond_to? :total_pages
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module PaginateResponder
|
2
|
+
class Paginator
|
3
|
+
attr_reader :responder, :adapter, :resource
|
4
|
+
|
5
|
+
def initialize(responder)
|
6
|
+
@responder = responder
|
7
|
+
@resource = responder.resource
|
8
|
+
@adapter = find_adapter
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_adapter
|
12
|
+
return controller.pagination_adapter(resource) if controller.respond_to? :pagination_adapter
|
13
|
+
|
14
|
+
Adapter::Base.subclasses.each do |adapter_class|
|
15
|
+
begin
|
16
|
+
adapter_class.new(resource).tap do |adapter|
|
17
|
+
return adapter if adapter.suitable?
|
18
|
+
end
|
19
|
+
rescue
|
20
|
+
next
|
21
|
+
end
|
22
|
+
end
|
23
|
+
Adapter::Base.new(resource)
|
24
|
+
end
|
25
|
+
|
26
|
+
def request; responder.request end
|
27
|
+
def response; controller.response end
|
28
|
+
def controller; responder.controller end
|
29
|
+
|
30
|
+
def paginate!
|
31
|
+
resource!
|
32
|
+
headers!
|
33
|
+
|
34
|
+
resource
|
35
|
+
end
|
36
|
+
|
37
|
+
def headers!
|
38
|
+
link! 'first', 1
|
39
|
+
link! 'prev', page - 1 if page > 1
|
40
|
+
link! 'next', page + 1 if total_pages && page < total_pages
|
41
|
+
link! 'last', total_pages if total_pages
|
42
|
+
|
43
|
+
response.headers["X-Total-Pages"] = total_pages.to_s if total_pages
|
44
|
+
end
|
45
|
+
|
46
|
+
def link!(rel, page)
|
47
|
+
response.link(controller.url_for(request.params.merge(:page => page)), :rel => rel)
|
48
|
+
end
|
49
|
+
|
50
|
+
def resource!
|
51
|
+
@resource = @adapter.paginate! :page => page, :per_page => per_page
|
52
|
+
end
|
53
|
+
|
54
|
+
def page
|
55
|
+
@page ||= controller.page.try(:to_i) if controller.respond_to? :page
|
56
|
+
@page ||= controller.params[:page].try(:to_i)
|
57
|
+
@page ||= 1
|
58
|
+
rescue
|
59
|
+
1
|
60
|
+
end
|
61
|
+
|
62
|
+
def per_page
|
63
|
+
@per_page ||= controller.per_page.try(:to_i) if controller.respond_to? :per_page
|
64
|
+
@per_page ||= controller.params[:per_page].try(:to_i)
|
65
|
+
@per_page ||= @adapter.defaults[:per_page].try(:to_i)
|
66
|
+
@per_page = [[1, @per_page].max, max_per_page].min
|
67
|
+
rescue
|
68
|
+
max_per_page
|
69
|
+
end
|
70
|
+
|
71
|
+
def max_per_page
|
72
|
+
@max_per_page ||= controller.max_per_page.try(:to_i) if controller.respond_to? :max_per_page
|
73
|
+
@max_per_page ||= @adapter.defaults[:max_per_page].try(:to_i)
|
74
|
+
@max_per_page ||= 50
|
75
|
+
end
|
76
|
+
|
77
|
+
def total_pages
|
78
|
+
@adapter.total_pages
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/paginate-responder.rb
CHANGED
@@ -6,4 +6,13 @@ end
|
|
6
6
|
|
7
7
|
module PaginateResponder
|
8
8
|
autoload :VERSION, 'paginate-responder/version'
|
9
|
+
autoload :Paginator, 'paginate-responder/paginator'
|
10
|
+
|
11
|
+
module Adapter
|
12
|
+
autoload :Base, 'paginate-responder/adapter/base'
|
13
|
+
autoload :Internal, 'paginate-responder/adapter/internal'
|
14
|
+
end
|
9
15
|
end
|
16
|
+
|
17
|
+
require 'paginate-responder/adapter/paginate_adapter'
|
18
|
+
require 'paginate-responder/adapter/kaminari_adapter'
|
@@ -1,46 +1,12 @@
|
|
1
|
-
|
2
1
|
module Responders
|
2
|
+
|
3
3
|
module PaginateResponder
|
4
|
-
def to_format
|
5
|
-
if get? && resource.respond_to?(:paginate)
|
6
|
-
@resource = resource.paginate :page => self.page, :per_page => self.per_page
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
controller.response.link(controller.url_for(request.params.merge(:page => total_pages)), :rel => "last") if total_pages
|
12
|
-
controller.response.headers["X-Total-Pages"] = total_pages.to_s if total_pages
|
5
|
+
def to_format
|
6
|
+
if get?
|
7
|
+
@resource = ::PaginateResponder::Paginator.new(self).paginate!
|
13
8
|
end
|
14
9
|
super
|
15
10
|
end
|
16
|
-
|
17
|
-
def page
|
18
|
-
@page ||= controller.page if controller.respond_to? :page
|
19
|
-
@page ||= controller.params[:page].try(:to_i)
|
20
|
-
@page ||= 1
|
21
|
-
@page
|
22
|
-
rescue
|
23
|
-
1
|
24
|
-
end
|
25
|
-
|
26
|
-
def total_pages
|
27
|
-
@total_pages ||= resource.total_pages if resource.respond_to? :total_pages
|
28
|
-
@total_pages
|
29
|
-
end
|
30
|
-
|
31
|
-
def per_page
|
32
|
-
@per_page ||= controller.per_page if controller.respond_to? :per_page
|
33
|
-
@per_page ||= controller.params[:per_page].try(:to_i)
|
34
|
-
@per_page = [[1, @per_page].max, max_per_page].min
|
35
|
-
@per_page
|
36
|
-
rescue
|
37
|
-
max_per_page
|
38
|
-
end
|
39
|
-
|
40
|
-
def max_per_page
|
41
|
-
@max_per_page ||= controller.max_per_page if controller.respond_to? :max_per_page
|
42
|
-
@max_per_page ||= 50
|
43
|
-
@max_per_page
|
44
|
-
end
|
45
11
|
end
|
46
12
|
end
|
data/paginate-responder.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'paginate-responder/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "paginate-responder"
|
8
8
|
gem.version = PaginateResponder::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
9
|
+
gem.authors = ['Jan Graichen']
|
10
|
+
gem.email = ['jg@altimos.de']
|
11
11
|
gem.description = %q{A Rails pagination responder with link header support.}
|
12
12
|
gem.summary = %q{A Rails pagination responder with link header support.}
|
13
13
|
gem.homepage = ""
|
@@ -15,7 +15,14 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.require_paths = [
|
18
|
+
gem.require_paths = ['lib']
|
19
19
|
|
20
20
|
gem.add_dependency 'rack-link_headers', '>= 2.2'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'actionpack', '>= 3.2.0'
|
24
|
+
gem.add_development_dependency 'minitest'
|
25
|
+
gem.add_development_dependency 'minitest-reporters', '>= 0.8.0'
|
26
|
+
gem.add_development_dependency 'will_paginate'
|
27
|
+
gem.add_development_dependency 'kaminari'
|
21
28
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper.rb'
|
2
|
+
require 'pagination_tests'
|
3
|
+
|
4
|
+
require 'kaminari/config'
|
5
|
+
require 'kaminari/helpers/paginator'
|
6
|
+
require 'kaminari/models/page_scope_methods'
|
7
|
+
require 'kaminari/models/configuration_methods'
|
8
|
+
require 'kaminari/models/array_extension'
|
9
|
+
|
10
|
+
Kaminari.configure do |config|
|
11
|
+
config.default_per_page = 50
|
12
|
+
config.max_per_page = 50
|
13
|
+
end
|
14
|
+
|
15
|
+
class KaminariResponderTest < ActionController::TestCase
|
16
|
+
tests PaginateController
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@controller.resource = Kaminari.paginate_array(resource)
|
20
|
+
end
|
21
|
+
|
22
|
+
include PaginationTests
|
23
|
+
end
|
@@ -1,140 +1,14 @@
|
|
1
1
|
require 'test_helper.rb'
|
2
|
+
require 'pagination_tests'
|
3
|
+
|
4
|
+
require 'will_paginate/array'
|
2
5
|
|
3
6
|
class PaginateResponderTest < ActionController::TestCase
|
4
7
|
tests PaginateController
|
5
8
|
|
6
|
-
def
|
7
|
-
|
8
|
-
def test_pagination
|
9
|
-
get :index, :format => :json
|
10
|
-
|
11
|
-
assert_equal 50, json.size
|
12
|
-
assert_equal ('AA'..'zz').to_a[0..49], json
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_pagination_page_2
|
16
|
-
get :index, :format => :json, :page => 2
|
17
|
-
|
18
|
-
assert_equal 50, json.size
|
19
|
-
assert_equal ('AA'..'zz').to_a[50..99], json
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_pagination_per_page
|
23
|
-
get :index, :format => :json, :page => 1, :per_page => 10
|
24
|
-
|
25
|
-
assert_equal 10, json.size
|
26
|
-
assert_equal ('AA'..'zz').to_a[0..9], json
|
9
|
+
def setup
|
10
|
+
@controller.resource = resource
|
27
11
|
end
|
28
12
|
|
29
|
-
|
30
|
-
get :index, :format => :json, :page => 2, :per_page => 10
|
31
|
-
|
32
|
-
assert_equal 10, json.size
|
33
|
-
assert_equal ('AA'..'zz').to_a[10..19], json
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_headers
|
37
|
-
get :index, :format => :json
|
38
|
-
|
39
|
-
assert_equal 3, response.links.size
|
40
|
-
|
41
|
-
assert_equal "first", response.links[0][:params][:rel]
|
42
|
-
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
43
|
-
|
44
|
-
assert_equal "next", response.links[1][:params][:rel]
|
45
|
-
assert_equal "http://test.host/index.json?page=2", response.links[1][:url]
|
46
|
-
|
47
|
-
assert_equal "last", response.links[2][:params][:rel]
|
48
|
-
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_headers_page_2
|
52
|
-
get :index, :format => :json, :page => 2
|
53
|
-
|
54
|
-
assert_equal 4, response.links.size
|
55
|
-
|
56
|
-
assert_equal "first", response.links[0][:params][:rel]
|
57
|
-
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
58
|
-
|
59
|
-
assert_equal "prev", response.links[1][:params][:rel]
|
60
|
-
assert_equal "http://test.host/index.json?page=1", response.links[1][:url]
|
61
|
-
|
62
|
-
assert_equal "next", response.links[2][:params][:rel]
|
63
|
-
assert_equal "http://test.host/index.json?page=3", response.links[2][:url]
|
64
|
-
|
65
|
-
assert_equal "last", response.links[3][:params][:rel]
|
66
|
-
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_headers_page_5
|
70
|
-
get :index, :format => :json, :page => 5
|
71
|
-
|
72
|
-
assert_equal 4, response.links.size
|
73
|
-
|
74
|
-
assert_equal "first", response.links[0][:params][:rel]
|
75
|
-
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
76
|
-
|
77
|
-
assert_equal "prev", response.links[1][:params][:rel]
|
78
|
-
assert_equal "http://test.host/index.json?page=4", response.links[1][:url]
|
79
|
-
|
80
|
-
assert_equal "next", response.links[2][:params][:rel]
|
81
|
-
assert_equal "http://test.host/index.json?page=6", response.links[2][:url]
|
82
|
-
|
83
|
-
assert_equal "last", response.links[3][:params][:rel]
|
84
|
-
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_headers_last_page
|
88
|
-
get :index, :format => :json, :page => 14
|
89
|
-
|
90
|
-
assert_equal 3, response.links.size
|
91
|
-
|
92
|
-
assert_equal "first", response.links[0][:params][:rel]
|
93
|
-
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
94
|
-
|
95
|
-
assert_equal "prev", response.links[1][:params][:rel]
|
96
|
-
assert_equal "http://test.host/index.json?page=13", response.links[1][:url]
|
97
|
-
|
98
|
-
assert_equal "last", response.links[2][:params][:rel]
|
99
|
-
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_headers_page_before_last_page
|
103
|
-
get :index, :format => :json, :page => 13
|
104
|
-
|
105
|
-
assert_equal 4, response.links.size
|
106
|
-
|
107
|
-
assert_equal "first", response.links[0][:params][:rel]
|
108
|
-
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
109
|
-
|
110
|
-
assert_equal "prev", response.links[1][:params][:rel]
|
111
|
-
assert_equal "http://test.host/index.json?page=12", response.links[1][:url]
|
112
|
-
|
113
|
-
assert_equal "next", response.links[2][:params][:rel]
|
114
|
-
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
115
|
-
|
116
|
-
assert_equal "last", response.links[3][:params][:rel]
|
117
|
-
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_headers_per_page
|
121
|
-
get :index, :format => :json, :page => 1, :per_page => 10
|
122
|
-
|
123
|
-
assert_equal 3, response.links.size
|
124
|
-
|
125
|
-
assert_equal "first", response.links[0][:params][:rel]
|
126
|
-
assert_equal "http://test.host/index.json?page=1&per_page=10", response.links[0][:url]
|
127
|
-
|
128
|
-
assert_equal "next", response.links[1][:params][:rel]
|
129
|
-
assert_equal "http://test.host/index.json?page=2&per_page=10", response.links[1][:url]
|
130
|
-
|
131
|
-
assert_equal "last", response.links[2][:params][:rel]
|
132
|
-
assert_equal "http://test.host/index.json?page=68&per_page=10", response.links[2][:url]
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_headers_total_pages
|
136
|
-
get :index, :format => :json
|
137
|
-
|
138
|
-
assert_equal "14", response.headers["X-Total-Pages"]
|
139
|
-
end
|
13
|
+
include PaginationTests
|
140
14
|
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
module PaginationTests
|
2
|
+
def json; JSON[@response.body] end
|
3
|
+
def resource; ('AA'..'zz').to_a end
|
4
|
+
|
5
|
+
def test_pagination
|
6
|
+
get :index, :format => :json
|
7
|
+
|
8
|
+
assert_equal 50, json.size
|
9
|
+
assert_equal ('AA'..'zz').to_a[0..49], json
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_pagination_page_2
|
13
|
+
get :index, :format => :json, :page => 2
|
14
|
+
|
15
|
+
assert_equal 50, json.size
|
16
|
+
assert_equal ('AA'..'zz').to_a[50..99], json
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_pagination_per_page
|
20
|
+
get :index, :format => :json, :page => 1, :per_page => 10
|
21
|
+
|
22
|
+
assert_equal 10, json.size
|
23
|
+
assert_equal ('AA'..'zz').to_a[0..9], json
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_pagination_per_page_2
|
27
|
+
get :index, :format => :json, :page => 2, :per_page => 10
|
28
|
+
|
29
|
+
assert_equal 10, json.size
|
30
|
+
assert_equal ('AA'..'zz').to_a[10..19], json
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_headers
|
34
|
+
get :index, :format => :json
|
35
|
+
|
36
|
+
assert_equal 3, response.links.size
|
37
|
+
|
38
|
+
assert_equal "first", response.links[0][:params][:rel]
|
39
|
+
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
40
|
+
|
41
|
+
assert_equal "next", response.links[1][:params][:rel]
|
42
|
+
assert_equal "http://test.host/index.json?page=2", response.links[1][:url]
|
43
|
+
|
44
|
+
assert_equal "last", response.links[2][:params][:rel]
|
45
|
+
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_headers_page_2
|
49
|
+
get :index, :format => :json, :page => 2
|
50
|
+
|
51
|
+
assert_equal 4, response.links.size
|
52
|
+
|
53
|
+
assert_equal "first", response.links[0][:params][:rel]
|
54
|
+
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
55
|
+
|
56
|
+
assert_equal "prev", response.links[1][:params][:rel]
|
57
|
+
assert_equal "http://test.host/index.json?page=1", response.links[1][:url]
|
58
|
+
|
59
|
+
assert_equal "next", response.links[2][:params][:rel]
|
60
|
+
assert_equal "http://test.host/index.json?page=3", response.links[2][:url]
|
61
|
+
|
62
|
+
assert_equal "last", response.links[3][:params][:rel]
|
63
|
+
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_headers_page_5
|
67
|
+
get :index, :format => :json, :page => 5
|
68
|
+
|
69
|
+
assert_equal 4, response.links.size
|
70
|
+
|
71
|
+
assert_equal "first", response.links[0][:params][:rel]
|
72
|
+
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
73
|
+
|
74
|
+
assert_equal "prev", response.links[1][:params][:rel]
|
75
|
+
assert_equal "http://test.host/index.json?page=4", response.links[1][:url]
|
76
|
+
|
77
|
+
assert_equal "next", response.links[2][:params][:rel]
|
78
|
+
assert_equal "http://test.host/index.json?page=6", response.links[2][:url]
|
79
|
+
|
80
|
+
assert_equal "last", response.links[3][:params][:rel]
|
81
|
+
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_headers_last_page
|
85
|
+
get :index, :format => :json, :page => 14
|
86
|
+
|
87
|
+
assert_equal 3, response.links.size
|
88
|
+
|
89
|
+
assert_equal "first", response.links[0][:params][:rel]
|
90
|
+
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
91
|
+
|
92
|
+
assert_equal "prev", response.links[1][:params][:rel]
|
93
|
+
assert_equal "http://test.host/index.json?page=13", response.links[1][:url]
|
94
|
+
|
95
|
+
assert_equal "last", response.links[2][:params][:rel]
|
96
|
+
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_headers_page_before_last_page
|
100
|
+
get :index, :format => :json, :page => 13
|
101
|
+
|
102
|
+
assert_equal 4, response.links.size
|
103
|
+
|
104
|
+
assert_equal "first", response.links[0][:params][:rel]
|
105
|
+
assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
|
106
|
+
|
107
|
+
assert_equal "prev", response.links[1][:params][:rel]
|
108
|
+
assert_equal "http://test.host/index.json?page=12", response.links[1][:url]
|
109
|
+
|
110
|
+
assert_equal "next", response.links[2][:params][:rel]
|
111
|
+
assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
|
112
|
+
|
113
|
+
assert_equal "last", response.links[3][:params][:rel]
|
114
|
+
assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_headers_per_page
|
118
|
+
get :index, :format => :json, :page => 1, :per_page => 10
|
119
|
+
|
120
|
+
assert_equal 3, response.links.size
|
121
|
+
|
122
|
+
assert_equal "first", response.links[0][:params][:rel]
|
123
|
+
assert_equal "http://test.host/index.json?page=1&per_page=10", response.links[0][:url]
|
124
|
+
|
125
|
+
assert_equal "next", response.links[1][:params][:rel]
|
126
|
+
assert_equal "http://test.host/index.json?page=2&per_page=10", response.links[1][:url]
|
127
|
+
|
128
|
+
assert_equal "last", response.links[2][:params][:rel]
|
129
|
+
assert_equal "http://test.host/index.json?page=68&per_page=10", response.links[2][:url]
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_headers_total_pages
|
133
|
+
get :index, :format => :json
|
134
|
+
|
135
|
+
assert_equal "14", response.headers["X-Total-Pages"]
|
136
|
+
end
|
137
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -8,7 +8,8 @@ ENV["RAILS_ENV"] = "test"
|
|
8
8
|
|
9
9
|
require 'active_support'
|
10
10
|
require 'action_controller'
|
11
|
-
require '
|
11
|
+
require 'minitest/reporters'
|
12
|
+
MiniTest::Reporters.use!
|
12
13
|
|
13
14
|
require 'paginate-responder'
|
14
15
|
|
@@ -23,17 +24,17 @@ class ActiveSupport::TestCase
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
class
|
27
|
+
class TestResponder < ActionController::Responder
|
27
28
|
include Responders::PaginateResponder
|
28
29
|
end
|
29
30
|
|
30
31
|
class PaginateController < ActionController::Base
|
32
|
+
attr_accessor :resource
|
31
33
|
include Responders::Routes.url_helpers
|
32
|
-
|
33
|
-
self.responder = PaginationResponder
|
34
|
+
self.responder = TestResponder
|
34
35
|
respond_to :json
|
35
36
|
|
36
37
|
def index
|
37
|
-
respond_with
|
38
|
+
respond_with resource
|
38
39
|
end
|
39
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paginate-responder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack-link_headers
|
@@ -27,6 +27,102 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '2.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: actionpack
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.2.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest-reporters
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.8.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.8.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: will_paginate
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: kaminari
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
30
126
|
description: A Rails pagination responder with link header support.
|
31
127
|
email:
|
32
128
|
- jg@altimos.de
|
@@ -41,10 +137,16 @@ files:
|
|
41
137
|
- README.md
|
42
138
|
- Rakefile
|
43
139
|
- lib/paginate-responder.rb
|
140
|
+
- lib/paginate-responder/adapter/base.rb
|
141
|
+
- lib/paginate-responder/adapter/kaminari_adapter.rb
|
142
|
+
- lib/paginate-responder/adapter/paginate_adapter.rb
|
143
|
+
- lib/paginate-responder/paginator.rb
|
44
144
|
- lib/paginate-responder/version.rb
|
45
145
|
- lib/responders/paginate_responder.rb
|
46
146
|
- paginate-responder.gemspec
|
147
|
+
- test/kaminari_responder_test.rb
|
47
148
|
- test/paginate_responder_test.rb
|
149
|
+
- test/pagination_tests.rb
|
48
150
|
- test/test_helper.rb
|
49
151
|
homepage: ''
|
50
152
|
licenses: []
|
@@ -58,24 +160,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
160
|
- - ! '>='
|
59
161
|
- !ruby/object:Gem::Version
|
60
162
|
version: '0'
|
61
|
-
segments:
|
62
|
-
- 0
|
63
|
-
hash: 3362821720286290426
|
64
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
164
|
none: false
|
66
165
|
requirements:
|
67
166
|
- - ! '>='
|
68
167
|
- !ruby/object:Gem::Version
|
69
168
|
version: '0'
|
70
|
-
segments:
|
71
|
-
- 0
|
72
|
-
hash: 3362821720286290426
|
73
169
|
requirements: []
|
74
170
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.8.
|
171
|
+
rubygems_version: 1.8.25
|
76
172
|
signing_key:
|
77
173
|
specification_version: 3
|
78
174
|
summary: A Rails pagination responder with link header support.
|
79
175
|
test_files:
|
176
|
+
- test/kaminari_responder_test.rb
|
80
177
|
- test/paginate_responder_test.rb
|
178
|
+
- test/pagination_tests.rb
|
81
179
|
- test/test_helper.rb
|