This class does the heavy lifting of actually building the pagination links. It is used by the will_paginate helper internally.
Returns the subset of options this instance was initialized with that represent HTML attributes for the container element of pagination links.
# File lib/will_paginate/view_helpers.rb, line 247 def html_attributes return @html_attributes if @html_attributes @html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class]) # pagination of Post models will have the ID of "posts_pagination" if @options[:container] and @options[:id] === true @html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination' end @html_attributes end
collection is a WillPaginate::Collection instance or any other object that conforms to that API
options are forwarded from will_paginate view helper
template is the reference to the template being rendered
# File lib/will_paginate/view_helpers.rb, line 222 def prepare(collection, options, template) @collection = collection @options = options @template = template # reset values in case we're re-using this instance @total_pages = @param_name = @url_string = nil end
Process it! This method returns the complete HTML string which contains pagination links. Feel free to subclass LinkRenderer and change this method as you see fit.
# File lib/will_paginate/view_helpers.rb, line 234 def to_html links = @options[:page_links] ? windowed_links : [] # previous/next buttons links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:previous_label]) links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label]) html = links.join(@options[:separator]) html = html.html_safe if html.respond_to? :html_safe @options[:container] ? @template.content_tag(:div, html, html_attributes) : html end
# File lib/will_paginate/view_helpers.rb, line 311 def page_link(page, text, attributes = {}) @template.link_to text, url_for(page), attributes end
# File lib/will_paginate/view_helpers.rb, line 299 def page_link_or_span(page, span_class, text = nil) text ||= page.to_s text = text.html_safe if text.respond_to? :html_safe if page and page != current_page classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last page_link page, text, :rel => rel_value(page), :class => classnames else page_span page, text, :class => span_class end end
# File lib/will_paginate/view_helpers.rb, line 315 def page_span(page, text, attributes = {}) @template.content_tag :span, text, attributes end
Returns URL params for page_link_or_span, taking the current GET params and :params option into account.
# File lib/will_paginate/view_helpers.rb, line 321 def url_for(page) page_one = page == 1 unless @url_string and !page_one @url_params = {} # page links should preserve GET parameters stringified_merge @url_params, @template.params if @template.request.get? stringified_merge @url_params, @options[:params] if @options[:params] if complex = param_name.index(/[^\w-]/) page_param = parse_query_parameters("#{param_name}=#{page}") stringified_merge @url_params, page_param else @url_params[param_name] = page_one ? 1 : 2 end url = @template.url_for(@url_params) return url if page_one if complex @url_string = url.sub(%((?:\?|&)#{CGI.escape param_name}=)#{page}!, "\\1\00"") return url else @url_string = url @url_params[param_name] = 3 @template.url_for(@url_params).split(//).each_with_index do |char, i| if char == '3' and url[i, 1] == '2' @url_string[i] = "\00"" break end end end end # finally! @url_string.sub "\00"", page.to_s end
Calculates visible page numbers using the :inner_window and :outer_window options.
# File lib/will_paginate/view_helpers.rb, line 274 def visible_page_numbers inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i window_from = current_page - inner_window window_to = current_page + inner_window # adjust lower or upper limit if other is out of bounds if window_to > total_pages window_from -= window_to - total_pages window_to = total_pages end if window_from < 1 window_to += 1 - window_from window_from = 1 window_to = total_pages if window_to > total_pages end visible = (1..total_pages).to_a left_gap = (2 + outer_window)...window_from right_gap = (window_to + 1)...(total_pages - outer_window) visible -= left_gap.to_a if left_gap.last - left_gap.first > 1 visible -= right_gap.to_a if right_gap.last - right_gap.first > 1 visible end
Collects link items for visible page numbers.
# File lib/will_paginate/view_helpers.rb, line 260 def windowed_links prev = nil visible_page_numbers.inject [] do |links, n| # detect gaps: links << gap_marker if prev and n > prev + 1 links << page_link_or_span(n, 'current') prev = n links end end
Generated with the Darkfish Rdoc Generator 2.