Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

render page links for zero or one page #270

Closed
wants to merge 4 commits into from

2 participants

@keating

This is a demand in one of my projects.
The client says the pagination info should always has the same behavior, it should not disappear at a time and appear at another time.
So, I add a param 'always_render' for always showing the pagination info.

@mislav
Owner

I don't consider this feature to be useful. See my comment on #76

@mislav mislav closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
View
15 lib/will_paginate/view_helpers.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)
View
19 spec/view_helpers/base_spec.rb
@@ -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 build_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 = build_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 = build_renderer collection
+
+ will_paginate(collection, :renderer => renderer, :always_render => true).should == '<PAGES>'
+ end
end
describe "pagination_options" do
Something went wrong with that request. Please try again.