api-pagination 3.2.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d731640c745b641b398f674974fad336835f430
4
- data.tar.gz: 87f4e54a8e7f52bfc64110a9631435ca720381ed
3
+ metadata.gz: 44223a7f3dfd7705282d1c93abc09c04e55b5cd0
4
+ data.tar.gz: dbcb589c144ae07bc298b90da5de60eb627b021c
5
5
  SHA512:
6
- metadata.gz: 3270038dc07c4e4fda06bd333d272c0c1825c27f0533e2d0881673eb0cacf166bc4a88fe98cbf20d7a0106f16abafd09f79c0b4a5bac51d1a0f02e40cbd1ae66
7
- data.tar.gz: d4b48b49eadbd2a313018110a643d659df61c3d042804c6dbcfd64b1568f44f11eb18889fea6a6211d27190364f177916c07d48df68a263aa1cd7af27b2c8558
6
+ metadata.gz: fe8b5fb947b1bf135a49283f4ca86f3b3c5131b564c2492105f358f5864eabf6f0509f5d54594c94e8dfff9a1f2a02fe4573c23dcd32625d08a2978b7be41bc8
7
+ data.tar.gz: df50ee727c766cb82b2f23efeae78f1c91ea63aa790e0459f5645ccaf48c52145904896592ec596990b8c40a4b542530613f4cc0e151b0dbee500254d06a4b72
@@ -1,20 +1,33 @@
1
- require 'api-pagination/configuration'
2
1
  require 'api-pagination/version'
3
2
 
4
3
  module ApiPagination
5
4
  class << self
5
+ attr_reader :paginator
6
+
6
7
  def paginate(collection, options = {})
7
8
  options[:page] = options[:page].to_i
8
9
  options[:page] = 1 if options[:page] <= 0
9
10
  options[:per_page] = options[:per_page].to_i
10
11
 
11
- case ApiPagination.config.paginator
12
+ case ApiPagination.paginator
12
13
  when :kaminari
13
- paginate_with_kaminari(collection, options)
14
+ if Kaminari.config.max_per_page && options[:per_page] > Kaminari.config.max_per_page
15
+ options[:per_page] = Kaminari.config.max_per_page
16
+ elsif options[:per_page] <= 0
17
+ options[:per_page] = Kaminari.config.default_per_page
18
+ end
19
+ collection = Kaminari.paginate_array(collection) if collection.is_a?(Array)
20
+ collection.page(options[:page]).per(options[:per_page])
14
21
  when :will_paginate
15
- paginate_with_will_paginate(collection, options)
22
+ options[:per_page] = WillPaginate.per_page if options[:per_page] <= 0
23
+
24
+ if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset)
25
+ collection.paginate(options[:page], options[:per_page])
26
+ else
27
+ collection.paginate(:page => options[:page], :per_page => options[:per_page])
28
+ end
16
29
  else
17
- raise StandardError, "Unknown paginator: #{ApiPagination.config.paginator}"
30
+ raise StandardError, "Unknown paginator: #{ApiPagination.paginator}"
18
31
  end
19
32
  end
20
33
 
@@ -33,49 +46,11 @@ module ApiPagination
33
46
  end
34
47
 
35
48
  def total_from(collection)
36
- case ApiPagination.config.paginator
49
+ case ApiPagination.paginator
37
50
  when :kaminari then collection.total_count.to_s
38
51
  when :will_paginate then collection.total_entries.to_s
39
52
  end
40
53
  end
41
-
42
- def paginator
43
- warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.paginator"
44
- config.paginator
45
- end
46
-
47
- def per_page_header
48
- warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.per_page_header"
49
- config.per_page_header
50
- end
51
-
52
- def total_header
53
- warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.total_header"
54
- config.total_header
55
- end
56
-
57
- private
58
-
59
- def paginate_with_kaminari(collection, options)
60
- if Kaminari.config.max_per_page && options[:per_page] > Kaminari.config.max_per_page
61
- options[:per_page] = Kaminari.config.max_per_page
62
- elsif options[:per_page] <= 0
63
- options[:per_page] = Kaminari.config.default_per_page
64
- end
65
-
66
- collection = Kaminari.paginate_array(collection) if collection.is_a?(Array)
67
- collection.page(options[:page]).per(options[:per_page])
68
- end
69
-
70
- def paginate_with_will_paginate(collection, options)
71
- options[:per_page] = WillPaginate.per_page if options[:per_page] <= 0
72
-
73
- if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset)
74
- collection.paginate(options[:page], options[:per_page])
75
- else
76
- collection.paginate(:page => options[:page], :per_page => options[:per_page])
77
- end
78
- end
79
54
  end
