Skip to content

Commit

Permalink
:semantic => true == ul/li pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
leshill committed Jun 26, 2009
1 parent 0ee5b65 commit 6db9356
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
31 changes: 29 additions & 2 deletions lib/will_paginate/view_helpers.rb
Expand Up @@ -32,7 +32,8 @@ module ViewHelpers
:params => nil,
:renderer => 'WillPaginate::LinkRenderer',
:page_links => true,
:container => true
:container => true,
:semantic => false
}
mattr_reader :pagination_options

Expand All @@ -56,6 +57,7 @@ module ViewHelpers
# * <tt>:id</tt> -- HTML ID for the container (default: nil). Pass +true+ to have the ID
# automatically generated from the class name of objects in collection: for example, paginating
# ArticleComment models would yield an ID of "article_comments_pagination".
# * <tt>:semantic</tt> -- pagination links rendered with UL/LI tags
#
# Advanced options:
# * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
Expand Down Expand Up @@ -103,6 +105,13 @@ def will_paginate(collection = nil, options = {})
options[:previous_label] = options.delete(:prev_label)
end

# semantic link rendering
if options[:semantic]
options[:renderer] = "WillPaginate::SemanticLinkRenderer"
options[:separator] = nil
options[:container] = true
end

# get the renderer instance
renderer = case options[:renderer]
when String
Expand Down Expand Up @@ -234,7 +243,8 @@ def to_html
links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label])

html = links.join(@options[:separator])
@options[:container] ? @template.content_tag(:div, html, html_attributes) : html
container_tag = @options[:semantic] ? :ul : :div
@options[:container] ? @template.content_tag(container_tag, html, html_attributes) : html
end

# Returns the subset of +options+ this instance was initialized with that
Expand Down Expand Up @@ -401,4 +411,21 @@ def parse_query_parameters(params)
end
end
end

class SemanticLinkRenderer < LinkRenderer

def initialize
@gap_marker = '<li class="gap">&hellip;</li>'
end

protected
def page_link(page, text, attributes = {})
li_attributes = attributes.has_key?(:class) ? {:class => attributes[:class] } : {}
@template.content_tag(:li, @template.link_to(text, url_for(page), attributes), li_attributes)
end

def page_span(page, text, attributes = {})
@template.content_tag(:li, text, attributes)
end
end
end
3 changes: 2 additions & 1 deletion test/lib/view_test_process.rb
Expand Up @@ -68,8 +68,9 @@ def paginate(collection = {}, options = {}, &block)
@html_document = HTML::Document.new(@html_result, true, false)

if block_given?
elementname = options[:semantic] ? 'ul' : 'div'
classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
assert_select("div.#{classname}", 1, 'no main DIV', &block)
assert_select("#{elementname}.#{classname}", 1, "no main #{elementname.upcase}", &block)
end
end

Expand Down
22 changes: 22 additions & 0 deletions test/view_test.rb
Expand Up @@ -370,4 +370,26 @@ def test_rescue_response_hook_presence
end
end

## semantic pagination ##

def test_semantic
paginate([1].paginate({ :page => 1, :total_entries => 13, :per_page => 4 }), :semantic => true) do |pagination|
assert_select 'li a[href]', 4 do |elements|
validate_page_numbers [2,3,4,2], elements
assert_select elements.last, ':last-child', "Next &raquo;"
end
assert_select 'li', 6
assert_select 'li.disabled.prev_page', '&laquo; Previous'
assert_select 'li.current', '1'
assert_select 'li a[rel=next]', '2'
assert_select 'li.next_page a.next_page[rel=next]', 'Next &raquo;'
assert_equal %Q(<ul class="pagination"><li class="disabled prev_page">&laquo; Previous</li><li class="current">1</li><li><a href="/foo/bar?page=2" rel="next">2</a></li><li><a href="/foo/bar?page=3">3</a></li><li><a href="/foo/bar?page=4">4</a></li><li class="next_page"><a href="/foo/bar?page=2" class="next_page" rel="next">Next &raquo;</a></li></ul>), @html_result
end
end

def test_semantic_gap
paginate([1].paginate({ :page => 2, :total_entries => 25, :per_page => 4 }), :inner_window => 1, :semantic => true) do |pagination|
assert_select 'li.gap', '&hellip;'
end
end
end

0 comments on commit 6db9356

Please sign in to comment.