Skip to content

Commit

Permalink
add support of query pagination with WillPaginate
Browse files Browse the repository at this point in the history
  • Loading branch information
josecoelho committed Aug 11, 2014
1 parent ad451af commit 64130ad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/chewy/query/pagination.rb
Expand Up @@ -13,3 +13,4 @@ def total_count
end

require 'chewy/query/pagination/kaminari' if defined?(::Kaminari)
require 'chewy/query/pagination/will_paginate' if defined?(::WillPaginate)
52 changes: 52 additions & 0 deletions lib/chewy/query/pagination/will_paginate.rb
@@ -0,0 +1,52 @@
module Chewy
class Query
module Pagination
module WillPaginate
include ::WillPaginate::CollectionMethods

def paginate(options = {})
@current_page = ::WillPaginate::PageNumber(options[:page] || @current_page || 1)
@page_multiplier = @current_page - 1

pp = (options[:per_page] || per_page || ::WillPaginate.per_page).to_i

#call Chewy::Query methods to limit results
per_page(pp)
offset( @page_multiplier * pp )
end

def current_page(value=:non_given)
if value == :non_given
@current_page
else
@current_page = value
end
end

def per_page(value = :non_given)
if value == :non_given
@per_page
else
@per_page = value
limit(value)
end
end

def page(page)
paginate(page: page)
end

def total_pages
(total_entries / per_page.to_f).ceil
end

def total_entries
@total_entries ||= self.total_count
end

end
end
end
end

Chewy::Query::Pagination.send :include, Chewy::Query::Pagination::WillPaginate

0 comments on commit 64130ad

Please sign in to comment.