80
55
  end
81
56
 
@@ -1,30 +1,77 @@
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
1
+ module ApiPagination
2
+ class Hooks
3
+ def self.init!
4
+ begin; require 'rails'; rescue LoadError; end
5
+ if defined?(ActionController::Base)
6
+ require 'rails/pagination'
7
+ ActionController::Base.send(:include, Rails::Pagination)
8
+ end
6
9
 
7
- begin; require 'rails-api'; rescue LoadError; end
8
- if defined?(ActionController::API)
9
- require 'rails/pagination'
10
- ActionController::API.send(:include, Rails::Pagination)
11
- end
10
+ begin; require 'rails-api'; rescue LoadError; end
11
+ if defined?(ActionController::API)
12
+ require 'rails/pagination'
13
+ ActionController::API.send(:include, Rails::Pagination)
14
+ end
12
15
 
13
- begin; require 'grape'; rescue LoadError; end
14
- if defined?(Grape::API)
15
- require 'grape/pagination'
16
- Grape::API.send(:include, Grape::Pagination)
17
- end
16
+ begin; require 'grape'; rescue LoadError; end
17
+ if defined?(Grape::API)
18
+ require 'grape/pagination'
19
+ Grape::API.send(:include, Grape::Pagination)
20
+ end
21
+
22
+ # Kaminari and will_paginate conflict with each other, so we should check
23
+ # to see if either is already active before attempting to load them.
24
+ if defined?(Kaminari) && defined?(WillPaginate::CollectionMethods)
25
+ STDERR.puts <<-EOC
26
+ Warning: api-pagination relies on either Kaminari or WillPaginate, but these
27
+ gems conflict. Please ensure only one of them is active at any given time.
28
+
29
+ EOC
30
+ return
31
+ elsif defined?(Kaminari)
32
+ initialize_kaminari! and return
33
+ elsif defined?(WillPaginate::CollectionMethods)
34
+ initialize_will_paginate! and return
35
+ end
36
+ # If neither is loaded, we can safely attempt these requires.
37
+ unless ApiPagination.paginator == :will_paginate
38
+ begin
39
+ require 'kaminari'
40
+ initialize_kaminari! and return
41
+ rescue LoadError
42
+ end
43
+ end
18
44
 
19
- begin; require 'kaminari'; rescue LoadError; end
20
- begin; require 'will_paginate'; rescue LoadError; end
45
+ begin
46
+ require 'will_paginate'
47
+ initialize_will_paginate! and return
48
+ rescue LoadError
49
+ end
21
50
 
22
- unless defined?(Kaminari) || defined?(WillPaginate::CollectionMethods)
23
- Kernel.warn <<-WARNING.gsub(/^\s{4}/, '')
24
- Warning: api-pagination relies on either Kaminari or WillPaginate. Please
25
- install either dependency by adding one of the following to your Gemfile:
51
+ STDERR.puts <<-EOC
52
+ Warning: api-pagination relies on either Kaminari or WillPaginate. Please
53
+ install either dependency by adding one of the following to your Gemfile:
26
54
 
27
- gem 'kaminari'
28
- gem 'will_paginate'
29
- WARNING
55
+ gem 'kaminari'
56
+ gem 'will_paginate'
57
+
58
+ EOC
59
+ end
60
+
61
+ def self.initialize_kaminari!
62
+ require 'kaminari/models/array_extension'
63
+ ApiPagination.instance_variable_set(:@paginator, :kaminari)
64
+ end
65
+
66
+ def self.initialize_will_paginate!
67
+ WillPaginate::CollectionMethods.module_eval do
68
+ def first_page?() !previous_page end
69
+ def last_page?() !next_page end
70
+ end
71
+
72
+ ApiPagination.instance_variable_set(:@paginator, :will_paginate)
73
+ end
74
+ end
30
75
  end
76
+
77
+ ApiPagination::Hooks.init!
@@ -1,8 +1,8 @@
1
1
  module ApiPagination
2
2
  class Version
3
- MAJOR = 3
4
- MINOR = 2
5
- PATCH = 1
3
+ MAJOR = 4
4
+ MINOR = 0
5
+ PATCH = 0
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, PATCH].join('.')
@@ -3,10 +3,9 @@ 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)
7
6
  options = {
8
7
  :page => params[:page],
9
- :per_page => [per_page, route_setting(:max_per_page)].compact.min
8
+ :per_page => (params[:per_page] || route_setting(:per_page))
10
9
  }
