views:

73

answers:

1

Hello, I'm using the Rails 3 plugin Will_Paginate and have been using the following tutorial to learn how to customize the pagination links:

http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3/

My question is, how to make the pagination links more like GMAIL, example:

1 - 100 of 25409 Older › Oldest »

‹ Newer 101 - 200 of 25409 Older › Oldest »

« Newest ‹ Newer 25401 - 25409 of 25409

Thanks

+2  A: 

It looks like that link has (almost) all of the information you need.

"Older" is basically "Next Page", so you override the next_page method in your renderer. "Oldest" is "Last Page"; you'll need to add a method and then make sure it's included in the array returned by your pagination method (the total_pages method built into will_paginate will help here).

Then do the reverse for Newer/Newest.

Take a look at the link_renderer.rb and link_renderer_base.rb files. They have the methods you'll be overriding.

I wrote a custom will_paginate 3 renderer to emulate the GitHub/Twitter-style "More" pagination. I've annotated the code below. It won't get you exactly where you need to go, but it's a start.

module TwitterPagination
  class LinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
    protected

    # Tells WP how to render the "Next Page" link
    def next_page
      # The only difference from the default here is we renamed the link to "More" 
      # and added a custom class, twitter_pagination
      previous_or_next_page(@collection.next_page, "More", 'twitter_pagination') if @collection.next_page
    end

    # Remove all links except our :next_page
    def pagination
      [ :next_page ]
    end
  end
end

Everything I needed to know in order to do this can be figured out by reading the two source files I linked above.

It really amazed me how easy this was to do; the latest design of will_paginate is brilliant in this area.

rspeicher
AnApprentice
Note the `if @collection.next_page` conditional - it only shows "More" if there's actually a next page. As for numbering the items, that's a bit more complicated. Look into the [collection.rb](http://github.com/mislav/will_paginate/blob/v3.0.pre2/lib/will_paginate/collection.rb) file. It's basically an array with extra methods, so you can call `@collection.size` to know the number of items in this group; multiply that by `current_page` to get the start, add the number of items in the group to the start to get the max bound, etc.
rspeicher
got it - thank you!
AnApprentice
Just as a followup, yesterday I released a Gem that aims to provide different pre-built renderers for will_paginate, and one of them is Gmail. Again it turned out to be pretty easy (harder to test than to write, even). https://rubygems.org/gems/will_paginate_renderers
rspeicher