-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allows radius to be a symbol representing a column in DB #1107
Conversation
What is the status of this? |
@Dragsbaek I'm running this on production and so far it's serve me well. I'm willing to rebase this and discuss it further if others see the value. |
@leonelgalan thanks for this and sorry about the very long delay. A couple of things:
|
|
Sorry, I was really asking about why we're using
These are tricky things to solve, mainly because the code was already somewhat branched and ugly before the PR. Would really prefer to avoid a rewrite--I think we can modify the PR to clear things up. Here are my thoughts on doing that:
I propose something like this: # If radius is a DB column name, bounding box should include
# all rows within the maximum radius appearing in that column.
# Note: performance is dependent on variability of radii.
bb_radius = radius.is_a?(Symbol) ? maximum(radius) : radius
b = Geocoder::Calculations.bounding_box([latitude, longitude], bb_radius, options)
...
if using_unextended_sqlite?
conditions = bounding_box_conditions
else
min_radius = options.fetch(:min_radius, 0).to_f
# if radius is a DB column name,
# find rows between min_radius and value in column
if radius.is_a?(Symbol)
c = "BETWEEN ? AND #{radius}"
a = [min_radius]
else
c = "BETWEEN ? AND ?"
a = [min_radius, radius]
end
conditions = [bounding_box_conditions + " AND (#{distance}) " + c] + a
end Let me know your thoughts. And thanks again for this. |
@alexreisner, I think you already wrote it for me. I agree with your suggestions and with the proposed implementation. The only change I propose is the names of
What they hold is:
At the same time, they are quickly used, just like |
I agree that those names are not very descriptive. But I'd argue that what they actually hold is:
So I'd be up for using more descriptive names, but I think they should, um, describe things in more or less that way. I think the way they are is alright too, only because they get used so quickly. |
- Keeping branching condition consistent - Not overwrite the method argument: ‘radius’ - Not assigning two variables in a single line, and - Add code comments where needed By “proposed” I meant, I copy/paste them from his comment. But we are in agreement.
Tests for jruby are failing because the lack of bundler:
And mysql builds with:
Should I take a look at those? Do you think rebasing might bring newer configuration that fixes the issues? |
Ah, don't worry about those tests--they're unrelated. Going to merge this. Thanks! |
…#1107) * Allows radius to be a symbol representing a column in DB * Proposed changes by @alexreisner: - Keeping branching condition consistent - Not overwrite the method argument: ‘radius’ - Not assigning two variables in a single line, and - Add code comments where needed By “proposed” I meant, I copy/paste them from his comment. But we are in agreement. * Fixes broken test in PR
…#1107) * Allows radius to be a symbol representing a column in DB * Proposed changes by @alexreisner: - Keeping branching condition consistent - Not overwrite the method argument: ‘radius’ - Not assigning two variables in a single line, and - Add code comments where needed By “proposed” I meant, I copy/paste them from his comment. But we are in agreement. * Fixes broken test in PR
Related to #334 (Near search using column for radius) and #449 (Allow symbol to mean radius comes from a db column)
I decided to not change
.bounding_box
based on the reasons discussed on #449, but instead build the bounding box based on the maximum radius and simply "filter" the results based on the distance between the min_radius (defaults to 0.0) and whatever is stored in radius_column. The performance hit is dependent on how variable the radiuses stored in the DB are.Other concern discussed previously: Security. I haven't handled this one. But what I had in mind, was to compare the given
radius_column
to the model's column_names array:Place.column_names.column_names.include? radius_column.to_s
, but decided not to implement it until we discussed it further.I wasn't sure how to stub
.maximum
for ActiveRecord::Base. Don't hesitate correcting me on that one and pointing me in other directions for me to fix.Finally, I didn't like how repeated the code look on the
if
/else
and wrote this alternative implenetation instead, except it sacrificed readability. I decided to go with the original implementation instead: