api-pagination 4.3.0 → 4.4.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: 48cb5f06eb2928aab44c8b41c50ad8ac87d8907f
4
- data.tar.gz: f4f14870f987d37dfe508217a27ee9905de4137e
3
+ metadata.gz: d8bd6ee617ad9ee01273cb7b36548cdb197110aa
4
+ data.tar.gz: b24a83cb8d231b98d7ed1670adb7f23680a80210
5
5
  SHA512:
6
- metadata.gz: d159ecc56ea17eeb912638cccb5db3d8d9e188ea7939fb6f4254aaa1f7912b6341ae3d78d9657ae985f6c4d0f56e47c3d40704c0403d47d629f424604cb48540
7
- data.tar.gz: a87c54ba5f0bc1761efc5fdf793df7b19d60a7bef57cf41d5b778aef31ace3712aef4ebed963600211c116505ec3050f91849557998c9af8d81dc0934e6b744c
6
+ metadata.gz: 729785a6c4835d934f1f6fdab6159ca54f2003aecb4368774581478bb40640b62b1790b332e9d567c2b02b55ae980bd37ae6b01b30fbf5288657f2c87e111cce
7
+ data.tar.gz: aba98e01b6082d375864506e0ba1d6d6483961e6ef6d5bc61f8c4662fa81b6a785b5ca21029d237dead8e4a8aad28309c8f4c28e9be3924a53cd2eb00362e8c1
@@ -1,7 +1,7 @@
1
1
  module ApiPagination
2
2
  class Version
3
3
  MAJOR = 4
4
- MINOR = 3
4
+ MINOR = 4
5
5
  PATCH = 0
6
6
 
7
7
  def self.to_s
@@ -24,13 +24,9 @@ module Rails
24
24
  private
25
25
 
26
26
  def _paginate_collection(collection, options={})
27
- options = {
28
- :page => ApiPagination.config.page_param(params),
29
- :per_page => (
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'] = links.join(', ') unless links.empty?
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] = options[:page].to_s unless page_header.nil?
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
@@ -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
@@ -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
- require 'kaminari'
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
- get :index_with_custom_render, on: :collection
44
- get :index_with_no_per_page, on: :collection
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.3.0
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-03-03 00:00:00.000000000 Z
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: