diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index 6d93db11aed629af9a1320677b8612203da6ef3f..3d8af3939453fbbacdf4f9e4a4ae4618efc1fd8e 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- require 'geocoder/sql' require 'geocoder/stores/base' @@ -93,13 +94,17 @@ module Geocoder::Store # between the given point and each found nearby point; # set to false for no bearing calculation. Use # Geocoder.configure[:distances] to configure default calculation method. - # * +:select+ - string with the SELECT SQL fragment (e.g. “id, nameâ€) - # * +: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) + # * +:select+ - string with the SELECT SQL fragment (e.g. “id, nameâ€) + # * +:select_distance+ - whether to include the distance alias in the + # SELECT SQL fragment (e.g. <formula> AS distance) + # * +: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) # def near_scope_options(latitude, longitude, radius = 20, options = {}) options[:units] ||= (geocoder_options[:units] || Geocoder.config.units) + select_distance = options.fetch(:select_distance, true) + options[:order] = "" if !select_distance && !options.include?(:order) bearing = bearing_sql(latitude, longitude, options) distance = distance_sql(latitude, longitude, options) @@ -116,7 +121,9 @@ module Geocoder::Store conditions = [bounding_box_conditions + " AND #{distance} <= ?", radius] end { - :select => select_clause(options[:select], distance, bearing), + :select => select_clause(options[:select], + select_distance ? distance : nil, + bearing), :conditions => add_exclude_condition(conditions, options[:exclude]), :order => options.include?(:order) ? options[:order] : "distance ASC" } @@ -166,9 +173,13 @@ module Geocoder::Store elsif columns == :geo_only clause = "" else - clause = (columns || full_column_name("*")) + ", " + clause = (columns || full_column_name("*")) end - clause + "#{distance} AS distance" + + if distance + clause += ", " unless clause.empty? + clause += "#{distance} AS distance" + end + clause + (bearing ? ", #{bearing} AS bearing" : "") end @@ -241,4 +252,3 @@ module Geocoder::Store alias_method :fetch_address, :reverse_geocode end end - diff --git a/test/near_test.rb b/test/near_test.rb index 359f2011423d0b38d3b83cbbb10eb2bfeed49abe..9bd6a88ca6cf800eef5e486c6f081791cac0b4ee 100644 --- a/test/near_test.rb +++ b/test/near_test.rb @@ -8,4 +8,28 @@ class NearTest < Test::Unit::TestCase assert_match /test_table_name.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND test_table_name.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /, result[:conditions][0] end + + def test_near_scope_options_with_defaults + result = Event.send(:near_scope_options, 1.0, 2.0, 5) + + assert_match /AS distance/, result[:select] + assert_match /AS bearing/, result[:select] + assert_no_consecutive_comma(result[:select]) + end + + def test_near_scope_options_with_no_distance + result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_distance => false) + + assert_no_match /AS distance/, result[:select] + assert_match /AS bearing/, result[:select] + assert_no_match /distance/, result[:condition] + assert_no_match /distance/, result[:order] + assert_no_consecutive_comma(result[:select]) + end + + private + + def assert_no_consecutive_comma(string) + assert_no_match /, *,/, string, "two consecutive commas" + end end