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

Documentation: Check the consistency with page number assignation change in 9+ #739

Open
7 tasks done
phyzical opened this issue Sep 17, 2024 · 5 comments
Open
7 tasks done

Comments

@phyzical
Copy link

phyzical commented Sep 17, 2024

👀 Before submitting...

  • I upgraded to pagy version 9.0.9
  • I searched through the Documentation
  • I searched through the Issues
  • I searched through the Q&A

🧐 REQUIREMENTS

  • I am providing a VALID code file that confirms the bug
  • I am NOT posting any of the USELESS THINGS listed above
  • I am aware that this issue will be automatically closed if the code file is missing or INVALID

💬 Description

Not sure if this is a bug but in 9 we had a set of tests fail that assumed that invalid page numbers result in the first page to be returned i.e providing
page: -1
page: 'blah'

in 8 would result in the first page of results
in 9 we get

Pagy::VariableError:
       expected :page >= 1; got -1
Pagy::VariableError:
       expected :page >= 1; got 0

TBH, i think its just a set of silly tests ourside but i could see it biting larger projects where people do dodge to reset pages ect.

Might just be worth getting added to the breaking changes list?

@phyzical phyzical added the bug label Sep 17, 2024
@phyzical
Copy link
Author

phyzical commented Sep 17, 2024

incase others need this and this is closed as 'not fixing' i monkey patched it by

@vars[name] = 1 if name == :page && (@vars[name].instance_of?(String) || (@vars[name]&.respond_to?(:to_i) && @vars[name].to_i <= 0))
class Pagy
  module SharedMethods
 def assign_and_check(name_min)
      name_min.each do |name, min|
        @vars[name] = 1 if name == :page && (@vars[name].instance_of?(String) || (@vars[name]&.respond_to?(:to_i) && @vars[name].to_i <= 0))
        raise VariableError.new(self, name, ">= #{min}", @vars[name]) \
        unless @vars[name]&.respond_to?(:to_i) && \
               instance_variable_set(:"@#{name}", @vars[name].to_i) >= min
      end
    end
  end
end

@ddnexus
Copy link
Owner

ddnexus commented Sep 17, 2024

@phyzical thank you for your report.

I am wondering how you don't get the automatic assignation of the page variable, through the pagy_get_page method.

Could you please use one of the Playground apps to show how to make it fail?

@phyzical
Copy link
Author

I assume then it is just simply the fact its a redundant spec causing the issue to begin with, as its explicitly passing in the param.

Based on what your saying its probably safe to just close this. Just figured id raise it as it does seem to be a functionality change not mentioned in the breaking changes.

context 'when the client passes a nonsense page param' do
    it 'renders the first page instead of falling over' do
      login_as_user users(:global_admin)
      get users_path(page: -1)
      expect(response).to have_http_status(:ok)
      expect(response.body).to include('Displaying Users 1-10')

      get users_path(page: 'nonsense')
      expect(response).to have_http_status(:ok)
      expect(response.body).to include('Displaying Users 1-10')

      get users_path(page: '')
      expect(response).to have_http_status(:ok)
      expect(response.body).to include('Displaying Users 1-10')
    end
  end

@phyzical
Copy link
Author

and why it happens
/lib/pagy/backend.rb

vars[:page] ||= pagy_get_page(vars) its just early exiting as its explicitly provided

@ddnexus
Copy link
Owner

ddnexus commented Sep 17, 2024

That explains it. Indeed, if you explicitly pass a variable, you are responsible for passing it right.

Just figured id raise it as it does seem to be a functionality change not mentioned in the breaking changes.

I will check whether the docs and breaking changes are consistent with the current behavior before closing it.

@ddnexus ddnexus added documentation and removed bug labels Sep 19, 2024
@ddnexus ddnexus changed the title Bug: Invalid page number change in 9+ Documentation: Check the consistency with page number assignation change in 9+ Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants