Skip to content

[WIP] Make paginator definition explicit #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/api-pagination.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require 'api-pagination/version'
require 'api-pagination/hooks'

module ApiPagination
class << self
attr_reader :paginator
attr_writer :total_header, :per_page_header
attr_accessor :paginator
attr_writer :total_header, :per_page_header

def config
yield(self) if block_given?
ApiPagination::Hooks.init!
end

def paginate(collection, options = {})
options[:page] = options[:page].to_i
Expand Down Expand Up @@ -62,5 +68,3 @@ def per_page_header
end
end
end

require 'api-pagination/hooks'
68 changes: 38 additions & 30 deletions lib/api-pagination/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,44 @@ def self.init!
Grape::API.send(:include, Grape::Pagination)
end

# Kaminari and will_paginate conflict with each other, so we should check
# to see if either is already active before attempting to load them.
if defined?(Kaminari) && defined?(WillPaginate::CollectionMethods)
STDERR.puts <<-EOC
Warning: api-pagination relies on either Kaminari or WillPaginate, but these
gems conflict. Please ensure only one of them is active at any given time.

EOC
return
elsif defined?(Kaminari)
initialize_kaminari! and return
elsif defined?(WillPaginate::CollectionMethods)
initialize_will_paginate! and return
end
# If neither is loaded, we can safely attempt these requires.
unless ApiPagination.paginator == :will_paginate
begin
require 'kaminari'
initialize_kaminari! and return
rescue LoadError
end
# Make sure kaminari or will paginate are defined
begin
require 'kaminari'
rescue LoadError
end

begin
require 'will_paginate'
initialize_will_paginate! and return
rescue LoadError
end

STDERR.puts <<-EOC
Warning: api-pagination relies on either Kaminari or WillPaginate. Please
install either dependency by adding one of the following to your Gemfile:
if _no_paginator_found?
STDERR.puts <<-EOC
Warning: api-pagination relies on either Kaminari or WillPaginate. Please
install either dependency by adding one of the following to your Gemfile:

gem 'kaminari'
gem 'will_paginate'

gem 'kaminari'
gem 'will_paginate'
EOC
end

case ApiPagination.paginator
when :will_paginate
initialize_will_paginate! and return
when :kaminari
initialize_kaminari! and return
when nil
if _cannot_infer_paginator?
STDERR.puts <<-EOC
Warning: api-pagination relies on either Kaminari or WillPaginate, but these
gems conflict. Please set ApiPagination.paginator in an initializer.

EOC
return
end
end

EOC
end

def self.initialize_kaminari!
Expand All @@ -71,7 +72,14 @@ def last_page?() !next_page end

ApiPagination.instance_variable_set(:@paginator, :will_paginate)
end

def self._cannot_infer_paginator?
defined?(Kaminari) &&
defined?(WillPaginate::CollectionMethods) && ApiPagination.paginator.nil?
end

def self._no_paginator_found?
!defined?(Kaminari) && !defined?(WillPaginate::CollectionMethods)
end
end
end

ApiPagination::Hooks.init!