1

I have kinda blog/wiki application where I'd like the home page to include a welcome/landing message and the 5 most recent blog entries and pagination linking to older entries.

Is it possible to have, for example, 5 pages returned as page one of paginated search results and 15 for subsequent pages? I'm currently using will_paginate.

mark
  • 10,316
  • 6
  • 37
  • 58

4 Answers4

4

You can use WillPaginate::Collection, here is an example you can use for this :

def self.find_with_pagination(params = {})
  WillPaginate::Collection.create(params[:page].to_i < 1 ? 1 : params[:page], per_page_for_page(params[:page])) do |pager|
    # inject the result array into the paginated collection:
    pager.replace(find(:all, params.merge({:limit => pager.per_page, :offset => pager.offset)}))
    unless pager.total_entries
      # the pager didn't manage to guess the total count, do it manually
      pager.total_entries = self.count
    end
  end
end

def self.offset_for_page(page_number)
  page_number.to_i > 1 ? ((page_number.to_i - 2) * 15 + 5) : 0
end

def self.per_page_for_page(page_number)
  page_number.to_i > 1 ? 15 : 5
end

I hope it would help, here is a link for the doc : http://rdoc.info/github/mislav/will_paginate/master/WillPaginate/Collection

Zabba
  • 64,285
  • 47
  • 179
  • 207
jrichardlai
  • 3,317
  • 4
  • 21
  • 24
  • Thanks very much. Looks great but haven't time to check it out at the mo. Sure it'll be great but will get back. – mark Feb 01 '11 at 19:53
1

To me, it sounds like you have two distinct views that you're trying to combine into one: "Welcome" and "Archives". It might be simpler just to split your one page into two:

  • The "Welcome" page, which shows the welcome message, the latest X posts, and a link to "Older Posts".
  • The "Archives" page, which has all the posts, will_paginated as necessary. Yes, the first five posts will show up here, too, but that's expected (and probably good) in an archive.

Just a different way to think about things - hope it helps!

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
  • Hi Xavier. Thanks for answer but in my case this approach won't work. I don't see them as distinct views really; this approach is fairly typical to blogs. Imagine the newest posts having larger representation on the front page say 1- 5 with 6 - 10 normal size. Then on page two I'll have 15 posts normal size in a similar total page size. Thanks again for the idea though. – mark Feb 04 '11 at 09:52
0

Yes, the answer is a bit old, but I will give here my solution on Rails 4.2, because it is a bit different.

I need 10 results on first page, and 12 - on others.

item.rb

def self.find_with_pagination(params = {}, filters = {})
  WillPaginate::Collection.create(params[:page].to_i < 1 ? 1 : params[:page], per_page_for_page(params[:page])) do |pager|
    result = Item.all.limit(pager.per_page).offset(offset_for_page(params[:page])).where(filters)
    pager.replace result
    unless pager.total_entries
      # the pager didn't manage to guess the total count, do it manually
      pager.total_entries = self.count
    end
  end
end

def self.offset_for_page(page_number)
  page_number.to_i > 1 ? ((page_number.to_i - 2) * 12 + 10) : 0
end

def self.per_page_for_page(page_number)
  page_number.to_i > 1 ? 12 : 10
end

your_super_controller.rb

@items = Item.find_with_pagination(params, @filters)
goooseman
  • 619
  • 5
  • 12
0

I haven't tried this, but perhaps by overriding the params[:per_page] on the subsequent pages will work. Something like:

Since the controller is something like:

 @posts = Post.paginate :page => params[:page], :per_page => 10, :include => [:posts], :conditions => ["post.user_id = ?", current_user.id], :order => "title,created_at"

the view perhaps could have something like this in it:

<%= params[:page] == 1 ? will_paginate @posts : will_paginate @posts, :per_page => 15 %>
jschorr
  • 3,034
  • 1
  • 17
  • 20
  • 2
    Hi karudzo and thanks for your answer. Problem with that approach is posts 6 - 10 will never be seen. – mark Jan 28 '11 at 19:25