Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use value#to_i instead of Kernel#Integer() to coerce page number into integer. #329

Closed
wants to merge 1 commit into from

2 participants

@botandrose

Integer() is pickier than #to_i. Integer("5/") throws an exception, while "5/".to_i returns 5.

@botandrose botandrose Use value#to_i instead of Kernel#Integer() to coerce page number into…
… integer.

Integer() is pickier than #to_i. Integer("5/") throws an exception, while "5/".to_i returns 5.
63068b8
@mislav
Owner

The exception is deliberate. However, future versions of will_paginate might configure Rails to handle the exception better. For now, you should handle it yourself. See #271

@mislav mislav closed this
@botandrose

Roger that. Just out of curiosity, what is the value of throwing an exception instead of performing a coercion? Thanks for will_paginate!

@mislav
Owner

Exceptions are almost always a better idea than silent coercion between types. The value "37signals" is not the same as the number 37.

@botandrose

In general, I agree! Years of PHP and JavaScript masochism has definitely drilled that notion into my skull. :) But personally, I think this is one of those rare times where coercion is the desirable option, and its an explicit coercion at that! We're looking for an integer here, and only an integer, so there's no chance of something getting coerced into the wrong format. Worst-case scenario, someone passes us garbage and we turn it into a 0. From a client code perspective, I'd rather deal with a page number of 0 than a page number of "asdf" + catching an exception. Just my $0.02, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2013
  1. @botandrose

    Use value#to_i instead of Kernel#Integer() to coerce page number into…

    botandrose authored
    … integer.
    
    Integer() is pickier than #to_i. Integer("5/") throws an exception, while "5/".to_i returns 5.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/will_paginate/page_number.rb
View
2  lib/will_paginate/page_number.rb
@@ -13,7 +13,7 @@ class PageNumber < DelegateClass(Integer)
extend Forwardable
def initialize(value, name)
- value = Integer(value)
+ value = value.to_i
if 'offset' == name ? (value < 0 or value > BIGINT) : value < 1
raise RangeError, "invalid #{name}: #{value.inspect}"
end
Something went wrong with that request. Please try again.