Parent

WillPaginate::LinkRenderer

This class does the heavy lifting of actually building the pagination links. It is used by the will_paginate helper internally.

Attributes

gap_marker[RW]

The gap in page links is represented by:

<span class="gap">&hellip;</span>

Public Class Methods

new() click to toggle source
# File lib/will_paginate/view_helpers.rb, line 214
def initialize
  @gap_marker = '<span class="gap">&hellip;</span>'
end

Public Instance Methods

html_attributes() click to toggle source

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
prepare(collection, options, template) click to toggle source
  • 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
to_html() click to toggle source

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

Protected Instance Methods

page_span(page, text, attributes = {}) click to toggle source
# File lib/will_paginate/view_helpers.rb, line 315
def page_span(page, text, attributes = {})
  @template.content_tag :span, text, attributes
end
url_for(page) click to toggle source

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(%((?:\?|&amp;)#{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
visible_page_numbers() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.