Skip to content

Commit 8329cf1

Browse files
committed
Only display paginator warning when relevant
If the user has configured a paginator, the warning about Kaminari and WillPaginate both appearing in the Gemfile should not be printed. Additionally, this moves loading of Kaminari and WillPaginate back into `hooks.rb` with a warning printed if neither gem is included. This warning still gets printed when the user's application starts. Finally, add a note to the README saying that we basically aren't going to try to deal with people's Kaminari/WillPaginate conflicts anymore. Let them sort that out themselves. Sorry, folks! Related: #18, #36 Signed-off-by: David Celis <[email protected]>
1 parent 3c6a7cb commit 8329cf1

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
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: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def paginator
1818
end
1919

2020
def paginator=(paginator)
21-
case paginator
21+
case paginator.to_sym
2222
when :kaminari
2323
use_kaminari
2424
when :will_paginate
@@ -35,41 +35,26 @@ def set_paginator
3535
Kernel.warn <<-WARNING
3636
Warning: api-pagination relies on either Kaminari or WillPaginate, but both are
3737
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:
38+
you _must_ configure api-pagination on your own. For example:
3939
4040
ApiPagination.configure do |config|
41-
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
4250
end
4351
4452
WARNING
4553
elsif defined?(Kaminari)
46-
use_kaminari and return
54+
return use_kaminari
4755
elsif defined?(WillPaginate::CollectionMethods)
48-
use_will_paginate and return
49-
end
50-
51-
begin
52-
require 'kaminari'
53-
use_kaminari and return
54-
rescue LoadError
55-
end
56-
57-
begin
58-
require 'will_paginate'
59-
use_will_paginate and return
60-
rescue LoadError
56+
return use_will_paginate
6157
end
62-
63-
Kernel.warn <<-WARNING
64-
Warning: api-pagination relies on either Kaminari or WillPaginate. Please
65-
install either dependency by adding one of the following to your Gemfile:
66-
67-
gem 'kaminari'
68-
gem 'will_paginate'
69-
70-
WARNING
71-
72-
@paginator
7358
end
7459

7560
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)