11
10
  collection = ApiPagination.paginate(collection, options)
12
11
 
@@ -20,12 +19,8 @@ module Grape
20
19
  links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
21
20
  end
22
21
 
23
- total_header = ApiPagination.config.total_header
24
- per_page_header = ApiPagination.config.per_page_header
25
-
26
- header 'Link', links.join(', ') unless links.empty?
27
- header total_header, ApiPagination.total_from(collection)
28
- header per_page_header, options[:per_page].to_s
22
+ header 'Link', links.join(', ') unless links.empty?
23
+ header 'Total', ApiPagination.total_from(collection)
29
24
 
30
25
  return collection
31
26
  end
@@ -34,7 +29,6 @@ module Grape
34
29
  base.class_eval do
35
30
  def self.paginate(options = {})
36
31
  route_setting :per_page, (options[:per_page] || 25)
37
- route_setting :max_per_page, options[:max_per_page]
38
32
  params do
39
33
  optional :page, :type => Integer, :default => 1,
40
34
  :desc => 'Page of results to fetch.'
@@ -39,14 +39,11 @@ module Rails
39
39
  links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
40
40
  end
41
41
 
42
- total_header = ApiPagination.config.total_header
43
- per_page_header = ApiPagination.config.per_page_header
44
-
45
- headers['Link'] = links.join(', ') unless links.empty?
46
- headers[total_header] = ApiPagination.total_from(collection)
47
- headers[per_page_header] = options[:per_page].to_s
42
+ headers['Link'] = links.join(', ') unless links.empty?
43
+ headers['Total'] = ApiPagination.total_from(collection)
48
44
 
49
45
  return collection
50
46
  end
51
47
  end
52
48
  end
49
+
@@ -8,10 +8,9 @@ describe NumbersAPI do
8
8
  describe 'GET #index' do
9
9
  let(:links) { last_response.headers['Link'].split(', ') }
10
10
  let(:total) { last_response.headers['Total'].to_i }
11
- let(:per_page) { last_response.headers['Per-Page'].to_i }
12
11
 
13
12
  context 'without enough items to give more than one page' do
14
- before { get '/numbers', :count => 10 }
13
+ before { get :numbers, :count => 10 }
15
14
 
16
15
  it 'should not paginate' do
17
16
  expect(last_response.headers.keys).not_to include('Link')
@@ -21,10 +20,6 @@ describe NumbersAPI do
21
20
  expect(total).to eq(10)
22
21
  end
23
22
 
24
- it 'should give a Per-Page header' do
25
- expect(per_page).to eq(10)
26
- end
27
-
28
23
  it 'should list all numbers in the response body' do
29
24
  body = '[1,2,3,4,5,6,7,8,9,10]'
30
25
  expect(last_response.body).to eq(body)
@@ -32,72 +27,29 @@ describe NumbersAPI do
32
27
  end
33
28
 
34
29
  context 'with existing Link headers' do
35
- before { get '/numbers', :count => 30, :with_headers => true }
30
+ before { get :numbers, :count => 30, :with_headers => true }
36
31
 
37
32
  it_behaves_like 'an endpoint with existing Link headers'
38
33
  end
39
34
 
40
35
  context 'with enough items to paginate' do
41
36
  context 'when on the first page' do
42
- before { get '/numbers', :count => 100 }
37
+ before { get :numbers, :count => 100 }
43
38
 
44
39
  it_behaves_like 'an endpoint with a first page'
45
40
  end
46
41
 
47
42
  context 'when on the last page' do
48
- before { get '/numbers', :count => 100, :page => 10 }
43
+ before { get :numbers, :count => 100, :page => 10 }
49
44
 
50
45
  it_behaves_like 'an endpoint with a last page'
51
46
  end
52
47
 
53
48
  context 'when somewhere comfortably in the middle' do
54
- before { get '/numbers', :count => 100, :page => 2 }
49
+ before { get :numbers, :count => 100, :page => 2 }
55
50
 
56
51
  it_behaves_like 'an endpoint with a middle page'
57
52
  end
