diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index 9382aaa62338e1b40ae09f85e7c3c18c0fcb8b27..7fc09cc1e07d45c65ab74250a95eae456f889763 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -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 = 'distance', bearing_column = 'bearing') 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 diff --git a/test/near_test.rb b/test/near_test.rb index 05411dd555f95b4e5e20f06cfcd86363fb3ddd95..4e95b9225002f862d2f65e03b69fe8ef56284004 100644 --- a/test/near_test.rb +++ b/test/near_test.rb @@ -35,6 +35,24 @@ class NearTest < Test::Unit::TestCase assert_no_consecutive_comma(result[:select]) 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 def assert_no_consecutive_comma(string)