Skip to content
Snippets Groups Projects
Commit 815681d9 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #465 from seanski/master

Allow custom naming of distance and bearing columns.
parents 2c0d0e78 d0a90f23
No related branches found
No related tags found
No related merge requests found
...@@ -105,6 +105,8 @@ module Geocoder::Store ...@@ -105,6 +105,8 @@ module Geocoder::Store
# * +:order+ - column(s) for ORDER BY SQL clause; default is distance; # * +:order+ - column(s) for ORDER BY SQL clause; default is distance;
# set to false or nil to omit the ORDER BY clause # set to false or nil to omit the ORDER BY clause
# * +:exclude+ - an object to exclude (used by the +nearbys+ method) # * +: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 = {}) def near_scope_options(latitude, longitude, radius = 20, options = {})
if options[:units] if options[:units]
...@@ -116,6 +118,8 @@ module Geocoder::Store ...@@ -116,6 +118,8 @@ module Geocoder::Store
select_bearing = options.fetch(:select_bearing, true) select_bearing = options.fetch(:select_bearing, true)
bearing = bearing_sql(latitude, longitude, options) bearing = bearing_sql(latitude, longitude, options)
distance = distance_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) b = Geocoder::Calculations.bounding_box([latitude, longitude], radius, options)
args = b + [ args = b + [
...@@ -132,9 +136,11 @@ module Geocoder::Store ...@@ -132,9 +136,11 @@ module Geocoder::Store
{ {
:select => select_clause(options[:select], :select => select_clause(options[:select],
select_distance ? distance : nil, select_distance ? distance : nil,
select_bearing ? bearing : nil), select_bearing ? bearing : nil,
distance_column,
bearing_column),
:conditions => add_exclude_condition(conditions, options[:exclude]), :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 end
...@@ -176,7 +182,7 @@ module Geocoder::Store ...@@ -176,7 +182,7 @@ module Geocoder::Store
## ##
# Generate the SELECT clause. # Generate the SELECT clause.
# #
def select_clause(columns, distance = nil, bearing = nil) def select_clause(columns, distance = nil, bearing = nil, distance_column = 'distance', bearing_column = 'bearing')
if columns == :id_only if columns == :id_only
return full_column_name(primary_key) return full_column_name(primary_key)
elsif columns == :geo_only elsif columns == :geo_only
...@@ -186,11 +192,11 @@ module Geocoder::Store ...@@ -186,11 +192,11 @@ module Geocoder::Store
end end
if distance if distance
clause += ", " unless clause.empty? clause += ", " unless clause.empty?
clause += "#{distance} AS distance" clause += "#{distance} AS #{distance_column}"
end end
if bearing if bearing
clause += ", " unless clause.empty? clause += ", " unless clause.empty?
clause += "#{bearing} AS bearing" clause += "#{bearing} AS #{bearing_column}"
end end
clause clause
end end
......
...@@ -35,6 +35,24 @@ class NearTest < Test::Unit::TestCase ...@@ -35,6 +35,24 @@ class NearTest < Test::Unit::TestCase
assert_no_consecutive_comma(result[:select]) assert_no_consecutive_comma(result[:select])
end end
def test_near_scope_options_with_custom_distance_column
result = Event.send(:near_scope_options, 1.0, 2.0, 5, :distance_column => 'calculated_distance')
assert_no_match /AS distance/, result[:select]
assert_match /AS calculated_distance/, result[:select]
assert_no_match /\bdistance\b/, result[:order]
assert_match /calculated_distance/, result[:order]
assert_no_consecutive_comma(result[:select])
end
def test_near_scope_options_with_custom_bearing_column
result = Event.send(:near_scope_options, 1.0, 2.0, 5, :bearing_column => 'calculated_bearing')
assert_no_match /AS bearing/, result[:select]
assert_match /AS calculated_bearing/, result[:select]
assert_no_consecutive_comma(result[:select])
end
private private
def assert_no_consecutive_comma(string) def assert_no_consecutive_comma(string)
......
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