Created
July 15, 2009 21:03
-
-
Save mislav/147985 to your computer and use it in GitHub Desktop.
Experiment in pagination that defaults to the last page
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
ActiveRecord::Base.class_eval do | |
def self.paginate_in_reverse(options = {}) | |
unless options[:page] | |
# we only default to the last page if no explicit page has been given | |
total_entries = self.count(:conditions => options[:conditions]) | |
# calculate the last page | |
total_pages = (total_entries / self.per_page.to_f).ceil | |
# update the options hash to hold this information | |
options = options.merge(:page => total_pages, :total_entries => total_entries) | |
end | |
# do the usual stuff | |
self.paginate(options) | |
end | |
end | |
# example usage: | |
User.paginate_in_reverse :page => params[:page], | |
:conditions => 'name IS NOT NULL', :order => 'created_at DESC' | |
# now if params[:page] is nil (there's no explicit page), this will default to | |
# the last page in the collection instead of page 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment