Add ability to set fields in configuration for Google Places#1572
Add ability to set fields in configuration for Google Places#1572czlee wants to merge 4 commits intoalexreisner:masterfrom
Conversation
|
Thanks for this. And apologies to @ericds for not responding to #1514 (I simply missed it). I do prefer this no-defaults approach for exactly the reasons you name. Would you mind adding a test or two, to make sure the fields get formatted properly in the query string? Also, what do you think about allowing the config to be overridden by using |
For sure! Will do (in the next week or so).
Yeah, I kind of agree that it's more intuitive behaviour. I think I shied away from it because it would have (at face) changed the existing behaviour of def fields(query)
query_fields = query.options[:fields]
return format_fields(query_fields) if query_fields
default_fields
endSo if you pass in it will fall back to the def fields(query)
if query.options.has_key?(:fields)
return format_fields(query.options[:fields])
end
if configuration.has_key?(:fields)
return format_fields(configuration[:fields])
end
default_fields
endThen the same The only way to fall back to It's a very subtle change in behaviour, but I know that I sometimes use This wouldn't matter for So... yeah, basically just a small backwards compatibility point for |
|
Yep, good point! Let's make this a pull request for the v1.8 branch (1.8.0 will probably be the next release) and I'll mention it in the release notes. |
|
Dumb question, but how do I run the tests? I'm used to rspec and don't really have experience with the standard library unit test framework, or anything that needs to be done to provide a test environment for gems 😅 Sorry if I missed documentation for this, I promise I looked! Currently I get, from the directory but I'm just running it in my app dev environment, which presumably isn't what the Geocoder test environment needs. (That's why I converted the PR to a draft: I haven't actually run the tests yet to see if they pass.) |
617097a to
80341f8
Compare
(previous behavior was to ignore if it was falsey)
80341f8 to
c019697
Compare
| def fields(query) | ||
| if query.options.has_key?(:fields) | ||
| return format_fields(query.options[:fields]) | ||
| end | ||
|
|
||
| if configuration.has_key?(:fields) | ||
| return format_fields(configuration[:fields]) | ||
| end | ||
|
|
||
| nil # use Google Places defaults | ||
| end | ||
|
|
||
| def format_fields(*fields) | ||
| flattened = fields.flatten.compact | ||
| return if flattened.empty? | ||
|
|
||
| flattened.join(',') | ||
| end | ||
|
|
There was a problem hiding this comment.
This repeats code in GooglePlacesSearch; let me know if you think these methods should be abstracted out to a base class GooglePlaces < Google. I felt it was arguable whether this was enough to justify it.
There was a problem hiding this comment.
Thanks for pointing this out. I agree that it's not enough to merit the complexity of a new base class.
|
The tests were a good idea, turns out the above-discussed modification actually caused it to fail to omit the |
|
Excellent! Tests are great for that. This looks good to me. Will merge shortly. Thanks for the good work on this. |
|
Closing this manually (was merged into the v1.8 branch, which Github isn't auto-marking as "merged"). |
Thanks so much for your work on this gem, @alexreisner!
This PR adds support for:
fieldsparameter to:google_places_detailsfieldsinGeocoder.configure, for both:google_places_detailsand:google_places_search(I know this is two things, but they're very tightly related hard-to-separate things!)
Limitation: If the Geocoder configuration has
fieldsconfigured, you can't override it in the query withnil. You can override it with[], but this would add&fields=&to the query string (which does the same thing, but would look different to the cache). Open to thoughts on whether this should be modified.Note on relationship with #1514 (@ericds): In principle they have a similar objective, and this PR includes half of #1514. However, #1514 also specifies a collection of default fields in
google_places_details.rb. Instead, in this PR I propose falling back tonil, which will send a request to Google Places without thefields=key, which (at least when I do it) seems to just default to all of the keys available. Reasons I felt this was preferable:fieldsisn't specified, this will do the same thing as currently)That said, I'd be down to discuss this with @ericds to arrive at an agreement on how this should work, for @alexreisner to consider.