58
-
59
- context 'with a max_per_page setting' do
60
- before { get '/numbers', :count => 100, :per_page => 30 }
61
-
62
- it 'should not go above the max_per_page_limit' do
63
- body = '[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]'
64
-
65
- expect(last_response.body).to eq(body)
66
- end
67
- end
68
- end
69
-
70
- context 'with custom response headers' do
71
- before do
72
- ApiPagination.config.total_header = 'X-Total-Count'
73
- ApiPagination.config.per_page_header = 'X-Per-Page'
74
-
75
- get '/numbers', count: 10
76
- end
77
-
78
- after do
79
- ApiPagination.config.total_header = 'Total'
80
- ApiPagination.config.per_page_header = 'Per-Page'
81
- end
82
-
83
- let(:total) { last_response.header['X-Total-Count'].to_i }
84
- let(:per_page) { last_response.header['X-Per-Page'].to_i }
85
-
86
- it 'should give a X-Total-Count header' do
87
- headers_keys = last_response.headers.keys
88
-
89
- expect(headers_keys).not_to include('Total')
90
- expect(headers_keys).to include('X-Total-Count')
91
- expect(total).to eq(10)
92
- end
93
-
94
- it 'should give a X-Per-Page header' do
95
- headers_keys = last_response.headers.keys
96
-
97
- expect(headers_keys).not_to include('Per-Page')
98
- expect(headers_keys).to include('X-Per-Page')
99
- expect(per_page).to eq(10)
100
- end
101
53
  end
102
54
  end
103
55
  end
@@ -10,7 +10,6 @@ describe NumbersController, :type => :controller do
10
10
  describe 'GET #index' do
11
11
  let(:links) { response.headers['Link'].split(', ') }
12
12
  let(:total) { response.headers['Total'].to_i }
13
- let(:per_page) { response.headers['Per-Page'].to_i }
14
13
 
15
14
  context 'without enough items to give more than one page' do
16
15
  before { get :index, :count => 10 }
@@ -23,10 +22,6 @@ describe NumbersController, :type => :controller do
23
22
  expect(total).to eq(10)
24
23
  end
25
24
 
26
- it 'should give a Per-Page header' do
27
- expect(per_page).to eq(10)
28
- end
29
-
30
25
  it 'should list all numbers in the response body' do
31
26
  body = '[1,2,3,4,5,6,7,8,9,10]'
32
27
  expect(response.body).to eq(body)
@@ -68,38 +63,10 @@ describe NumbersController, :type => :controller do
68
63
  expect(response.body).to eq(json)
69
64
  end
70
65
  end
66
+ end
67
+ end
71
68
 
72
- context 'with custom response headers' do
73
- before do
74
- ApiPagination.config.total_header = 'X-Total-Count'
75
- ApiPagination.config.per_page_header = 'X-Per-Page'
76
-
77
- get :index, count: 10
78
- end
79
-
80
- after do
81
- ApiPagination.config.total_header = 'Total'
82
- ApiPagination.config.per_page_header = 'Per-Page'
83
- end
84
-
85
- let(:total) { response.header['X-Total-Count'].to_i }
86
- let(:per_page) { response.header['X-Per-Page'].to_i }
87
69
 
88
- it 'should give a X-Total-Count header' do
89
- headers_keys = response.headers.keys
90
70
 
91
- expect(headers_keys).not_to include('Total')
92
- expect(headers_keys).to include('X-Total-Count')
93
- expect(total).to eq(10)
94
- end
95
71
 
96
- it 'should give a X-Per-Page header' do
97
- headers_keys = response.headers.keys
98
72
 
99
- expect(headers_keys).not_to include('Per-Page')
100
- expect(headers_keys).to include('X-Per-Page')
101
- expect(per_page).to eq(10)
102
- end
103
- end
104
- end
105
- end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if ApiPagination.config.paginator == :will_paginate
3
+ if ApiPagination.paginator == :will_paginate
4
4
  require 'sqlite3'
5
5
  require 'sequel'
6
6
  require 'will_paginate/sequel'
@@ -1,17 +1,15 @@
1
- require 'support/numbers_controller'
2
- require 'support/numbers_api'
3
- require 'api-pagination'
4
-
5
1
  if ENV['PAGINATOR']
6
- require ENV['PAGINATOR']
7
- ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym
2
+ ApiPagination.instance_variable_set(:@paginator, ENV['PAGINATOR'].to_sym)
8
3
  else
9
4
  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
5
+ ApiPagination.instance_variable_set(:@paginator, :kaminari)
12
6
  end
13
7
 
14
- require 'will_paginate/array'
8
+ require 'support/numbers_controller'
9
+ require 'support/numbers_api'
10
+ require 'api-pagination'
11
+
12
+ require 'will_paginate/array' if ApiPagination.paginator == :will_paginate
15
13
 
16
14
  RSpec.configure do |config|
17
15
  config.include Rack::Test::Methods
