Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mpz opened this issue Mar 9, 2015 · 4 comments
Open

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

mpz opened this issue Mar 9, 2015 · 4 comments

Comments

@mpz
Copy link

mpz commented Mar 9, 2015

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
Copy link
Owner

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. 😄 )

@begriffs
Copy link
Owner

begriffs commented Jun 4, 2015

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

@mpz
Copy link
Author

mpz commented Jun 4, 2015

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 added a commit that referenced this issue Jun 7, 2015
@begriffs
Copy link
Owner

begriffs commented Jun 7, 2015

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 (#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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants