where: Support String arguments
#470
Open
+107
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem
When a resource queries an "index"-like endpoint and receives a response
payload that relies on URL-based pagination, it can be tedious to
deconstruct the next page's URL and transform it into a subsequent
.wherecall.For example, consider a
Postpayload like:{ "posts": { … }, "next_page": "https://api.blog.com/posts?page=2" }In order to query the URL in the
next_pageproperty, the collectionparser must extract a
{ "page" => "2" }Hash and forward it to aresource_class.wherecall:The process becomes complicated when there are other parameters
(including "array"-like keys):
{ "posts": { … }, "next_page": "https://api.blog.com/posts?tags[]=Ruby&tags[]=Rails&page=2" }In this scenario, the Array created by URI.decode_www_form will only
retain both
tags[]-keyed values, but the Array#to_h call willflatten that Array into a Hash that only contains the last key. In this
case,
tags[]=Rubywill be omitted, and the resulting Hash would be{ "tags[]" => "Rails", "page" => "2" }.The proposal
Active Record's
.wheremethod supports both String and Arrayarguments. In the context of Active Record,
StringandArrayarguments are in support of the underlying SQL queries to be executed.
In the context of Active Resource, the underlying format for a "query"
is an HTTP-compliant query that's encoded as an
application/x-www-form-urlencoded string.
This commit proposes adding support to
Base.whereto accept Stringarguments.
This support would simplify the scenario above:
When Active Resource is loaded alongside a Rails or Rack application,
rely on Rack::Utils.parse_nested_query to decode key-value pairs.
Otherwise, rely on URI.decode_www_form and Array#to_h.