Skip to content

Instantly share code, notes, and snippets.

@ryana
Created August 27, 2010 14:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryana/553473 to your computer and use it in GitHub Desktop.
Save ryana/553473 to your computer and use it in GitHub Desktop.
class RawRenderer < WillPaginate::LinkRenderer
def prepare col, opt, template
@collection = col
@options = opt
@options[:container] = nil
@template = template
@total_pages = @collection.total_pages
end
def page_link(page, text, attr = {})
"<a href='/#{root_page}?page=#{page}'>#{text}</a>"
end
def root_page
"items"
end
def page_span(page, text, attr = {})
"<span>#{text}</span>"
end
end
WillPaginate::ViewHelpers.pagination_options[:renderer] = RawRenderer
# Use will_paginate, Record is a MongoMapper document class. This is in your sinatra app:
require 'will_paginate'
require 'will_paginate/collection'
require 'will_paginate/view_helpers'
helpers do
include WillPaginate::ViewHelpers
end
# note /items is same as #root_page in the renderer
get '/items' do
limit = 20
page = params[:page] || 1
page = page.to_i
page -= 1
offset = limit * page
records = Records.all :limit => limit, :offset => offset
@records = WillPaginate::Collection.create(page + 1, limit, Record.count) {|p| p.replace records }
erb :your_view
end
# just use will_paginate
<%= will_paginate @items %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment