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 the 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 205 205: def initialize 206: @gap_marker = '<span class="gap">…</span>' 207: 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 237 237: def html_attributes 238: return @html_attributes if @html_attributes 239: @html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class]) 240: # pagination of Post models will have the ID of "posts_pagination" 241: if @options[:container] and @options[:id] === true 242: @html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination' 243: end 244: @html_attributes 245: end
# File lib/will_paginate/view_helpers.rb, line 213 213: def prepare(collection, options, template) 214: @collection = collection 215: @options = options 216: @template = template 217: 218: # reset values in case we're re-using this instance 219: @total_pages = @param_name = @url_string = nil 220: 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 225 225: def to_html 226: links = @options[:page_links] ? windowed_links : [] 227: # previous/next buttons 228: links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:previous_label]) 229: links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label]) 230: 231: html = links.join(@options[:separator]) 232: @options[:container] ? @template.content_tag(:div, html, html_attributes) : html 233: end
# File lib/will_paginate/view_helpers.rb, line 300 300: def page_link(page, text, attributes = {}) 301: @template.link_to text, url_for(page), attributes 302: end
# File lib/will_paginate/view_helpers.rb, line 289 289: def page_link_or_span(page, span_class, text = nil) 290: text ||= page.to_s 291: 292: if page and page != current_page 293: classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last 294: page_link page, text, :rel => rel_value(page), :class => classnames 295: else 296: page_span page, text, :class => span_class 297: end 298: end
# File lib/will_paginate/view_helpers.rb, line 304 304: def page_span(page, text, attributes = {}) 305: @template.content_tag :span, text, attributes 306: 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 310 310: def url_for(page) 311: page_one = page == 1 312: unless @url_string and !page_one 313: @url_params = {} 314: # page links should preserve GET parameters 315: stringified_merge @url_params, @template.params if @template.request.get? 316: stringified_merge @url_params, @options[:params] if @options[:params] 317: 318: if complex = param_name.index(/[^\w-]/) 319: page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest). 320: parse_query_parameters("#{param_name}=#{page}") 321: 322: stringified_merge @url_params, page_param 323: else 324: @url_params[param_name] = page_one ? 1 : 2 325: end 326: 327: url = @template.url_for(@url_params) 328: return url if page_one 329: 330: if complex 331: @url_string = url.sub(%r!((?:\?|&)#{CGI.escape param_name}=)#{page}!, '\1@') 332: return url 333: else 334: @url_string = url 335: @url_params[param_name] = 3 336: @template.url_for(@url_params).split(//).each_with_index do |char, i| 337: if char == '3' and url[i, 1] == '2' 338: @url_string[i] = '@' 339: break 340: end 341: end 342: end 343: end 344: # finally! 345: @url_string.sub '@', page.to_s 346: end
Calculates visible page numbers using the :inner_window and :outer_window options.
# File lib/will_paginate/view_helpers.rb, line 264 264: def visible_page_numbers 265: inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i 266: window_from = current_page - inner_window 267: window_to = current_page + inner_window 268: 269: # adjust lower or upper limit if other is out of bounds 270: if window_to > total_pages 271: window_from -= window_to - total_pages 272: window_to = total_pages 273: end 274: if window_from < 1 275: window_to += 1 - window_from 276: window_from = 1 277: window_to = total_pages if window_to > total_pages 278: end 279: 280: visible = (1..total_pages).to_a 281: left_gap = (2 + outer_window)...window_from 282: right_gap = (window_to + 1)...(total_pages - outer_window) 283: visible -= left_gap.to_a if left_gap.last - left_gap.first > 1 284: visible -= right_gap.to_a if right_gap.last - right_gap.first > 1 285: 286: visible 287: end
Collects link items for visible page numbers.
# File lib/will_paginate/view_helpers.rb, line 250 250: def windowed_links 251: prev = nil 252: 253: visible_page_numbers.inject [] do |links, n| 254: # detect gaps: 255: links << gap_marker if prev and n > prev + 1 256: links << page_link_or_span(n, 'current') 257: prev = n 258: links 259: end 260: end
# File lib/will_paginate/view_helpers.rb, line 358 358: def current_page 359: @collection.current_page 360: end
# File lib/will_paginate/view_helpers.rb, line 366 366: def param_name 367: @param_name ||= @options[:param_name].to_s 368: end
# File lib/will_paginate/view_helpers.rb, line 350 350: def rel_value(page) 351: case page 352: when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '') 353: when @collection.next_page; 'next' 354: when 1; 'start' 355: end 356: end
Recursively merge into target hash by using stringified keys from the other one
# File lib/will_paginate/view_helpers.rb, line 371 371: def stringified_merge(target, other) 372: other.each do |key, value| 373: key = key.to_s # this line is what it's all about! 374: existing = target[key] 375: 376: if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?) 377: stringified_merge(existing || (target[key] = {}), value) 378: else 379: target[key] = value 380: end 381: end 382: end