Skip to content

Instantly share code, notes, and snippets.

@cflipse
Last active August 29, 2015 14:15
Show Gist options
  • Save cflipse/07beec78382b911ebd2b to your computer and use it in GitHub Desktop.
Save cflipse/07beec78382b911ebd2b to your computer and use it in GitHub Desktop.
pagination wrapper
require 'charlatan'
# A simple pagination wrapper that implements the handful of
# methods that Kaminari wants for displaying pagination.
#
# Currently requires implementing a `window` function in the
# relation as well. `limit().offeset()` filters are what make
# up that method.
class DatasetPagination
include Charlatan.new(:relation)
attr_reader :current_page
def initialize(relation, current_page = nil)
super relation
@current_page = current_page
end
def page(num = 1)
num = num.blank? ? 1 : num.to_i
self.class.new relation.window( (num - 1) * per_page, per_page), num
end
def total
relation.relation.dataset.unlimited.count
end
def total_pages
(total / per_page) + 1
end
def per_page
25
end
alias_method :limit_value, :per_page
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment