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

Coerce pagination string params #110

Closed

Conversation

ryansch
Copy link
Contributor

@ryansch ryansch commented May 8, 2014

We hit this in our app when we pass in params from a rails controller. This PR causes the limit and offset methods to work like the page (or ::Kaminari.config.page_method_name) method already does.

@morganick
Copy link

👍

@@ -69,7 +69,7 @@ def limit(value)
@results = nil
@records = nil
@response = nil
@per_page = value
@per_page = value.to_i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may introduce some confusion as any value other than a string representation of a number is likely going to return 0 by calling the #to_i:

"35".to_i   # good
> 35     
"ab".to_i  # not good
> 0

This will cause the next few lines to succeed, with a size of 0 and a per_page of 0, and likely add confusion. I think it should fail loudly. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of a way to get ruby to throw up when converting a String to a Fixnum if the conversion doesn't make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever we decide here needs to also be applied to the #page method above in the same file (it already uses #to_i.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if instead of just coercing we validated it as well?

@per_page.to_i must be >= 1

If it is < 1, then it has no affect on the query and neither does the page argument. Thus returning all results. The page number is only valid when there is a proper per_page limit.

When I put in a page number that doesn't make sense as a possible value say -1 or boom kaminari just gives me page 1. So following that I would really want it to be ignored and fall back to a default value. Default value for @page is easy. Picking a default value for @per_page would be arbitrary.

Also, I wouldn't want to throw an exception because someone decided to put ?per_page=boom in their address bar; I get enough email as it is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fail loudly:

@per_page = Integer(value)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronRustad That's entirely too obvious. 😏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like @morganick's idea of testing for >=1 since that also deals with negative integers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronRustad @morganick I've added code/tests for invalid string params.

@ryansch ryansch force-pushed the coerce-pagination-string-params branch from dcda5a1 to 50920d6 Compare November 11, 2014 04:42
@ryansch
Copy link
Contributor Author

ryansch commented Nov 11, 2014

Coming back to ES after some time on other projects. This PR still needs a merge. I've rebased the topic branch and pushed it.

@ryansch
Copy link
Contributor Author

ryansch commented May 4, 2015

I've signed the CLA.

@ryansch ryansch force-pushed the coerce-pagination-string-params branch from 50920d6 to 68c8dea Compare May 4, 2015 22:41
@karmi karmi closed this in 6b81a77 May 20, 2015
@karmi
Copy link
Contributor

karmi commented May 20, 2015

@ryansch Finally got to merge this, sorry for the ridiculous delay!

@ryansch ryansch deleted the coerce-pagination-string-params branch May 20, 2015 17:03
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.

4 participants