Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix #570: make per(nil) respect max_per_page#800

Closed
jonathanhefner wants to merge 2 commits intokaminari:masterfrom
jonathanhefner:fix-570
Closed

Fix #570: make per(nil) respect max_per_page#800
jonathanhefner wants to merge 2 commits intokaminari:masterfrom
jonathanhefner:fix-570

Conversation

@jonathanhefner
Copy link
Contributor

This makes per always respect max_per_page, fixing #570. The issue actually applies to any invalid argument, e.g. per("bad"). Now, when an invalid argument is given, per uses default_per_page in its place.

This makes `per` always respect `max_per_page`, fixing kaminari#570.  The issue
actually applies to any invalid argument, e.g. `per("bad")`. Now, when
an invalid argument is given, `per` uses `default_per_page` in its
place.
limit(n)
elsif max_per_page && max_per_page < n
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
if num == 0 || num == '0'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is more efficient, but does not handle unlikely corner cases. For example " 0 " and "-0" will fall through to the else. (Note: "-0" was not handled in the previous version, either.)

I can change this, if we want to be extremely thorough. Thoughts?

This makes `per` handle abnormal expressions of zero, such as "000" and
"-0".  Negative numbers and non-numeric inputs are still treated as if
they were nil.
@jonathanhefner
Copy link
Contributor Author

As mentioned in my comment, I modified per to handle abnormal zero strings (e.g. " 0 ", "-0", and "000").

When given bad input, Integer() is much slower (~30x in a micro-benchmark) than String#to_i because it raises an exception. However, if we can assume that most calls to per will pass a numeric, numeric string, or nil argument, it should be fine.

@jonathanhefner
Copy link
Contributor Author

@yuki24 Are there any changes that should be made? Any additional tests you would like me to add?

@yuki24 yuki24 added this to the 1.0.0 milestone Oct 11, 2016
@yuki24 yuki24 self-assigned this Oct 11, 2016
@yuki24
Copy link
Member

yuki24 commented Nov 17, 2016

manually merged: 405842c, 6d24110. Thank you for your contribution!

@yuki24 yuki24 closed this Nov 17, 2016
@jonathanhefner
Copy link
Contributor Author

Happy to contribute! Thank you! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants