api-pagination 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8bc41a88d9698b56c9329d03b153e4a26359a89
4
- data.tar.gz: 5b12ab2abac9d7fa0ad8569b7c80811c35cc3d7c
3
+ metadata.gz: 48cb5f06eb2928aab44c8b41c50ad8ac87d8907f
4
+ data.tar.gz: f4f14870f987d37dfe508217a27ee9905de4137e
5
5
  SHA512:
6
- metadata.gz: 04d2fe7e9d7df6f1b5bf3ca3e623594d9f01934ec8aaf98147fd759a196a5c778d4fda5946e38ab5b1164d68db0b9d641b6473b23bb90b1ed54abc068f44687a
7
- data.tar.gz: 02c3c45d64667438376d0243ab3671d36f1e0f6360881c1a37398e35a25a4252a7653b0bce98cce739aefc7e228c7eb430895ba4f60be7fb1591ba4bb88b8d28
6
+ metadata.gz: d159ecc56ea17eeb912638cccb5db3d8d9e188ea7939fb6f4254aaa1f7912b6341ae3d78d9657ae985f6c4d0f56e47c3d40704c0403d47d629f424604cb48540
7
+ data.tar.gz: a87c54ba5f0bc1761efc5fdf793df7b19d60a7bef57cf41d5b778aef31ace3712aef4ebed963600211c116505ec3050f91849557998c9af8d81dc0934e6b744c
@@ -45,7 +45,7 @@ module ApiPagination
45
45
  if Kaminari.config.max_per_page && options[:per_page] > Kaminari.config.max_per_page
46
46
  options[:per_page] = Kaminari.config.max_per_page
47
47
  elsif options[:per_page] <= 0
48
- options[:per_page] = Kaminari.config.default_per_page
48
+ options[:per_page] = get_default_per_page_for_kaminari(collection)
49
49
  end
50
50
 
51
51
  collection = Kaminari.paginate_array(collection, paginate_array_options) if collection.is_a?(Array)
@@ -53,7 +53,9 @@ module ApiPagination
53
53
  end
54
54
 
55
55
  def paginate_with_will_paginate(collection, options)
56
- options[:per_page] = WillPaginate.per_page if options[:per_page] <= 0
56
+ if options[:per_page] <= 0
57
+ options[:per_page] = default_per_page_for_will_paginate(collection)
58
+ end
57
59
 
58
60
  if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset)
59
61
  collection.paginate(options[:page], options[:per_page])
@@ -61,6 +63,24 @@ module ApiPagination
61
63
  collection.paginate(:page => options[:page], :per_page => options[:per_page])
62
64
  end
63
65
  end
66
+
67
+ def get_default_per_page_for_kaminari(collection)
68
+ default = Kaminari.config.default_per_page
69
+ detect_model(collection).default_per_page || default
70
+ rescue
71
+ default
72
+ end
73
+
74
+ def default_per_page_for_will_paginate(collection)
75
+ default = WillPaginate.per_page
76
+ detect_model(collection).per_page || default
77
+ rescue
78
+ default
79
+ end
80
+
81
+ def detect_model(collection)
82
+ collection.first.class
83
+ end
64
84
  end
65
85
  end
66
86
 
@@ -19,6 +19,33 @@ module ApiPagination
19
19
  @include_total = true
20
20
  end
21
21
 
22
+ ['page', 'per_page'].each do |param_name|
23
+ method_name = "#{param_name}_param"
24
+ instance_variable_name = "@#{method_name}"
25
+
26
+ define_method method_name do |params = nil, &block|
27
+ if block.is_a?(Proc)
28
+ instance_variable_set(instance_variable_name, block)
29
+ return
30
+ end
31
+
32
+ if instance_variable_get(instance_variable_name).nil?
33
+ # use :page & :per_page by default
34
+ instance_variable_set(instance_variable_name, (lambda { |p| p[param_name.to_sym] }))
35
+ end
36
+
37
+ instance_variable_get(instance_variable_name).call(params)
38
+ end
39
+
40
+ define_method "#{method_name}=" do |param|
41
+ if param.is_a?(Symbol) || param.is_a?(String)
42
+ instance_variable_set(instance_variable_name, (lambda { |params| params[param] }))
43
+ else
44
+ raise ArgumentError, "Cannot set page_param option"
45
+ end
46
+ end
47
+ end
48
+
22
49
  def paginator
23
50
  @paginator || set_paginator
24
51
  end
@@ -1,7 +1,7 @@
1
1
  module ApiPagination
2
2
  class Version
3
3
  MAJOR = 4
4
- MINOR = 2
4
+ MINOR = 3
5
5
  PATCH = 0
6
6
 
7
7
  def self.to_s
@@ -3,9 +3,10 @@ module Grape
3
3
  def self.included(base)
4
4
  Grape::Endpoint.class_eval do
5
5
  def paginate(collection)
6
- per_page = params[:per_page] || route_setting(:per_page)
6
+ per_page = ApiPagination.config.per_page_param(params) || route_setting(:per_page)
7
+
7
8
  options = {
8
- :page => params[:page],
9
+ :page => ApiPagination.config.page_param(params),
9
10
  :per_page => [per_page, route_setting(:max_per_page)].compact.min
10
11
  }
