Skip to content
Snippets Groups Projects
Commit 737801a3 authored by Sean Hill's avatar Sean Hill
Browse files

Allow for setting a custom distance and bearing column.

parent 95ad41db
No related branches found
No related tags found
No related merge requests found
......@@ -105,6 +105,8 @@ module Geocoder::Store
# * +:order+ - column(s) for ORDER BY SQL clause; default is distance;
# set to false or nil to omit the ORDER BY clause
# * +:exclude+ - an object to exclude (used by the +nearbys+ method)
# * +:distance_column+ - used to set the column name of the calculated distance.
# * +:bearing_column+ - used to set the column name of the calculated bearing.
#
def near_scope_options(latitude, longitude, radius = 20, options = {})
if options[:units]
......@@ -116,6 +118,8 @@ module Geocoder::Store
select_bearing = options.fetch(:select_bearing, true)
bearing = bearing_sql(latitude, longitude, options)
distance = distance_sql(latitude, longitude, options)
distance_column = options.fetch(:distance_column, 'distance')
bearing_column = options.fetch(:bearing_column, 'bearing')
b = Geocoder::Calculations.bounding_box([latitude, longitude], radius, options)
args = b + [
......@@ -132,9 +136,11 @@ module Geocoder::Store
{
:select => select_clause(options[:select],
select_distance ? distance : nil,
select_bearing ? bearing : nil),
select_bearing ? bearing : nil,
distance_column,
bearing_column),
:conditions => add_exclude_condition(conditions, options[:exclude]),
:order => options.include?(:order) ? options[:order] : "distance ASC"
:order => options.include?(:order) ? options[:order] : "#{distance_column} ASC"
}
end
......@@ -176,7 +182,7 @@ module Geocoder::Store
##
# Generate the SELECT clause.
#
def select_clause(columns, distance = nil, bearing = nil)
def select_clause(columns, distance = nil, bearing = nil, distance_column = nil, bearing_column = nil)
if columns == :id_only
return full_column_name(primary_key)
elsif columns == :geo_only
......@@ -186,11 +192,11 @@ module Geocoder::Store
end
if distance
clause += ", " unless clause.empty?
clause += "#{distance} AS distance"
clause += "#{distance} AS #{distance_column}"
end
if bearing
clause += ", " unless clause.empty?
clause += "#{bearing} AS bearing"
clause += "#{bearing} AS #{bearing_column}"
end
clause
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment