Pagination: Last Page


#1

Hi Folks

is there any way to link to the last page of a paginated list, if the
number of pages is not available in this moment? For example a forum
with topic and replies. I’d like to link to the last page of the
replies, but I don’t know how many pages there are in the forum view?
I’m using mislav-will_paginate for pagination.

Thank you in advance!


#2

Patrick Heneise wrote:

Hi Folks

is there any way to link to the last page of a paginated list, if the
number of pages is not available in this moment? For example a forum
with topic and replies. I’d like to link to the last page of the
replies, but I don’t know how many pages there are in the forum view?
I’m using mislav-will_paginate for pagination.

Thank you in advance!

If you have an object that you have paginated, you can get the number of
pages with .total_pages: this will be the same as the number of the last
page.

Presumably when you’re looking at the topic, you’ve already loaded it’s
replies anyway?

eg

@topic = Topic.find(params[:id])
@replies = @topic.replies.paginate(:page => 1, :per_page => 20)

last_page = @replies.total_pages


#3

Max W. wrote:

If you have an object that you have paginated, you can get the number of
pages with .total_pages: this will be the same as the number of the last
page.

Presumably when you’re looking at the topic, you’ve already loaded it’s
replies anyway?

eg

@topic = Topic.find(params[:id])
@replies = @topic.replies.paginate(:page => 1, :per_page => 20)

last_page = @replies.total_pages

Well, the problem is that the replies are not yet paginated. It’s a
summary on a landing-page and the last three replies should show up for
each topic. The total_pages attribute is not know at this point.


#4

i had the same problem. some people already asked this feature to
be added to will_paginate, apparently the developer doesn’t think
it is a good idea. check this:

http://groups.google.com/group/will_paginate/browse_thread/thread/9c4ff183a4271bde/3f71f7dc31a54767

for my projects i made an extension to make will_paginate accept
:page => :last.

Patrick Heneise wrote:

Max W. wrote:

If you have an object that you have paginated, you can get the number of
pages with .total_pages: this will be the same as the number of the last
page.

Presumably when you’re looking at the topic, you’ve already loaded it’s
replies anyway?

eg

@topic = Topic.find(params[:id])
@replies = @topic.replies.paginate(:page => 1, :per_page => 20)

last_page = @replies.total_pages

Well, the problem is that the replies are not yet paginated. It’s a
summary on a landing-page and the last three replies should show up for
each topic. The total_pages attribute is not know at this point.


#5

PP Junty wrote:

i had the same problem. some people already asked this feature to
be added to will_paginate, apparently the developer doesn’t think
it is a good idea. check this:

http://groups.google.com/group/will_paginate/browse_thread/thread/9c4ff183a4271bde/3f71f7dc31a54767

for my projects i made an extension to make will_paginate accept
:page => :last.

Thanks for this hint. Too bad Mislav doesn’t accept this point. Do you
have a link for your extension?

Thank you!


#6

ok, i made a little plugin with this functionality, it is attached.
definitely not a pro solution but may be useful. could you test
it and see if it is working? if it is i could publish it on github.
this plugin also alter another behavior that i find annoying, which is:
when you request a page greater than the total pages will_paginate
returns an empty page. that happens specially when the last item of
a page is deleted. the extension will always return the last page in
this case.

Patrick Heneise wrote:

Thanks for this hint. Too bad Mislav doesn’t accept this point. Do you
have a link for your extension?

Thank you!


#7

just a correction.
by
“that happens specially when the last item of a page is deleted.”
i meant
“that happens specially when the last page has only one item and the
item is deleted.”


#8

PP Junty wrote:

ok, i made a little plugin with this functionality, it is attached.
definitely not a pro solution but may be useful. could you test
it and see if it is working? if it is i could publish it on github.
this plugin also alter another behavior that i find annoying, which is:
when you request a page greater than the total pages will_paginate
returns an empty page. that happens specially when the last item of
a page is deleted. the extension will always return the last page in
this case.

Thanks for the code. Didn’t catch the attachment at first sight. I
tested the plugin and it works quite fine. However, I have a small
problem while selecting a specific page. :page => :last always redirects
to the last page, even if a parameter is given. I tried to check the
parameter before, but this throws an error as well:

if (params[:page].nil?)
@replies = @bullet.replies.paginate :page => :last, :order =>
‘created_at ASC’, :per_page => 10
else
@replies = @bullet.replies.paginate :page => params[:page], :order =>
‘created_at ASC’, :per_page => 10
end

Throws:
comparison of String with 2 failed

/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
>' /Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:inpaginate’


#9

you should use blank? instead of nil? because even the original
will_paginate will throw an error if you pass an empty string.
and you should not rely on what you receive in the param :page as
a user could manually alter the url to some strange value. what
i do is to have a parse method that ensure that will_paginate is
getting a valid page number, that is why i didn’t bother throwing
a more informative error message, i’ll see if it can be improved.

Thanks for the code. Didn’t catch the attachment at first sight. I
tested the plugin and it works quite fine. However, I have a small
problem while selecting a specific page. :page => :last always redirects
to the last page, even if a parameter is given. I tried to check the
parameter before, but this throws an error as well:

if (params[:page].nil?)
@replies = @bullet.replies.paginate :page => :last, :order =>
‘created_at ASC’, :per_page => 10
else
@replies = @bullet.replies.paginate :page => params[:page], :order =>
‘created_at ASC’, :per_page => 10
end

Throws:
comparison of String with 2 failed

/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
>' /Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:inpaginate’


#10

PP Junty wrote:

you should use blank? instead of nil? because even the original
will_paginate will throw an error if you pass an empty string.
and you should not rely on what you receive in the param :page as
a user could manually alter the url to some strange value. what
i do is to have a parse method that ensure that will_paginate is
getting a valid page number, that is why i didn’t bother throwing
a more informative error message, i’ll see if it can be improved.

I solved the problem with a simple integer validation:

@page = params[:page].to_i
if (@page != 0)
@replies = @bullet.replies.paginate :page => @page, :order =>
‘created_at ASC’, :per_page => 10
else
@replies = @bullet.replies.paginate :page => :last, :order =>
‘created_at ASC’, :per_page => 10
end

Works pretty good.

Thank you for this enhancement to will_paginate. You should definitely
put it to github.