11
12
  collection = ApiPagination.paginate(collection, options)
@@ -25,8 +25,11 @@ module Rails
25
25
 
26
26
  def _paginate_collection(collection, options={})
27
27
  options = {
28
- :page => params[:page],
29
- :per_page => (options.delete(:per_page) || params[:per_page])
28
+ :page => ApiPagination.config.page_param(params),
29
+ :per_page => (
30
+ options.delete(:per_page) ||
31
+ ApiPagination.config.per_page_param(params)
32
+ )
30
33
  }
31
34
  collection = ApiPagination.paginate(collection, options)
32
35
 
@@ -123,5 +123,124 @@ describe NumbersController, :type => :controller do
123
123
 
124
124
  after { ApiPagination.config.include_total = true }
125
125
  end
126
+
127
+ context 'custom page param' do
128
+ context 'page_param as a symbol' do
129
+ before do
130
+ ApiPagination.config.page_param = :foo
131
+ ApiPagination.config.page_header = 'Page'
132
+ end
133
+
134
+ after do
135
+ ApiPagination.config.page_param = :page
136
+ ApiPagination.config.page_header = nil
137
+ end
138
+
139
+ it 'should work' do
140
+ get :index, :foo => 2, :count => 100
141
+
142
+ expect(response.header['Page']).to eq('2')
143
+ end
144
+ end
145
+
146
+ context 'page_param as a block' do
147
+ before do
148
+ ApiPagination.config.page_param do |params|
149
+ params[:foo][:bar]
150
+ end
151
+
152
+ ApiPagination.config.page_header = 'Page'
153
+ end
154
+
155
+ after do
156
+ ApiPagination.config.page_param = :page
157
+ ApiPagination.config.page_header = nil
158
+ end
159
+
160
+ it 'should work' do
161
+ get :index, :foo => { :bar => 2 }, :count => 100
162
+
163
+ expect(response.header['Page']).to eq('2')
164
+ end
165
+ end
166
+ end
167
+
168
+ context 'custom per_page param' do
169
+ context 'per_page_param as a symbol' do
170
+ before do
171
+ ApiPagination.config.per_page_param = :foo
172
+ end
173
+
174
+ after do
175
+ ApiPagination.config.per_page_param = :per_page
176
+ end
177
+
178
+ it 'should work' do
179
+ get :index_with_no_per_page, :foo => 2, :count => 100
180
+
181
+ expect(response.header['Per-Page']).to eq('2')
182
+ end
183
+ end
184
+
185
+ context 'page_param as a block' do
186
+ before do
187
+ ApiPagination.config.per_page_param do |params|
188
+ params[:foo][:bar]
189
+ end
190
+ end
191
+
192
+ after do
193
+ ApiPagination.config.per_page_param = :per_page
194
+ end
195
+
196
+ it 'should work' do
197
+ get :index_with_no_per_page, :foo => { :bar => 2 }, :count => 100
198
+
199
+ expect(response.header['Per-Page']).to eq('2')
200
+ end
201
+ end
202
+ end
203
+
204
+ context 'default per page in model' do
205
+ before do
206
+ class Fixnum
207
+ @default_per_page = 6
208
+ @per_page = 6
209
+
210
+ class << self
211
+ attr_accessor :default_per_page, :per_page
212
+ end
213
+ end
214
+ end
215
+
216
+ after do
217
+ class Fixnum
218
+ @default_per_page = 25
219
+ @per_page = 25
220
+ end
221
+ end
222
+
223
+ it 'should use default per page from model' do
224
+ get :index_with_no_per_page, count: 100
225
+
226
+ expect(response.header['Per-Page']).to eq('6')
227
+ end
228
+
229
+ it 'should not fail if model does not respond to per page' do
230
+ class Fixnum
231
+ @default_per_page = nil
232
+ @per_page = nil
233
+ end
234
+
235
+ get :index_with_no_per_page, count: 100
236
+
237
+ expect(response.header['Per-Page']).to eq(
238
+ case ApiPagination.config.paginator
239
+ when :kaminari then Kaminari.config.default_per_page.to_s
240
+ when :will_paginate then WillPaginate.per_page.to_s
241
+ end
242
+ )
243
+ end
244
+ end
126
245
  end
127
246
  end
@@ -41,6 +41,7 @@ end
41
41
  Rails.application.routes.draw do
42
42
  resources :numbers, :only => [:index] do
43
43
  get :index_with_custom_render, on: :collection
44
+ get :index_with_no_per_page, on: :collection
44
45
  end
45
46
  end
46
47
 
@@ -76,4 +77,12 @@ class NumbersController < ActionController::Base
76
77
 
77
78
  render json: NumbersSerializer.new(numbers)
78
79
  end
80
+
81
+ def index_with_no_per_page
82
+ total = params.fetch(:count).to_i
83
+ numbers = (1..total).to_a
84
+ numbers = paginate numbers
85
+
86
+ render json: NumbersSerializer.new(numbers)
87
+ end
79
88
  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.2.0
4
+ version: 4.3.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-01-01 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec