Last active
August 29, 2015 14:15
-
-
Save cflipse/07beec78382b911ebd2b to your computer and use it in GitHub Desktop.
pagination wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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