api-pagination 4.4.0 → 4.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api-pagination/hooks.rb +17 -7
- data/lib/api-pagination/version.rb +1 -1
- data/spec/rails_spec.rb +15 -15
- data/spec/support/numbers_controller.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83806da219ed915e8d9e22d4f88fad6cffb36540
|
4
|
+
data.tar.gz: cb8d29c985ecc0bcafb83d34f31050c3bc0482ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be07abe0cf7d3a3bbc4e8efc3a86e8e38023820788d777db8963873ebd5445c40ce60036d6c82574803a52d60b1ca3bc97b645d96da28770352b46627d4c1c01
|
7
|
+
data.tar.gz: 45b6cca14fe34a4f7abb20b0846c818938c375b8ff1a33b4a22a5a5952921da766f008cf2b390374f570c6a6de103964b26dba9218d5fde6b89ee0cb8536ed18
|
data/lib/api-pagination/hooks.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
begin; require 'rails'; rescue LoadError; end
|
2
|
-
if defined?(ActionController::Base)
|
3
|
-
require 'rails/pagination'
|
4
|
-
ActionController::Base.send(:include, Rails::Pagination)
|
5
|
-
end
|
6
|
-
|
7
2
|
begin; require 'rails-api'; rescue LoadError; end
|
8
|
-
if defined?(
|
3
|
+
if defined?(Rails)
|
9
4
|
require 'rails/pagination'
|
10
|
-
|
5
|
+
|
6
|
+
ActiveSupport.on_load(:action_controller) do
|
7
|
+
ApiPagination::Hooks.rails_parent_controller.send(:include, Rails::Pagination)
|
8
|
+
end
|
11
9
|
end
|
12
10
|
|
13
11
|
begin; require 'grape'; rescue LoadError; end
|
@@ -28,3 +26,15 @@ unless defined?(Kaminari) || defined?(WillPaginate::CollectionMethods)
|
|
28
26
|
gem 'will_paginate'
|
29
27
|
WARNING
|
30
28
|
end
|
29
|
+
|
30
|
+
module ApiPagination
|
31
|
+
module Hooks
|
32
|
+
def self.rails_parent_controller
|
33
|
+
if Gem::Version.new(Rails.version) >= Gem::Version.new('5') || defined?(ActionController::API)
|
34
|
+
ActionController::API
|
35
|
+
else
|
36
|
+
ActionController::Base
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/rails_spec.rb
CHANGED
@@ -13,7 +13,7 @@ describe NumbersController, :type => :controller do
|
|
13
13
|
let(:per_page) { response.headers['Per-Page'].to_i }
|
14
14
|
|
15
15
|
context 'without enough items to give more than one page' do
|
16
|
-
before { get :index, :count
|
16
|
+
before { get :index, params: {count: 10} }
|
17
17
|
|
18
18
|
it 'should not paginate' do
|
19
19
|
expect(response.headers.keys).not_to include('Link')
|
@@ -34,26 +34,26 @@ describe NumbersController, :type => :controller do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
context 'with existing Link headers' do
|
37
|
-
before { get :index, :count
|
37
|
+
before { get :index, params: {count: 30, with_headers: true} }
|
38
38
|
|
39
39
|
it_behaves_like 'an endpoint with existing Link headers'
|
40
40
|
end
|
41
41
|
|
42
42
|
context 'with enough items to paginate' do
|
43
43
|
context 'when on the first page' do
|
44
|
-
before { get :index, :count
|
44
|
+
before { get :index, params: {count: 100} }
|
45
45
|
|
46
46
|
it_behaves_like 'an endpoint with a first page'
|
47
47
|
end
|
48
48
|
|
49
49
|
context 'when on the last page' do
|
50
|
-
before { get :index, :count
|
50
|
+
before { get :index, params: {count: 100, page: 10} }
|
51
51
|
|
52
52
|
it_behaves_like 'an endpoint with a last page'
|
53
53
|
end
|
54
54
|
|
55
55
|
context 'when somewhere comfortably in the middle' do
|
56
|
-
before { get :index, :count
|
56
|
+
before { get :index, params: {count: 100, page: 2} }
|
57
57
|
|
58
58
|
it_behaves_like 'an endpoint with a middle page'
|
59
59
|
end
|
@@ -61,7 +61,7 @@ describe NumbersController, :type => :controller do
|
|
61
61
|
|
62
62
|
context 'providing a block' do
|
63
63
|
it 'yields to the block instead of implicitly rendering' do
|
64
|
-
get :index_with_custom_render, :count
|
64
|
+
get :index_with_custom_render, params: {count: 100}
|
65
65
|
|
66
66
|
json = { numbers: (1..10).map { |n| { number: n } } }.to_json
|
67
67
|
|
@@ -75,7 +75,7 @@ describe NumbersController, :type => :controller do
|
|
75
75
|
ApiPagination.config.per_page_header = 'X-Per-Page'
|
76
76
|
ApiPagination.config.page_header = 'X-Page'
|
77
77
|
|
78
|
-
get :index, count: 10
|
78
|
+
get :index, params: {count: 10}
|
79
79
|
end
|
80
80
|
|
81
81
|
after do
|
@@ -116,7 +116,7 @@ describe NumbersController, :type => :controller do
|
|
116
116
|
before { ApiPagination.config.include_total = false }
|
117
117
|
|
118
118
|
it 'should not include a Total header' do
|
119
|
-
get :index, count: 10
|
119
|
+
get :index, params: {count: 10}
|
120
120
|
|
121
121
|
expect(response.header['Total']).to be_nil
|
122
122
|
end
|
@@ -137,7 +137,7 @@ describe NumbersController, :type => :controller do
|
|
137
137
|
end
|
138
138
|
|
139
139
|
it 'should work' do
|
140
|
-
get :index, :foo
|
140
|
+
get :index, params: {foo: 2, count: 100}
|
141
141
|
|
142
142
|
expect(response.header['Page']).to eq('2')
|
143
143
|
end
|
@@ -158,7 +158,7 @@ describe NumbersController, :type => :controller do
|
|
158
158
|
end
|
159
159
|
|
160
160
|
it 'should work' do
|
161
|
-
get :index, :foo
|
161
|
+
get :index, params: {foo: {bar: 2}, count: 100}
|
162
162
|
|
163
163
|
expect(response.header['Page']).to eq('2')
|
164
164
|
end
|
@@ -176,7 +176,7 @@ describe NumbersController, :type => :controller do
|
|
176
176
|
end
|
177
177
|
|
178
178
|
it 'should work' do
|
179
|
-
get :index_with_no_per_page, :foo
|
179
|
+
get :index_with_no_per_page, params: {foo: 2, count: 100}
|
180
180
|
|
181
181
|
expect(response.header['Per-Page']).to eq('2')
|
182
182
|
end
|
@@ -194,7 +194,7 @@ describe NumbersController, :type => :controller do
|
|
194
194
|
end
|
195
195
|
|
196
196
|
it 'should work' do
|
197
|
-
get :index_with_no_per_page, :foo
|
197
|
+
get :index_with_no_per_page, params: {foo: {bar: 2}, count: 100}
|
198
198
|
|
199
199
|
expect(response.header['Per-Page']).to eq('2')
|
200
200
|
end
|
@@ -211,7 +211,7 @@ describe NumbersController, :type => :controller do
|
|
211
211
|
end
|
212
212
|
|
213
213
|
specify do
|
214
|
-
get :index_with_paginate_array_options, params
|
214
|
+
get :index_with_paginate_array_options, params: params
|
215
215
|
expect(response.header['Total'].to_i).to eq total_header
|
216
216
|
end
|
217
217
|
end
|
@@ -267,7 +267,7 @@ describe NumbersController, :type => :controller do
|
|
267
267
|
end
|
268
268
|
|
269
269
|
it 'should use default per page from model' do
|
270
|
-
get :index_with_no_per_page, count: 100
|
270
|
+
get :index_with_no_per_page, params: {count: 100}
|
271
271
|
|
272
272
|
expect(response.header['Per-Page']).to eq('6')
|
273
273
|
end
|
@@ -278,7 +278,7 @@ describe NumbersController, :type => :controller do
|
|
278
278
|
@per_page = nil
|
279
279
|
end
|
280
280
|
|
281
|
-
get :index_with_no_per_page, count: 100
|
281
|
+
get :index_with_no_per_page, params: {count: 100}
|
282
282
|
|
283
283
|
expect(response.header['Per-Page']).to eq(
|
284
284
|
case ApiPagination.config.paginator
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'action_controller'
|
2
|
+
require 'api-pagination/hooks'
|
2
3
|
require 'ostruct'
|
3
4
|
|
4
5
|
module Rails
|
@@ -58,7 +59,7 @@ class NumbersSerializer
|
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
61
|
-
class NumbersController <
|
62
|
+
class NumbersController < ApiPagination::Hooks.rails_parent_controller
|
62
63
|
include Rails.application.routes.url_helpers
|
63
64
|
|
64
65
|
def index
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|