-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Issue#669 bad page number parameters causing ArgumentError #1144
Conversation
…page number parameters and wrote a test on applications_controller_spec.rb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very interesting approach @hisayohorie and does seem to do the job. However since the error is bubbling up from the will_paginate gem I wonder if it's not something that should be fixed in the Gem itself?
I'm thinking that surely we're not the only ones affected by this error too. Can you please have a look and see if that's right and see how other people are fixing this? I'd check the Gem's issues page first 👍
@henare sure! I will look into it! |
@henare this is the discussion around the issue, and seems the best practice isn't solidified? |
@hisayohorie nice one on tracking down that discussion. That makes me 👍 this solution then. I think we should add a comment above that method so people know why we've added it. You could even include a link to that discussion. Presumably when it's resolved upstream we'll be able to remove this workaround ✨ Once that's done let's merge and |
…am method which is responding to the current bug in will_paginate gem
@hisayohorie I noticed you updated this so I thought I'd give it a more thorough check.
OK I got a bit ahead of myself 😄 I'll add a couple of comments 💬 |
# this method is to respond to the will_paginate bug of invalid page number leading to error being thrown. | ||
# see discussion here https://github.com/mislav/will_paginate/issues/271 | ||
def validate_page_param | ||
unless params[:page].present? && params[:page].to_i > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use unless with else. Rewrite these with the positive case first.
I also notice your indentation is a little out on the params lines - you need an extra space.
@@ -15,6 +15,21 @@ | |||
get :index | |||
expect(assigns[:rss]).to be_nil | |||
end | |||
|
|||
it "should try to convert page param to an integer" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my first review I didn't check where these tests were placed. They're under #index rss feed
and since this isn't related to the RSS feed these tests should not be here.
But where should they go? I didn't know so I hunted around. I discovered that ApplicationController
behaviour that does things like global error checking should be tested by defining an anonymous controller.
So create a new file in this directory called application_controller_spec.rb
, with something like this (and removing the tests from this file):
require "spec_helper"
describe ApplicationController do
controller do
def index
render text: nil
end
end
describe "#validate_page_param" do
it "should try to convert page param to an integer" do
get :index, page: "2%5B&q"
expect(controller.params[:page]).to eq(2)
end
it "should default to page nil when no page number param is given" do
get :index, page: "%5B&q"
expect(controller.params[:page]).to eq(nil)
end
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do!
@hisayohorie Once you're ready for me to take another look please assign back to me 🤝 |
…and removed bad parametor test from applications_controller_spec.rb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ready to go 👍
@@ -15,6 +15,7 @@ | |||
get :index | |||
expect(assigns[:rss]).to be_nil | |||
end | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this stray extra line.
Deployed |
Fixes #669.
I wrote 2 test for this method because
when the string passed starts with integer, to_i returns the same integer (ex. "2K%&)".to_i = 2)
and other time it returns 0 (ex. "%&43{".to_i = 0)
and I'm not super sure this is the appropriate way to do it? please let me know!
@equivalentideas @henare