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

Support array query params #234

Merged
merged 2 commits into from
Jan 1, 2025
Merged

Conversation

jbennett
Copy link
Contributor

URI.decode_www_form doesn’t support arrays but Rack::Utils.parse_query does. I'm passing a list of IDs to exclude which currently is reduced to only the final value:

<%= form.combobox :membership_id, members_path(exclude: [1,2]) %>
<%# src => /members?exclude[]=3&…  not /members?exclude[]=1&exclude=2&… %>

This also addresses issue #211

`URI.decode_www_form` doesn’t support arrays but `Rack::Utils.parse_query` does
@josefarias
Copy link
Owner

Thanks! Taking a look tomorrow

Copy link
Owner

@josefarias josefarias left a comment

Choose a reason for hiding this comment

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

Thanks a bunch @jbennett!

I think we should depend on the Rack::Utils.parse_nested_query + Rack::Utils.build_nested_query combo. Left some comments to that extent. Let me know what you think!

@@ -115,7 +115,7 @@ def hw_first_page?

def hw_uri_with_params(url_or_path, **params)
URI.parse(url_or_path).tap do |url_or_path|
query = URI.decode_www_form(url_or_path.query || "").to_h.merge(params.compact_blank.stringify_keys)
query = Rack::Utils.parse_query(url_or_path.query || "").merge(params.compact_blank.stringify_keys)
Copy link
Owner

Choose a reason for hiding this comment

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

I didn't know Rack exposed this util. Nice!

Glancing at Rack's source code, it seems we should use parse_nested_query instead. Any reason to prefer parse_query over that?

Also, I noticed Rack exposes build_nested_query as well. What do you think about using that instead of URI.encode_www_form on the next line? Probably a good idea to use the pair together as they'll likely evolve together as well inside Rack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, makes a lot of sense to use that same setoff parse/build and switched to the nested version.


html = hw_paginated_combobox_options [], next_page: 2

assert_attrs html, tag_name: "turbo-frame", src: "/foo?ary=1&ary=2&page=2&format=turbo_stream"
Copy link
Owner

Choose a reason for hiding this comment

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

As far as I know, Rails prefers the ary[]=1&ary[]=2... convention for array/nested params. I believe we get that for free with Rack's build_nested_query. Are you depending on the ary=1&ary=2 convention instead?

I think to get a passing test using that notation we'd need to unescape the url encoding in the result. Something like:

assert_attrs CGI.unescape(html), tag_name: "turbo-frame", src: "/foo?ary[]=1&ary[]=2&page=2&format=turbo_stream"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

switched the test to include []

@josefarias josefarias merged commit c280da0 into josefarias:main Jan 1, 2025
2 checks passed
@josefarias
Copy link
Owner

Excellent! Thanks @jbennett. Going out in the next release.

@josefarias josefarias added the bug Something isn't working label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants