Created
January 10, 2012 19:30
-
-
Save maletor/1590681 to your computer and use it in GitHub Desktop.
This file contains 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
module Sunspot | |
module Search | |
class PaginatedCollection | |
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval|object_id/ } | |
attr_reader :current_page, :per_page | |
attr_accessor :total_count | |
alias :total_entries :total_count | |
alias :total_entries= :total_count= | |
alias :limit_value :per_page | |
def initialize(collection, page, per_page, total) | |
@collection = collection | |
@current_page = page | |
@per_page = per_page | |
@total_count = total | |
end | |
def total_pages | |
(total_count.to_f / per_page).ceil | |
end | |
alias :num_pages :total_pages | |
def first_page? | |
current_page == 1 | |
end | |
def last_page? | |
current_page >= total_pages | |
end | |
def previous_page | |
current_page > 1 ? (current_page - 1) : nil | |
end | |
def next_page | |
current_page < total_pages ? (current_page + 1) : nil | |
end | |
def out_of_bounds? | |
current_page > total_pages | |
end | |
def offset | |
(current_page - 1) * per_page | |
end | |
private | |
def method_missing(method, *args, &block) | |
@collection.send(method, *args, &block) | |
end | |
def respond_to?(*args) | |
super || @collection.respond_to?(*args) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment