Rails and Stuff at Abel killed Cain

Ruby on Rails and other Webdevelopment

I wanted to have a rather simple pagination without next and previous buttons (yah, the post title told you, eh?).

However, did not find any option to deal with it. But Will_Paginate provides a nice way to overwrite the link renderer in order to have full customization.

So i created the following:

1
2
3
4
5
6
7
8
9
10
class PaginationListLinkRenderer < WillPaginate::LinkRenderer
 
  def to_html
    links = @options[:page_links] ? windowed_links : []
    links.unshift(page_link_or_span(@collection.previous_page, 'previous', @options[:previous_label])) if options[:do_prev]
    links.push(page_link_or_span(@collection.next_page, 'next', @options[:next_label])) if options[:do_next]
    html = links.join(@options[:separator])
    @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
  end
end

So from now on, using the new class ans renderer, i can add :do_next=>true IF i want to have next/prev links. per default its disabled as you can see.
activate it via

1
2
3
4
#environment.rb
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'PaginationListLinkRenderer'
#or template:
will_paginate @foo, :renderer=>PaginationListLinkRenderer

hf!

Write a Comment