Skip to content
Snippets Groups Projects
Commit 81ae5d4e authored by Nicolas Despres's avatar Nicolas Despres
Browse files

Add :select_bearing option for near scope.

Should close #374.
parent 4db0f534
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,7 @@ module Geocoder::Store ...@@ -97,6 +97,7 @@ module Geocoder::Store
# * +:select+ - string with the SELECT SQL fragment (e.g. “id, name”) # * +:select+ - string with the SELECT SQL fragment (e.g. “id, name”)
# * +:select_distance+ - whether to include the distance alias in the # * +:select_distance+ - whether to include the distance alias in the
# SELECT SQL fragment (e.g. <formula> AS distance) # SELECT SQL fragment (e.g. <formula> AS distance)
# * +:select_bearing+ - like +:select_distance+ but for bearing.
# * +: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)
...@@ -105,6 +106,7 @@ module Geocoder::Store ...@@ -105,6 +106,7 @@ module Geocoder::Store
options[:units] ||= (geocoder_options[:units] || Geocoder.config.units) options[:units] ||= (geocoder_options[:units] || Geocoder.config.units)
select_distance = options.fetch(:select_distance, true) select_distance = options.fetch(:select_distance, true)
options[:order] = "" if !select_distance && !options.include?(:order) options[:order] = "" if !select_distance && !options.include?(:order)
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)
...@@ -123,7 +125,7 @@ module Geocoder::Store ...@@ -123,7 +125,7 @@ module Geocoder::Store
{ {
:select => select_clause(options[:select], :select => select_clause(options[:select],
select_distance ? distance : nil, select_distance ? distance : nil,
bearing), select_bearing ? bearing : nil),
: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 ASC"
} }
...@@ -179,8 +181,11 @@ module Geocoder::Store ...@@ -179,8 +181,11 @@ module Geocoder::Store
clause += ", " unless clause.empty? clause += ", " unless clause.empty?
clause += "#{distance} AS distance" clause += "#{distance} AS distance"
end end
clause + if bearing
(bearing ? ", #{bearing} AS bearing" : "") clause += ", " unless clause.empty?
clause += "#{bearing} AS bearing"
end
clause
end end
## ##
......
...@@ -27,6 +27,14 @@ class NearTest < Test::Unit::TestCase ...@@ -27,6 +27,14 @@ 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_no_bearing
result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_bearing => false)
assert_match /AS distance/, result[:select]
assert_no_match /AS 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