class Leaf::ViewHelpers::LinkRenderer

This class does the heavy lifting of actually building the pagination links. It is used by leaf 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/leaf/view_helpers/link_renderer.rb, line 35
def container_attributes
  @container_attributes ||= begin
    attributes = @options.except *(Leaf::ViewHelpers.pagination_options.keys - [:class])
    # pagination of Post models will have the ID of "posts_pagination"
    if @options[:container] and @options[:id] === true
      attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
    end
    attributes
  end
end
prepare(collection, options, template) click to toggle source
  • collection is a Leaf::Collection instance or any other object that conforms to that API

  • options are forwarded from leaf view helper

  • template is the reference to the template being rendered

Calls superclass method
# File lib/leaf/view_helpers/link_renderer.rb, line 14
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/leaf/view_helpers/link_renderer.rb, line 23
def to_html
  html = pagination.map do |item|
    item.is_a?(Fixnum) ?
      page_number(item) :
      send(item)
  end.join(@options[:separator])
  
  @options[:container] ? html_container(html) : html
end

Protected Instance Methods

gap() click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 56
def gap
  '<span class="gap">&hellip;</span>'
end
html_container(html) click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 76
def html_container(html)
  tag(:div, html, container_attributes)
end
next_page() click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 64
def next_page
  previous_or_next_page(@collection.next_page, @options[:next_label], 'next_page')
end
page_number(page) click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 48
def page_number(page)
  unless page == current_page
    link(page, page, :rel => rel_value(page))
  else
    tag(:em, page)
  end
end
previous_or_next_page(page, text, classname) click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 68
def previous_or_next_page(page, text, classname)
  if page
    link(text, page, :class => classname)
  else
    tag(:span, text, :class => classname + ' disabled')
  end
end
previous_page() click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 60
def previous_page
  previous_or_next_page(@collection.previous_page, @options[:previous_label], 'previous_page')
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/leaf/view_helpers/link_renderer.rb, line 82
def url(page)
  raise NotImplementedError
end

Private Instance Methods

rel_value(page) click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 107
def rel_value(page)
  case page
  when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '')
  when @collection.next_page; 'next'
  when 1; 'start'
  end
end
symbolized_update(target, other) click to toggle source
# File lib/leaf/view_helpers/link_renderer.rb, line 115
def symbolized_update(target, other)
  other.each do |key, value|
    key = key.to_sym
    existing = target[key]
    
    if value.is_a?(Hash) 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/leaf/view_helpers/link_renderer.rb, line 97
def tag(name, value, attributes = {})
  string_attributes = attributes.inject('') do |attrs, pair|
    unless pair.last.nil?
      attrs << %( #{pair.first}="#{Rack::Utils.escape_html(pair.last.to_s)}")
    end
    attrs
  end
  "<#{name}#{string_attributes}>#{value}</#{name}>"
end