Created
May 12, 2011 14:09
-
-
Save TertiumQuid/968566 to your computer and use it in GitHub Desktop.
Quick and Dirty Rails Pagination Extension With Scope
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 ActiveSupport | |
module PagedScope | |
def self.extended(base) | |
base.scope :paginated, lambda { |page,per_page| | |
page ||= 0 | |
per_page ||= 25 | |
base.limit( per_page.to_i ).offset( per_page.to_i * page.to_i ) | |
} | |
end | |
end | |
end | |
class User < ActiveRecord::Base | |
extend ActiveSupport::PagedScope | |
end | |
>> User.paginated(1,25).count | |
=> 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gist!
I'm a Rails beginner and was using will_paginate gem just for .paginate method alone, but for my case it did make more sense to implement some custom scope instead of a whole gem. So, based on your gist I did:
Example use: