Skip to content

Commit

Permalink
render page links for zero or one page
Browse files Browse the repository at this point in the history
  • Loading branch information
keating committed Sep 28, 2012
1 parent 39a29ae commit 8c742f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
15 changes: 11 additions & 4 deletions lib/will_paginate/view_helpers.rb
Expand Up @@ -41,8 +41,7 @@ class << self
include WillPaginate::I18n

# Returns HTML representing page links for a WillPaginate::Collection-like object.
# In case there is no more than one page in total, nil is returned.
#
#
# ==== Options
# * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
# * <tt>:previous_label</tt> -- default: "« Previous"
Expand All @@ -59,6 +58,8 @@ class << self
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
# * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
# false only when you are rendering your own pagination markup (default: true)
# * <tt>:always_render</tt> -- if there is no more than one page in total, check this param
# to determine if will render the page links
#
# All options not recognized by will_paginate will become HTML attributes on the container
# element for pagination links (the DIV). For example:
Expand All @@ -70,8 +71,14 @@ class << self
# <div class="pagination" style="color:blue"> ... </div>
#
def will_paginate(collection, options = {})
# early exit if there is nothing to render
return nil unless collection.total_pages > 1
# check the always_render param
unless collection.total_pages > 1
if options[:always_render]
options.delete(:always_render)
else
return nil
end
end

options = WillPaginate::ViewHelpers.pagination_options.merge(options)

Expand Down
19 changes: 16 additions & 3 deletions spec/view_helpers/base_spec.rb
Expand Up @@ -19,11 +19,17 @@
include WillPaginate::ViewHelpers

describe "will_paginate" do
it "should render" do
collection = WillPaginate::Collection.new(1, 2, 4)
renderer = mock 'Renderer'

def builder_renderer collection
renderer = mock 'Renderer'
renderer.expects(:prepare).with(collection, instance_of(Hash), self)
renderer.expects(:to_html).returns('<PAGES>')
renderer
end

it "should render" do
collection = WillPaginate::Collection.new(1, 2, 4)
renderer = builder_renderer collection

will_paginate(collection, :renderer => renderer).should == '<PAGES>'
end
Expand All @@ -32,6 +38,13 @@
collection = mock 'Collection', :total_pages => 1
will_paginate(collection).should be_nil
end

it "should render for single-page collections with param always_render" do
collection = WillPaginate::Collection.new(1, 1, 1)
renderer = builder_renderer collection

will_paginate(collection, :renderer => renderer, :always_render => true).should == '<PAGES>'
end
end

describe "pagination_options" do
Expand Down

0 comments on commit 8c742f3

Please sign in to comment.