Skip to content

always call jsonapi_page_size method on pagination enabled requests#40

Merged
stas merged 4 commits intostas:masterfrom
easyPEP:feature/issue-37-always-call-jsonapi-page-size
Feb 16, 2021
Merged

always call jsonapi_page_size method on pagination enabled requests#40
stas merged 4 commits intostas:masterfrom
easyPEP:feature/issue-37-always-call-jsonapi-page-size

Conversation

@fluxsaas
Copy link
Contributor

@fluxsaas fluxsaas commented Feb 15, 2021

What is the current behavior?

related to issue #37

if you set the params page[size] in the query, jsonapi_page_size is not called and page[size] is used to determine the page size of the paginated response.

What is the new behavior?

always call the module method jsonapi_page_size and pass the value of the query param page[size] to the method to define the page size of the paginated response. Fallback to JSONAPI_PAGE_SIZE if page[size] is lower than 1 or not present in the query string at all.

Checklist

Please make sure the following requirements are complete:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes /
    features)
  • All automated checks pass (CI/CD)

pass the per_page_param to the jsonapi_page_size method
@fluxsaas
Copy link
Contributor Author

@stas let me know if the implementation works for you, so i can add some docs and tests

@stas
Copy link
Owner

stas commented Feb 15, 2021

@fluxsaas this looks good. Two suggestions:

  • let's handle the string-to-int conversion as part of the jsonapi_page_size method and remove the to_f.to_i from where it is called
  • add a proper if/else block, or even better an early return instead of the ternary operator

Thank youuuu!!! 🙇

@fluxsaas
Copy link
Contributor Author

fluxsaas commented Feb 16, 2021

@stas great, i think it makes sense to pass in the pagination hash, maybe the extra infos will help.

We would also avoid passing a string OR nil value to the method.

def jsonapi_page_size(pagination_params)
  per_page = pagination_params[:size].to_f.to_i

  return self.class
          .const_get(:JSONAPI_PAGE_SIZE)
          .to_i if per_page < 1

  per_page
end

see files for the updated version

@stas
Copy link
Owner

stas commented Feb 16, 2021

@fluxsaas sounds good. Are we ready to merge this? 🙃

@fluxsaas
Copy link
Contributor Author

looks good from my side, i improved the Readme to reflect the changes.

@stas stas merged commit 7cd3698 into stas:master Feb 16, 2021
@stas
Copy link
Owner

stas commented Feb 16, 2021

Sweet! I'll prepare the release in a bit! Thank you @fluxsaas 🙇

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

Successfully merging this pull request may close these issues.

2 participants