updated the page method, added args to AR page method #276

Closed
wants to merge 5 commits into
from

Conversation

Projects
None yet
4 participants
@rocknruby

In my fork its possible to set a default_page as a argument for the page method. If param[:page] is invalid, the page method would return the default_page and is not raising an exception, like it is now for the issue #271

params[:page] = 'foobar'
posts = Post.where(:published => true).page(params[:page])
#posts.current_page = 1

# set default_page if necessary 
posts = Post.where(:published => true).page(params[:page], :default_page => 10)
#posts.current_page = 10
# this raises no exception, it trys to return the default_page

with best regards

Hannes Maack added some commits Nov 5, 2012

Hannes Maack
reverted page_number.rb to original, added args to page method for ac…
…tive-record, now its possible to add default_page param e.g. page('foo', :default_page => 2) to get no exception in case of invalid pagenum ("foo") and to set a default page if its not page one
@miloops

This comment has been minimized.

Show comment Hide comment
@miloops

miloops Jan 9, 2013

Any chance to get this fixed? Users keep changing URL and we get lots of errors because of this... cc @mislav

miloops commented Jan 9, 2013

Any chance to get this fixed? Users keep changing URL and we get lots of errors because of this... cc @mislav

@mislav

This comment has been minimized.

Show comment Hide comment
@mislav

mislav Jan 10, 2013

Owner

Nope, I don't find the feature useful. The exception is deliberate so you can handle it in your application.

posts = Post.where(:published => true)
begin
  posts = posts.page(params[:page])
rescue ArgumentError
  posts = posts.page(default_page)
end

Or you could massage params[:page] before you pass it in will_paginate.

Owner

mislav commented Jan 10, 2013

Nope, I don't find the feature useful. The exception is deliberate so you can handle it in your application.

posts = Post.where(:published => true)
begin
  posts = posts.page(params[:page])
rescue ArgumentError
  posts = posts.page(default_page)
end

Or you could massage params[:page] before you pass it in will_paginate.

@mislav mislav closed this Jan 10, 2013

@miloops

This comment has been minimized.

Show comment Hide comment
@miloops

miloops Jan 10, 2013

In every action in your application that uses pagination? That's not very practical.

miloops commented Jan 10, 2013

In every action in your application that uses pagination? That's not very practical.

@dgilperez

This comment has been minimized.

Show comment Hide comment
@dgilperez

dgilperez Feb 11, 2014

@mislav I think I could handle that exception in our apps if it would be different from the too common ArgumentError. What about raising a custom exception?

@mislav I think I could handle that exception in our apps if it would be different from the too common ArgumentError. What about raising a custom exception?

@mislav

This comment has been minimized.

Show comment Hide comment
@mislav

mislav Feb 11, 2014

Owner

@dgilperez You could be handling WillPaginate::InvalidPage instead of ArgumentError.

Owner

mislav commented Feb 11, 2014

@dgilperez You could be handling WillPaginate::InvalidPage instead of ArgumentError.

@dgilperez

This comment has been minimized.

Show comment Hide comment
@dgilperez

dgilperez Feb 11, 2014

@mislav hmm that's exactly what I'd rather do, but the exceptions in my controllers look like ArgumentError: invalid value for Integer(): "vdpcaglxx" (sorry, I just realized I should have commented in #271, not here)

@mislav hmm that's exactly what I'd rather do, but the exceptions in my controllers look like ArgumentError: invalid value for Integer(): "vdpcaglxx" (sorry, I just realized I should have commented in #271, not here)

@dgilperez

This comment has been minimized.

Show comment Hide comment
@dgilperez

dgilperez Feb 11, 2014

@mislav sorry for being too fast on that. Peeping on the code I see InvalidPage as a module that tags that exception ... not used to that pattern, but I it works. Thanks!

@mislav sorry for being too fast on that. Peeping on the code I see InvalidPage as a module that tags that exception ... not used to that pattern, but I it works. Thanks!

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