views:

514

answers:

1

I'm working with the Rails mislav-will_paginate plugin to paginate my records. I want to produce the following output, regardless of whether there were multiple pages:

X - Y of Z
1 - 100 of 1826

will_paginate in WillPaginate::ViewHelpers returns nil if there was only one page of records. I want to override this in the cleanest possible way to produce the above output if there was only one page, or the above output surrounded by the usual output if there were multiple pages.

I've overridden WillPaginate::LinkRenderer's to_html method to produce this output, but I can't figure out how to produce it if there is only one page of records.

Should I be moving the code to produce X - Y of Z somewhere else and calling it both from my overridding to_html method, and from the place where I make my will_paginate call if it returns nil? If so, where should it live?

A: 

Ok, here's what I did:

  • Created a module Paginator
  • Included the Paginator module in my model's helper
  • Called Paginator.paginate in my views
  • Defined Paginator.paginate to call Paginator.produce_totals if there was only one page, or to call will_paginate (the normal method) for multiple pages
  • Defined Paginator.produce_totals to output the "X - Y of Z" totals as above
  • Included the Paginator module in my WillPaginate::LinkRenderer overriding class
  • Called Paginator.produce_totals at the appropriate point in my overriding to_html method

Phew! Can post code if it's useful to anyone - just ask.

nfm
Why not make this a plugin which extends will_paginate, or fork/branch will_paginate to include this support and submit a pull-request?
Matthew Savage