From 87ee56238c66dca23382538423c261d899d341a2 Mon Sep 17 00:00:00 2001 From: Matthew Norton Date: Fri, 16 Dec 2022 09:18:57 -0500 Subject: [PATCH] Add default radius option as db column fallback --- lib/geocoder/stores/active_record.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index be7870c27..8e704541c 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -128,6 +128,7 @@ def near_scope_options(latitude, longitude, radius = 20, options = {}) distance = distance_sql(latitude, longitude, options) distance_column = options.fetch(:distance_column) { 'distance' } bearing_column = options.fetch(:bearing_column) { 'bearing' } + default_radius = options.fetch(:default_radius) # If radius is a DB column name, bounding box should include # all rows within the maximum radius appearing in that column. @@ -147,8 +148,13 @@ def near_scope_options(latitude, longitude, radius = 20, options = {}) # 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] + if default_radius.present? + c = "BETWEEN ? AND COALESCE(#{radius}, ?)" + a = [min_radius, default_radius] + else + c = "BETWEEN ? AND #{radius}" + a = [min_radius] + end else c = "BETWEEN ? AND ?" a = [min_radius, radius]