Permalink
Browse files

render page links for zero or one page

  • Loading branch information...
1 parent 39a29ae commit 8c742f3e1f459fd1e0d226d7c7980a949c8c4ee6 @keating committed Sep 28, 2012
Showing with 27 additions and 7 deletions.
  1. +11 −4 lib/will_paginate/view_helpers.rb
  2. +16 −3 spec/view_helpers/base_spec.rb
@@ -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"
@@ -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:
@@ -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)
@@ -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
@@ -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

0 comments on commit 8c742f3

Please sign in to comment.