Skip to content

Instantly share code, notes, and snippets.

@andyferra
Created September 26, 2008 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyferra/13036 to your computer and use it in GitHub Desktop.
Save andyferra/13036 to your computer and use it in GitHub Desktop.
Generates per_page links for will_paginate
def per_page_links(collection, options={})
unless collection.empty?
separator = options[:seperator] || ' '
css_class = options[:class] || 'per_page_links'
param = options[:param] || :per_page
per_page = collection.first.class.per_page
options = [per_page/2, per_page, per_page*2]
value = params[param] || per_page.to_s
link = lambda {|p| url_for(params.merge({param => p}))}
item = lambda {|p, current_value| current_value == p.to_s ? content_tag(:span, p, :class => 'current') : link_to(p, link.call(p)) }
content_tag(:div, options.map {|p| item.call(p, value)}.join(separator), :class => css_class)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment