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

Issue#669 bad page number parameters causing ArgumentError #1144

Merged
merged 6 commits into from
Jun 10, 2017

Conversation

hisayohorie
Copy link
Contributor

@hisayohorie hisayohorie commented Apr 19, 2017

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

Copy link
Member

@henare henare left a 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 henare assigned hisayohorie and unassigned henare May 3, 2017
@hisayohorie
Copy link
Contributor Author

@henare sure! I will look into it!

@hisayohorie
Copy link
Contributor Author

@henare this is the discussion around the issue, and seems the best practice isn't solidified?
mislav/will_paginate#271

@henare
Copy link
Member

henare commented May 4, 2017

@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 :shipit:

…am method which is responding to the current bug in will_paginate gem
@henare
Copy link
Member

henare commented May 5, 2017

@hisayohorie I noticed you updated this so I thought I'd give it a more thorough check.

Once that's done let's merge and :shipit:

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
Copy link
Member

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
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do!

@henare
Copy link
Member

henare commented May 5, 2017

@hisayohorie Once you're ready for me to take another look please assign back to me 🤝

Copy link
Member

@henare henare left a 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

Copy link
Member

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.

@henare henare merged commit a6451ed into openaustralia:master Jun 10, 2017
@henare
Copy link
Member

henare commented Jun 10, 2017

Deployed :shipit:

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.

2 participants