Skip to content

Commit a17c489

Browse files
committed
Merge branch 'master' into 3-x-stable
2 parents 621dffb + 8329cf1 commit a17c489

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ Per-Page: 10
133133
# ...
134134
```
135135

136+
## A Note on Kaminari and WillPaginate
137+
138+
api-pagination requires either Kaminari or WillPaginate in order to function, but some users may find themselves in situations where their application includes both. For example, you may have included [ActiveAdmin][activeadmin] (which uses Kaminari for pagination) and WillPaginate to do your own pagination. While it's suggested that you remove one paginator gem or the other, if you're unable to do so, you _must_ configure api-pagination explicitly:
139+
140+
```ruby
141+
ApiPagination.configure do |config|
142+
config.paginator = :will_paginate
143+
end
144+
```
145+
146+
If you don't do this, an annoying warning will print once your app starts seeing traffic. You should also configure Kaminari to use a different name for its `per_page` method (see https://github.com/activeadmin/activeadmin/wiki/How-to-work-with-will_paginate):
147+
148+
```ruby
149+
Kaminari.configure do |config|
150+
config.page_method_name = :per_page_kaminari
151+
end
152+
```
153+
154+
[activeadmin]: https://github.com/activeadmin/activeadmin
136155
[kaminari]: https://github.com/amatsuda/kaminari
137156
[will_paginate]: https://github.com/mislav/will_paginate
138157

lib/api-pagination/configuration.rb

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module ApiPagination
22
class Configuration
3-
attr_reader :paginator
4-
53
attr_accessor :total_header
64

75
attr_accessor :per_page_header
@@ -13,11 +11,14 @@ def configure(&block)
1311
def initialize
1412
@total_header = 'Total'
1513
@per_page_header = 'Per-Page'
16-
set_paginator
14+
end
15+
16+
def paginator
17+
@paginator || set_paginator
1718
end
1819

1920
def paginator=(paginator)
20-
case paginator
21+
case paginator.to_sym
2122
when :kaminari
2223
use_kaminari
2324
when :will_paginate
@@ -34,39 +35,26 @@ def set_paginator
3435
Kernel.warn <<-WARNING
3536
Warning: api-pagination relies on either Kaminari or WillPaginate, but both are
3637
currently active. If possible, you should remove one or the other. If you can't,
37-
you must configure api-pagination on your own. For example:
38+
you _must_ configure api-pagination on your own. For example:
3839
3940
ApiPagination.configure do |config|
40-
config.paginator = Kaminari
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
4150
end
4251
4352
WARNING
4453
elsif defined?(Kaminari)
45-
use_kaminari and return
54+
return use_kaminari
4655
elsif defined?(WillPaginate::CollectionMethods)
47-
use_will_paginate and return
48-
end
49-
50-
begin
51-
require 'kaminari'
52-
use_kaminari and return
53-
rescue LoadError
54-
end
55-
56-
begin
57-
require 'will_paginate'
58-
use_will_paginate and return
59-
rescue LoadError
56+
return use_will_paginate
6057
end
61-
62-
Kernel.warn <<-WARNING
63-
Warning: api-pagination relies on either Kaminari or WillPaginate. Please
64-
install either dependency by adding one of the following to your Gemfile:
65-
66-
gem 'kaminari'
67-
gem 'will_paginate'
68-
69-
WARNING
7058
end
7159

7260
def use_kaminari

lib/api-pagination/hooks.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@
1515
require 'grape/pagination'
1616
Grape::API.send(:include, Grape::Pagination)
1717
end
18+
19+
begin; require 'kaminari'; rescue LoadError; end
20+
begin; require 'will_paginate'; rescue LoadError; end
21+
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:
26+
27+
gem 'kaminari'
28+
gem 'will_paginate'
29+
WARNING
30+
end

0 commit comments

Comments
 (0)