Class | WillPaginate::LinkRenderer |
In: |
lib/will_paginate/view_helpers.rb
|
Parent: | Object |
This class does the heavy lifting of actually building the pagination links. It is used by will_paginate helper internally.
gap_marker | [RW] |
The gap in page links is represented by:
<span class="gap">…</span> |
# File lib/will_paginate/view_helpers.rb, line 195 195: def initialize 196: @gap_marker = '<span class="gap">…</span>' 197: end
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 227 227: def html_attributes 228: return @html_attributes if @html_attributes 229: @html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class]) 230: # pagination of Post models will have the ID of "posts_pagination" 231: if @options[:container] and @options[:id] === true 232: @html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination' 233: end 234: @html_attributes 235: end
# File lib/will_paginate/view_helpers.rb, line 203 203: def prepare(collection, options, template) 204: @collection = collection 205: @options = options 206: @template = template 207: 208: # reset values in case we're re-using this instance 209: @total_pages = @param_name = @url_string = nil 210: 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 215 215: def to_html 216: links = @options[:page_links] ? windowed_links : [] 217: # previous/next buttons 218: links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:prev_label]) 219: links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label]) 220: 221: html = links.join(@options[:separator]) 222: @options[:container] ? @template.content_tag(:div, html, html_attributes) : html 223: end
# File lib/will_paginate/view_helpers.rb, line 290 290: def page_link(page, text, attributes = {}) 291: @template.link_to text, url_for(page), attributes 292: end
# File lib/will_paginate/view_helpers.rb, line 279 279: def page_link_or_span(page, span_class, text = nil) 280: text ||= page.to_s 281: 282: if page and page != current_page 283: classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last 284: page_link page, text, :rel => rel_value(page), :class => classnames 285: else 286: page_span page, text, :class => span_class 287: end 288: end
# File lib/will_paginate/view_helpers.rb, line 294 294: def page_span(page, text, attributes = {}) 295: @template.content_tag :span, text, attributes 296: 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 300 300: def url_for(page) 301: page_one = page == 1 302: unless @url_string and !page_one 303: @url_params = {} 304: # page links should preserve GET parameters 305: stringified_merge @url_params, @template.params if @template.request.get? 306: stringified_merge @url_params, @options[:params] if @options[:params] 307: 308: if complex = param_name.index(/[^\w-]/) 309: page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest). 310: parse_query_parameters("#{param_name}=#{page}") 311: 312: stringified_merge @url_params, page_param 313: else 314: @url_params[param_name] = page_one ? 1 : 2 315: end 316: 317: url = @template.url_for(@url_params) 318: return url if page_one 319: 320: if complex 321: @url_string = url.sub(%r!((?:\?|&)#{CGI.escape param_name}=)#{page}!, '\1@') 322: return url 323: else 324: @url_string = url 325: @url_params[param_name] = 3 326: @template.url_for(@url_params).split(//).each_with_index do |char, i| 327: if char == '3' and url[i, 1] == '2' 328: @url_string[i] = '@' 329: break 330: end 331: end 332: end 333: end 334: # finally! 335: @url_string.sub '@', page.to_s 336: end
Calculates visible page numbers using the :inner_window and :outer_window options.
# File lib/will_paginate/view_helpers.rb, line 254 254: def visible_page_numbers 255: inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i 256: window_from = current_page - inner_window 257: window_to = current_page + inner_window 258: 259: # adjust lower or upper limit if other is out of bounds 260: if window_to > total_pages 261: window_from -= window_to - total_pages 262: window_to = total_pages 263: end 264: if window_from < 1 265: window_to += 1 - window_from 266: window_from = 1 267: window_to = total_pages if window_to > total_pages 268: end 269: 270: visible = (1..total_pages).to_a 271: left_gap = (2 + outer_window)...window_from 272: right_gap = (window_to + 1)...(total_pages - outer_window) 273: visible -= left_gap.to_a if left_gap.last - left_gap.first > 1 274: visible -= right_gap.to_a if right_gap.last - right_gap.first > 1 275: 276: visible 277: end
Collects link items for visible page numbers.
# File lib/will_paginate/view_helpers.rb, line 240 240: def windowed_links 241: prev = nil 242: 243: visible_page_numbers.inject [] do |links, n| 244: # detect gaps: 245: links << gap_marker if prev and n > prev + 1 246: links << page_link_or_span(n, 'current') 247: prev = n 248: links 249: end 250: end
# File lib/will_paginate/view_helpers.rb, line 348 348: def current_page 349: @collection.current_page 350: end
# File lib/will_paginate/view_helpers.rb, line 356 356: def param_name 357: @param_name ||= @options[:param_name].to_s 358: end
# File lib/will_paginate/view_helpers.rb, line 340 340: def rel_value(page) 341: case page 342: when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '') 343: when @collection.next_page; 'next' 344: when 1; 'start' 345: end 346: end
Recursively merge into target hash by using stringified keys from the other one
# File lib/will_paginate/view_helpers.rb, line 361 361: def stringified_merge(target, other) 362: other.each do |key, value| 363: key = key.to_s # this line is what it's all about! 364: existing = target[key] 365: 366: if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?) 367: stringified_merge(existing || (target[key] = {}), value) 368: else 369: target[key] = value 370: end 371: end 372: end