Reverse pagination with kaminari
Asked Answered
S

4

18

I want to create pagination for a messaging system in which the first page shown contains the oldest messages, with subsequent pages showing newer messages.

For example, if normal pagination for {a,b,c,d,e,f,g,h,i} with 3 per page is:

{a,b,c}, {d,e,f}, {g,h,i}

Then reverse pagination would be:

{g,h,i}, {d,e,f}, {a,b,c}

I plan to prepend the pages so the result is the same as normal pagination, only starting from the last page.

Is this possible with kaminari?

Senility answered 6/12, 2012 at 6:21 Comment(1)
Why do you not simply paginate the reversed dataset? YourModel.order("created_at ASC").page ?Quint
M
3

Kaminary.paginate_array does not produce query with offset and limit. For optimization reason, you shouldn't use this.

Instead you can do this:

@messages = query_for_message.order('created_at DESC').page(params[:page]).per(3)

Where query_for_message stands for any query which you use to retrieve the records for pagination. For example, it can be all the messages of a particular conversation.

Now in the view file, you just need to display @messages in the reverse order. For example:

<%= render :collection => @messages.reverse, :partial => 'message' %>
<%= paginate @messages %>
Morrissey answered 6/4, 2014 at 17:42 Comment(0)
R
2

There's a good example repo on Github called reverse_kaminari on github. It suggests an implementation along these lines (Source).

class CitiesController < ApplicationController

  def index
    @cities = prepare_cities City.order('created_at DESC')
  end

  private

  def prepare_cities(scope)
    @per_page = City.default_per_page
    total_count = scope.count
    rest_count = total_count > @per_page ? (total_count % @per_page) : 0
    @num_pages = total_count > @per_page ? (total_count / @per_page) : 1

    if params[:page]
      offset = params[:page].sub(/-.*/, '').to_i
      current_page = @num_pages - (offset - 1) / @per_page
      scope.page(current_page).per(@per_page).padding(rest_count)
    else
      scope.page(1).per(@per_page + rest_count)
    end
  end

end

All credits go to Andrew Djoga. He also hosted the app as a working demo.

Rawdon answered 18/3, 2013 at 10:20 Comment(0)
C
1

One way to solve this problem would be this one: Reverse pagination with kaminari? It does not look very clean nor optimal, but it works :)

Centuplicate answered 17/3, 2013 at 17:22 Comment(0)
D
0

Yes, but the method I have come up with isn't exactly pretty. Effectively, you have to set your own order:

Message.page(1).per(3).order("created_at DESC").reverse!

The problem with this approach is twofold:

First the reverse! call resolves the scope to an array and does the query, nerfing some of the awesome aspects of kaminari using AR scopes.

Second, as with any reverse pagination your offset is going to move, meaning that between two repeat calls, you could have exactly 3 new messages send and you would get the exact same data back. This problem is inherent with reverse pagination.

An alternative approach would be to interrogate the "last" page number and increment your page number down towards 1.

Dibb answered 28/12, 2012 at 22:1 Comment(2)
Something like: pages = Message.count % 3; Message.page(pages - i).per(3).order("created_at DESC") ?Inquire
Except order("created_at DESC") should be order("created_at ASC") if you are going to go with that approach.Dibb

© 2022 - 2024 — McMap. All rights reserved.