@@ -5,7 +5,7 @@ class NumbersAPI < Grape::API
5
5
  format :json
6
6
 
7
7
  desc 'Return some paginated set of numbers'
8
- paginate :per_page => 10, :max_per_page => 25
8
+ paginate :per_page => 10
9
9
  params do
10
10
  requires :count, :type => Integer
11
11
  optional :with_headers, :default => false, :type => Boolean
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 4.0.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: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: grape
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.10.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.10.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: railties
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 3.0.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: actionpack
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.0.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.0.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sequel
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 4.9.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 4.9.0
83
83
  description: Link header pagination for Rails and Grape APIs
@@ -87,10 +87,9 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - lib/api-pagination/configuration.rb
90
+ - lib/api-pagination.rb
91
91
  - lib/api-pagination/hooks.rb
92
92
  - lib/api-pagination/version.rb
93
- - lib/api-pagination.rb
94
93
  - lib/grape/pagination.rb
95
94
  - lib/rails/pagination.rb
96
95
  - spec/grape_spec.rb
@@ -113,17 +112,17 @@ require_paths:
113
112
  - lib
114
113
  required_ruby_version: !ruby/object:Gem::Requirement
115
114
  requirements:
116
- - - '>='
115
+ - - ">="
117
116
  - !ruby/object:Gem::Version
118
117
  version: '0'
119
118
  required_rubygems_version: !ruby/object:Gem::Requirement
120
119
  requirements:
121
- - - '>='
120
+ - - ">="
122
121
  - !ruby/object:Gem::Version
123
122
  version: '0'
124
123
  requirements: []
125
124
  rubyforge_project:
126
- rubygems_version: 2.0.14
125
+ rubygems_version: 2.4.5
127
126
  signing_key:
128
127
  specification_version: 4
129
128
  summary: Link header pagination for Rails and Grape APIs. Don't use the request body.
@@ -138,3 +137,4 @@ test_files:
138
137
  - spec/support/shared_examples/first_page.rb
139
138
  - spec/support/shared_examples/last_page.rb
140
139
  - spec/support/shared_examples/middle_page.rb
140
+ has_rdoc:
@@ -1,85 +0,0 @@
1
- module ApiPagination
2
- class Configuration
3
- attr_accessor :total_header
4
-
5
- attr_accessor :per_page_header
6
-
7
- def configure(&block)
8
- yield self
9
- end
10
-
11
- def initialize
12
- @total_header = 'Total'
13
- @per_page_header = 'Per-Page'
14
- end
15
-
16
- def paginator
17
- @paginator || set_paginator
18
- end
19
-
20
- def paginator=(paginator)
21
- case paginator.to_sym
22
- when :kaminari
23
- use_kaminari
24
- when :will_paginate
25
- use_will_paginate
26
- else
27
- raise StandardError, "Unknown paginator: #{paginator}"
28
- end
29
- end
30
-
31
- private
32
-
33
- def set_paginator
34
- if defined?(Kaminari) && defined?(WillPaginate::CollectionMethods)
35
- Kernel.warn <<-WARNING
36
- Warning: api-pagination relies on either Kaminari or WillPaginate, but both are
37
- currently active. If possible, you should remove one or the other. If you can't,
38
- you _must_ configure api-pagination on your own. For example:
39
-
40
- ApiPagination.configure do |config|
41
- config.paginator = :kaminari
42
- end
43
-
44
- You should also configure Kaminari to use a different `per_page` method name as
45
- using these gems together causes a conflict; some information can be found at
46
- https://github.com/activeadmin/activeadmin/wiki/How-to-work-with-will_paginate
47
-
48
- Kaminari.configure do |config|
49
- config.page_method_name = :per_page_kaminari
50
- end
51
-
52
- WARNING
53
- elsif defined?(Kaminari)
54
- return use_kaminari
55
- elsif defined?(WillPaginate::CollectionMethods)
56
- return use_will_paginate
57
- end
58
- end
59
-
60
- def use_kaminari
61
- require 'kaminari/models/array_extension'
62
- @paginator = :kaminari
63
- end
64
-
65
- def use_will_paginate
66
- WillPaginate::CollectionMethods.module_eval do
67
- def first_page?() !previous_page end
68
- def last_page?() !next_page end
69
- end
70
-
71
- @paginator = :will_paginate
72
- end
73
- end
74
-
75
- class << self
76
- def configure
77
- yield config
78
- end
79
-
80
- def config
81
- @config ||= Configuration.new
82
- end
83
- alias :configuration :config
84
- end
85
- end