begriffs / clean_pagination

API pagination the way RFC7233 intended it
MIT License
33 stars 3 forks source link

Collection is NOT displayed if count of items equals 1 #9

Open mpz opened 9 years ago

mpz commented 9 years ago
max_page = 10
paginate companies.count, max_page do |limit, offset|
      render json: {
              :companies=>companies.limit(limit).offset(offset)
                .as_json()}
    end

if companies.count is 1 request return JSON with one item but it is NOT displayed on client. max_page can be 0,1 or 2 - no any effect

begriffs commented 9 years ago

Thanks for opening this issue. Can you write a failing test that demonstrates the problem and send it in a pull request?

(Actually a pull request with the solution as well would be much appreciated as I am a bit overwhelmed by other projects right now. :smile: )

begriffs commented 9 years ago

Oh man it's been a while, sorry for neglecting this issue. Is this still a problem for you?

mpz commented 9 years ago

Yes i still have problem with this issue.

Now i have such bug workaround:

  MAX_PER_PAGE = 100  # max per page in paginator

  def get_max_per_page_for(count)
    # bug work around for CleanPagination
    return MAX_PER_PAGE if count == 0
    return 1 if count == 1
    count < MAX_PER_PAGE ? count - 1 : MAX_PER_PAGE # because not visible for less count
  end

  def index
    count = Event.count
    if count == 0
      render json: { result: 'no any events' }
      return
    end

    paginate count, get_max_per_page_for(count) do |limit, offset|
      render json: {
        events: Event.all.limit(limit).offset(offset).as_json()
      }
    end
  end
begriffs commented 9 years ago

Certainly if max_page = 0 it won't show any results because this specifies that the maximum results per page is zero. However I'm not able to reproduce the bug you reported where the response count is 1 but no results are served. I created a test here which demonstrates that your block will be called with limit=1 and offset=0.

Also note that I created a pull request (https://github.com/begriffs/clean_pagination/pull/11) to make it so you don't have to make a special case for count=0.

Can you please review my test to see if I understood your bug correctly?