paginate-responder 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - jruby
5
4
  - 1.9.3
5
+ env:
6
+ - GEM=will_paginate
7
+ - GEM=kaminari
data/README.md CHANGED
@@ -20,6 +20,8 @@ You will also need a pagination gem. `PaginateResponder` comes with adapters for
20
20
  * [will_paginate](https://github.com/mislav/will_paginate) and
21
21
  * [kaminari](https://github.com/amatsuda/kaminari).
22
22
 
23
+ It is recommended to use only on pagination gem at once.
24
+
23
25
  ## Usage
24
26
 
25
27
  Add `Responders::PaginateResponder` to your responder chain:
@@ -10,7 +10,6 @@ module PaginateResponder
10
10
 
11
11
  module Adapter
12
12
  autoload :Base, 'paginate-responder/adapter/base'
13
- autoload :Internal, 'paginate-responder/adapter/internal'
14
13
  end
15
14
  end
16
15
 
@@ -5,7 +5,7 @@ module PaginateResponder::Adapter
5
5
  class KaminariAdapter < Base
6
6
 
7
7
  def suitable?
8
- resource.respond_to?(:page)
8
+ defined?(:Kaminari) and resource.respond_to?(:page) and not resource.respond_to?(:paginate)
9
9
  end
10
10
 
11
11
  def paginate(opts)
@@ -5,7 +5,7 @@ module PaginateResponder::Adapter
5
5
  class PaginateAdapter < Base
6
6
 
7
7
  def suitable?
8
- resource.respond_to?(:paginate)
8
+ defined?(:WillPaginate) and resource.respond_to?(:paginate)
9
9
  end
10
10
 
11
11
  def paginate(opts)
@@ -1,7 +1,7 @@
1
1
  module PaginateResponder
2
2
  module VERSION
3
3
  MAJOR = 1
4
- MINOR = 2
4
+ MINOR = 3
5
5
  PATCH = 0
6
6
  STAGE = nil
7
7
 
@@ -21,6 +21,8 @@ Gem::Specification.new do |gem|
21
21
 
22
22
  gem.add_development_dependency 'rake'
23
23
  gem.add_development_dependency 'actionpack', '>= 3.2.0'
24
+ gem.add_development_dependency 'activerecord', '>= 3.2.0'
25
+ gem.add_development_dependency 'sqlite3'
24
26
  gem.add_development_dependency 'minitest'
25
27
  gem.add_development_dependency 'minitest-reporters', '>= 0.8.0'
26
28
  gem.add_development_dependency 'will_paginate'
@@ -1,14 +1,214 @@
1
1
  require 'test_helper.rb'
2
- require 'pagination_tests'
3
2
 
4
- require 'will_paginate/array'
3
+ GEM = ENV['GEM'].to_s.split ','
4
+ GEM = [ 'will_paginate' ] if GEM.empty?
5
+ puts "[INFO] Running tests with #{GEM.join(' and ')}."
5
6
 
6
7
  class PaginateResponderTest < ActionController::TestCase
7
8
  tests PaginateController
8
9
 
10
+ def array_resource
11
+ ('AA'..'zz').to_a
12
+ end
13
+
14
+ GEM.each do |gem|
15
+ case gem
16
+ when 'will_paginate'
17
+ require 'will_paginate/array'
18
+ require 'will_paginate/active_record'
19
+ when 'kaminari'
20
+ require 'kaminari'
21
+ require 'kaminari/models/array_extension'
22
+ Kaminari::Hooks.init
23
+
24
+ Kaminari.configure do |config|
25
+ config.default_per_page = 50
26
+ config.max_per_page = 50
27
+ end
28
+
29
+ def array_resource
30
+ Kaminari.paginate_array ('AA'..'zz').to_a
31
+ end
32
+ else
33
+ end
34
+ end
35
+
9
36
  def setup
10
- @controller.resource = resource
37
+ @controller.resource = ArModel.scoped
38
+ end
39
+ def json; JSON[@response.body] end
40
+
41
+ def test_pagination
42
+ get :index, :format => :json
43
+
44
+ assert_equal 50, json.size
45
+ assert_equal (1..50).to_a, json
46
+ end
47
+
48
+ def test_pagination_arr
49
+ @controller.resource = array_resource
50
+ get :index, :format => :json
51
+
52
+ assert_equal 50, json.size
53
+ assert_equal ('AA'..'zz').to_a[0..49], json
54
+ end
55
+
56
+ def test_pagination_assoc
57
+ @controller.resource = ArModel.find(1).ar_assoc_models
58
+ get :index, :format => :json
59
+
60
+ assert_equal 5, json.size
61
+ assert_equal (1..5).to_a, json
62
+ end
63
+
64
+ def test_pagination_page_2
65
+ get :index, :format => :json, :page => 2
66
+
67
+ assert_equal 50, json.size
68
+ assert_equal (51..100).to_a, json
69
+ end
70
+
71
+ def test_pagination_arr_page_2
72
+ @controller.resource = array_resource
73
+ get :index, :format => :json, :page => 2
74
+
75
+ assert_equal 50, json.size
76
+ assert_equal ('AA'..'zz').to_a[50..99], json
77
+ end
78
+
79
+ def test_pagination_per_page
80
+ get :index, :format => :json, :page => 1, :per_page => 10
81
+
82
+ assert_equal 10, json.size
83
+ assert_equal (1..10).to_a, json
11
84
  end
12
85
 
13
- include PaginationTests
86
+
87
+ def test_pagination_arr_per_page
88
+ @controller.resource = array_resource
89
+ get :index, :format => :json, :page => 1, :per_page => 10
90
+
91
+ assert_equal 10, json.size
92
+ assert_equal ('AA'..'zz').to_a[0..9], json
93
+ end
94
+
95
+ def test_pagination_per_page_2
96
+ get :index, :format => :json, :page => 2, :per_page => 10
97
+
98
+ assert_equal 10, json.size
99
+ assert_equal (11..20).to_a, json
100
+ end
101
+
102
+ def test_pagination_arr_per_page_2
103
+ @controller.resource = array_resource
104
+ get :index, :format => :json, :page => 2, :per_page => 10
105
+
106
+ assert_equal 10, json.size
107
+ assert_equal ('AA'..'zz').to_a[10..19], json
108
+ end
109
+
110
+ def test_headers
111
+ get :index, :format => :json
112
+
113
+ assert_equal 3, response.links.size
114
+
115
+ assert_equal "first", response.links[0][:params][:rel]
116
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
117
+
118
+ assert_equal "next", response.links[1][:params][:rel]
119
+ assert_equal "http://test.host/index.json?page=2", response.links[1][:url]
120
+
121
+ assert_equal "last", response.links[2][:params][:rel]
122
+ assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
123
+ end
124
+
125
+ def test_headers_page_2
126
+ get :index, :format => :json, :page => 2
127
+
128
+ assert_equal 4, response.links.size
129
+
130
+ assert_equal "first", response.links[0][:params][:rel]
131
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
132
+
133
+ assert_equal "prev", response.links[1][:params][:rel]
134
+ assert_equal "http://test.host/index.json?page=1", response.links[1][:url]
135
+
136
+ assert_equal "next", response.links[2][:params][:rel]
137
+ assert_equal "http://test.host/index.json?page=3", response.links[2][:url]
138
+
139
+ assert_equal "last", response.links[3][:params][:rel]
140
+ assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
141
+ end
142
+
143
+ def test_headers_page_5
144
+ get :index, :format => :json, :page => 5
145
+
146
+ assert_equal 4, response.links.size
147
+
148
+ assert_equal "first", response.links[0][:params][:rel]
149
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
150
+
151
+ assert_equal "prev", response.links[1][:params][:rel]
152
+ assert_equal "http://test.host/index.json?page=4", response.links[1][:url]
153
+
154
+ assert_equal "next", response.links[2][:params][:rel]
155
+ assert_equal "http://test.host/index.json?page=6", response.links[2][:url]
156
+
157
+ assert_equal "last", response.links[3][:params][:rel]
158
+ assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
159
+ end
160
+
161
+ def test_headers_last_page
162
+ get :index, :format => :json, :page => 14
163
+
164
+ assert_equal 3, response.links.size
165
+
166
+ assert_equal "first", response.links[0][:params][:rel]
167
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
168
+
169
+ assert_equal "prev", response.links[1][:params][:rel]
170
+ assert_equal "http://test.host/index.json?page=13", response.links[1][:url]
171
+
172
+ assert_equal "last", response.links[2][:params][:rel]
173
+ assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
174
+ end
175
+
176
+ def test_headers_page_before_last_page
177
+ get :index, :format => :json, :page => 13
178
+
179
+ assert_equal 4, response.links.size
180
+
181
+ assert_equal "first", response.links[0][:params][:rel]
182
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
183
+
184
+ assert_equal "prev", response.links[1][:params][:rel]
185
+ assert_equal "http://test.host/index.json?page=12", response.links[1][:url]
186
+
187
+ assert_equal "next", response.links[2][:params][:rel]
188
+ assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
189
+
190
+ assert_equal "last", response.links[3][:params][:rel]
191
+ assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
192
+ end
193
+
194
+ def test_headers_per_page
195
+ get :index, :format => :json, :page => 1, :per_page => 10
196
+
197
+ assert_equal 3, response.links.size
198
+
199
+ assert_equal "first", response.links[0][:params][:rel]
200
+ assert_equal "http://test.host/index.json?page=1&per_page=10", response.links[0][:url]
201
+
202
+ assert_equal "next", response.links[1][:params][:rel]
203
+ assert_equal "http://test.host/index.json?page=2&per_page=10", response.links[1][:url]
204
+
205
+ assert_equal "last", response.links[2][:params][:rel]
206
+ assert_equal "http://test.host/index.json?page=68&per_page=10", response.links[2][:url]
207
+ end
208
+
209
+ def test_headers_total_pages
210
+ get :index, :format => :json
211
+
212
+ assert_equal "14", response.headers["X-Total-Pages"]
213
+ end
14
214
  end
data/test/test_helper.rb CHANGED
@@ -8,6 +8,7 @@ ENV["RAILS_ENV"] = "test"
8
8
 
9
9
  require 'active_support'
10
10
  require 'action_controller'
11
+ require 'active_record'
11
12
  require 'minitest/reporters'
12
13
  MiniTest::Reporters.use!
13
14
 
@@ -24,6 +25,31 @@ class ActiveSupport::TestCase
24
25
  end
25
26
  end
26
27
 
28
+ class ArModel < ActiveRecord::Base
29
+ has_many :ar_assoc_models
30
+ def as_json(opts = {}); self.id end
31
+ end
32
+
33
+ class ArAssocModel < ActiveRecord::Base
34
+ def as_json(opts = {}); self.id end
35
+ end
36
+
37
+ ActiveRecord::Base.establish_connection(
38
+ :adapter => 'sqlite3',
39
+ :database => ':memory:'
40
+ )
41
+ ActiveRecord::Base.connection.execute(
42
+ 'CREATE TABLE ar_models (id INTEGER PRIMARY KEY AUTOINCREMENT);')
43
+ ActiveRecord::Base.connection.execute(
44
+ 'CREATE TABLE ar_assoc_models (id INTEGER PRIMARY KEY AUTOINCREMENT, ar_model_id INTEGER);')
45
+ 676.times do
46
+ ArModel.create!.tap do |ar_model|
47
+ 5.times do
48
+ ar_model.ar_assoc_models.create!
49
+ end
50
+ end
51
+ end
52
+
27
53
  class TestResponder < ActionController::Responder
28
54
  include Responders::PaginateResponder
29
55
  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.2.0
4
+ version: 1.3.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-02-25 00:00:00.000000000 Z
12
+ date: 2013-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack-link_headers
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.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: 3.2.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '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'
62
94
  - !ruby/object:Gem::Dependency
63
95
  name: minitest
64
96
  requirement: !ruby/object:Gem::Requirement
@@ -144,9 +176,7 @@ files:
144
176
  - lib/paginate-responder/version.rb
145
177
  - lib/responders/paginate_responder.rb
146
178
  - paginate-responder.gemspec
147
- - test/kaminari_responder_test.rb
148
179
  - test/paginate_responder_test.rb
149
- - test/pagination_tests.rb
150
180
  - test/test_helper.rb
151
181
  homepage: ''
152
182
  licenses: []
@@ -173,7 +203,5 @@ signing_key:
173
203
  specification_version: 3
174
204
  summary: A Rails pagination responder with link header support.
175
205
  test_files:
176
- - test/kaminari_responder_test.rb
177
206
  - test/paginate_responder_test.rb
178
- - test/pagination_tests.rb
179
207
  - test/test_helper.rb
@@ -1,23 +0,0 @@
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,137 +0,0 @@
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