Created
April 14, 2011 21:05
-
-
Save tiegz/920550 to your computer and use it in GitHub Desktop.
a will_paginate extension
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
module WillPaginate | |
class Collection | |
# This is a special case of paginate() for _dubstep_ fans (_hyper-dubstep_ not supported). | |
# It is different in this regard: | |
# * The first page should load +first_per_page+ records | |
# * The rest of the pages should load +more_per_page+ records | |
# | |
# NOTE: will_paginate view helpers not really tested with this (eg the '1,2,3...N' thing) | |
# | |
def self.dubstep_paginate(finder, page = 1, first_per_page = 15, more_per_page = 15, finder_opts = {}) | |
page = [page.to_i, 1].max | |
per_page = page == 1 ? first_per_page : more_per_page | |
WillPaginate::Collection.create(page, per_page, finder.count(finder_opts)) do |pager| | |
# Dubstep-remix of total_pages | |
total_pages = case pager.total_entries | |
when 0 then 0 | |
when (1..3) then 1 | |
else ((pager.total_entries - 3) / more_per_page).succ.succ | |
end | |
pager.instance_variable_set(:@total_pages, total_pages) | |
pager.replace(finder.all(finder_opts.update({ | |
:limit => pager.per_page, | |
:offset => pager.dubstep_offset | |
}))) | |
end | |
end | |
def dubstep_offset | |
current_page > 1 ? (current_page.pred.pred * per_page) + 3 : 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment