Skip to content

Commit 2267ef2

Browse files
committed
Merge pull request #19 from Swapt/feature/fix_will_paginate_specs
Fix will_paginate specs
2 parents 654e297 + ca06ea1 commit 2267ef2

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/api-pagination.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'api-pagination/hooks'
21
require 'api-pagination/version'
32

43
module ApiPagination
@@ -19,6 +18,8 @@ def paginate(collection, options = {})
1918
else
2019
collection.paginate(:page => options[:page], :per_page => options[:per_page])
2120
end
21+
else
22+
fail StandardError, "Unknown paginator: #{ApiPagination.paginator}"
2223
end
2324
end
2425

@@ -44,3 +45,5 @@ def total_from(collection)
4445
end
4546
end
4647
end
48+
49+
require 'api-pagination/hooks'

lib/api-pagination/hooks.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ def self.init!
1818
require 'grape/pagination'
1919
Grape::API.send(:include, Grape::Pagination)
2020
end
21-
2221
# Kaminari and will_paginate conflict with each other, so we should check
2322
# to see if either is already active before attempting to load them.
24-
if defined?(Kaminari)
23+
if defined?(Kaminari) && defined?(WillPaginate::CollectionMethods)
24+
STDERR.puts <<-EOC
25+
Warning: api-pagination relies on either Kaminari or WillPaginate, but these
26+
gems conflict. Please ensure only one of them is active at any given time.
27+
EOC
28+
return
29+
elsif defined?(Kaminari)
2530
initialize_kaminari! and return
2631
elsif defined?(WillPaginate::CollectionMethods)
2732
initialize_will_paginate! and return
2833
end
29-
3034
# If neither is loaded, we can safely attempt these requires.
31-
begin
32-
require 'kaminari'
33-
initialize_kaminari! and return
34-
rescue LoadError
35+
unless ApiPagination.paginator == :will_paginate
36+
begin
37+
require 'kaminari'
38+
initialize_kaminari! and return
39+
rescue LoadError
40+
end
3541
end
3642

3743
begin
@@ -65,4 +71,8 @@ def last_page?() !next_page end
6571
end
6672
end
6773

74+
if ENV['PAGINATOR']
75+
ApiPagination.instance_variable_set(:@paginator, ENV['PAGINATOR'].to_sym)
76+
end
77+
6878
ApiPagination::Hooks.init!

0 commit comments

Comments
 (0)