class WillPaginate::ViewHelpers::LinkRenderer
This class does the heavy lifting of actually building the pagination links. It is used by will_paginate
helper internally.
Public Instance Methods
container_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/link_renderer.rb, line 37 def container_attributes @container_attributes ||= { :role => 'navigation', :"aria-label" => @template.will_paginate_translate(:container_aria_label) { 'Pagination' } }.update @options.except(*(ViewHelpers.pagination_options.keys + [:renderer] - [:class])) end
prepare(collection, options, template)
click to toggle source
-
collection
is aWillPaginate::Collection
instance or any other object that conforms to that API -
options
are forwarded fromwill_paginate
view helper -
template
is the reference to the template being rendered
Calls superclass method
# File lib/will_paginate/view_helpers/link_renderer.rb, line 16 def prepare(collection, options, template) super(collection, options) @template = template @container_attributes = @base_url_params = 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/link_renderer.rb, line 25 def to_html html = pagination.map do |item| item.is_a?(Integer) ? page_number(item) : send(item) end.join(@options[:link_separator]) @options[:container] ? html_container(html) : html end
Protected Instance Methods
gap()
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 55 def gap text = @template.will_paginate_translate(:page_gap) { '…' } %(<span class="gap">#{text}</span>) end
html_container(html)
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 80 def html_container(html) tag(:div, html, container_attributes) end
next_page()
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 66 def next_page num = @collection.current_page < total_pages && @collection.current_page + 1 aria_label = @template.will_paginate_translate(:next_aria_label) { "Next page" } previous_or_next_page(num, @options[:next_label], 'next_page', aria_label) end
page_number(page)
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 46 def page_number(page) aria_label = @template.will_paginate_translate(:page_aria_label, :page => page.to_i) { "Page #{page}" } if page == current_page tag(:em, page, :class => 'current', :"aria-label" => aria_label, :"aria-current" => 'page') else link(page, page, :rel => rel_value(page), :"aria-label" => aria_label) end end
previous_or_next_page(page, text, classname, aria_label = nil)
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 72 def previous_or_next_page(page, text, classname, aria_label = nil) if page link(text, page, :class => classname, :'aria-label' => aria_label) else tag(:span, text, :class => classname + ' disabled', :'aria-label' => aria_label) end end
previous_page()
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 60 def previous_page num = @collection.current_page > 1 && @collection.current_page - 1 aria_label = @template.will_paginate_translate(:previous_aria_label) { "Previous page" } previous_or_next_page(num, @options[:previous_label], 'previous_page', aria_label) end
url(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/link_renderer.rb, line 86 def url(page) raise NotImplementedError end
Private Instance Methods
link(text, target, attributes = {})
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 96 def link(text, target, attributes = {}) if target.is_a?(Integer) attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target tag(:a, text, attributes) end
param_name()
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 92 def param_name @options[:param_name].to_s end
rel_value(page)
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 114 def rel_value(page) case page when @collection.current_page - 1; 'prev' when @collection.current_page + 1; 'next' end end
symbolized_update(target, other, blacklist = nil)
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 121 def symbolized_update(target, other, blacklist = nil) other.each_pair do |key, value| key = key.to_sym existing = target[key] next if blacklist && blacklist.include?(key) if value.respond_to?(:each_pair) and (existing.is_a?(Hash) or existing.nil?) symbolized_update(existing || (target[key] = {}), value) else target[key] = value end end end
tag(name, value, attributes = {})
click to toggle source
# File lib/will_paginate/view_helpers/link_renderer.rb, line 105 def tag(name, value, attributes = {}) string_attributes = attributes.map do |pair| unless pair.last.nil? %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}") end end "<#{name}#{string_attributes.compact.join("")}>#{value}</#{name}>" end