api-pagination 4.3.0 → 4.4.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/version.rb +1 -1
- data/lib/rails/pagination.rb +14 -10
- data/spec/rails_spec.rb +46 -0
- data/spec/spec_helper.rb +5 -6
- data/spec/support/numbers_controller.rb +14 -2
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8bd6ee617ad9ee01273cb7b36548cdb197110aa
|
4
|
+
data.tar.gz: b24a83cb8d231b98d7ed1670adb7f23680a80210
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 729785a6c4835d934f1f6fdab6159ca54f2003aecb4368774581478bb40640b62b1790b332e9d567c2b02b55ae980bd37ae6b01b30fbf5288657f2c87e111cce
|
7
|
+
data.tar.gz: aba98e01b6082d375864506e0ba1d6d6483961e6ef6d5bc61f8c4662fa81b6a785b5ca21029d237dead8e4a8aad28309c8f4c28e9be3924a53cd2eb00362e8c1
|
data/lib/rails/pagination.rb
CHANGED
@@ -24,13 +24,9 @@ module Rails
|
|
24
24
|
private
|
25
25
|
|
26
26
|
def _paginate_collection(collection, options={})
|
27
|
-
options =
|
28
|
-
|
29
|
-
|
30
|
-
options.delete(:per_page) ||
|
31
|
-
ApiPagination.config.per_page_param(params)
|
32
|
-
)
|
33
|
-
}
|
27
|
+
options[:page] = ApiPagination.config.page_param(params)
|
28
|
+
options[:per_page] ||= ApiPagination.config.per_page_param(params)
|
29
|
+
|
34
30
|
collection = ApiPagination.paginate(collection, options)
|
35
31
|
|
36
32
|
links = (headers['Link'] || "").split(',').map(&:strip)
|
@@ -47,12 +43,20 @@ module Rails
|
|
47
43
|
page_header = ApiPagination.config.page_header
|
48
44
|
include_total = ApiPagination.config.include_total
|
49
45
|
|
50
|
-
headers['Link']
|
51
|
-
headers[total_header] = ApiPagination.total_from(collection) if include_total
|
46
|
+
headers['Link'] = links.join(', ') unless links.empty?
|
52
47
|
headers[per_page_header] = options[:per_page].to_s
|
53
|
-
headers[page_header]
|
48
|
+
headers[page_header] = options[:page].to_s unless page_header.nil?
|
49
|
+
headers[total_header] = total_count(collection, options) if include_total
|
54
50
|
|
55
51
|
return collection
|
56
52
|
end
|
53
|
+
|
54
|
+
def total_count(collection, options)
|
55
|
+
total_count = if ApiPagination.config.paginator == :kaminari
|
56
|
+
paginate_array_options = options[:paginate_array_options]
|
57
|
+
paginate_array_options[:total_count] if paginate_array_options
|
58
|
+
end
|
59
|
+
total_count || ApiPagination.total_from(collection)
|
60
|
+
end
|
57
61
|
end
|
58
62
|
end
|
data/spec/rails_spec.rb
CHANGED
@@ -201,6 +201,52 @@ describe NumbersController, :type => :controller do
|
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
204
|
+
context 'paginate array options' do
|
205
|
+
shared_examples 'properly set Total header' do
|
206
|
+
let(:params) do
|
207
|
+
{
|
208
|
+
paginate_array_total_count: paginate_array_total_count,
|
209
|
+
count: count,
|
210
|
+
}
|
211
|
+
end
|
212
|
+
|
213
|
+
specify do
|
214
|
+
get :index_with_paginate_array_options, params
|
215
|
+
expect(response.header['Total'].to_i).to eq total_header
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
context 'kaminari' do
|
220
|
+
around do |example|
|
221
|
+
paginator = ApiPagination.config.paginator
|
222
|
+
ApiPagination.config.paginator = :kaminari
|
223
|
+
example.run
|
224
|
+
ApiPagination.config.paginator = paginator
|
225
|
+
end
|
226
|
+
|
227
|
+
it_should_behave_like 'properly set Total header' do
|
228
|
+
let(:paginate_array_total_count) { 300 }
|
229
|
+
let(:total_header) { 300 }
|
230
|
+
let(:count) { 50 }
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context 'will_paginate' do
|
235
|
+
around do |example|
|
236
|
+
paginator = ApiPagination.config.paginator
|
237
|
+
ApiPagination.config.paginator = :will_paginate
|
238
|
+
example.run
|
239
|
+
ApiPagination.config.paginator = paginator
|
240
|
+
end
|
241
|
+
|
242
|
+
it_should_behave_like 'properly set Total header' do
|
243
|
+
let(:paginate_array_total_count) { 300 }
|
244
|
+
let(:total_header) { 50 }
|
245
|
+
let(:count) { 50 }
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
204
250
|
context 'default per page in model' do
|
205
251
|
before do
|
206
252
|
class Fixnum
|
data/spec/spec_helper.rb
CHANGED
@@ -2,15 +2,14 @@ require 'support/numbers_controller'
|
|
2
2
|
require 'support/numbers_api'
|
3
3
|
require 'api-pagination'
|
4
4
|
|
5
|
-
if ENV['PAGINATOR']
|
6
|
-
require ENV['PAGINATOR']
|
7
|
-
ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym
|
8
|
-
else
|
5
|
+
if ENV['PAGINATOR'].nil?
|
9
6
|
warn 'No PAGINATOR set. Defaulting to kaminari. To test against will_paginate, run `PAGINATOR=will_paginate bundle exec rspec`'
|
10
|
-
|
11
|
-
ApiPagination.config.paginator = :kaminari
|
7
|
+
ENV['PAGINATOR'] = 'kaminari'
|
12
8
|
end
|
13
9
|
|
10
|
+
require ENV['PAGINATOR']
|
11
|
+
ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym
|
12
|
+
|
14
13
|
require 'will_paginate/array'
|
15
14
|
|
16
15
|
RSpec.configure do |config|
|
@@ -40,8 +40,11 @@ end
|
|
40
40
|
|
41
41
|
Rails.application.routes.draw do
|
42
42
|
resources :numbers, :only => [:index] do
|
43
|
-
|
44
|
-
|
43
|
+
collection do
|
44
|
+
get :index_with_custom_render
|
45
|
+
get :index_with_no_per_page
|
46
|
+
get :index_with_paginate_array_options
|
47
|
+
end
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
@@ -85,4 +88,13 @@ class NumbersController < ActionController::Base
|
|
85
88
|
|
86
89
|
render json: NumbersSerializer.new(numbers)
|
87
90
|
end
|
91
|
+
|
92
|
+
def index_with_paginate_array_options
|
93
|
+
count = params.fetch(:count).to_i
|
94
|
+
total_count = params.fetch(:paginate_array_total_count).to_i
|
95
|
+
numbers = (1..count).to_a
|
96
|
+
numbers = paginate numbers, paginate_array_options: {total_count: total_count}
|
97
|
+
|
98
|
+
render json: NumbersSerializer.new(numbers)
|
99
|
+
end
|
88
100
|
end
|
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.4.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-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -140,4 +140,3 @@ test_files:
|
|
140
140
|
- spec/support/shared_examples/first_page.rb
|
141
141
|
- spec/support/shared_examples/last_page.rb
|
142
142
|
- spec/support/shared_examples/middle_page.rb
|
143
|
-
has_rdoc:
|