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

Merge branch 'no_grouping'

parents 47edf4e3 4c37c98d
No related branches found
No related tags found
No related merge requests found
......@@ -133,12 +133,12 @@ module Geocoder::Store
"POWER(SIN((#{latitude} - #{lat_attr}) * PI() / 180 / 2), 2) + " +
"COS(#{latitude} * PI() / 180) * COS(#{lat_attr} * PI() / 180) * " +
"POWER(SIN((#{longitude} - #{lon_attr}) * PI() / 180 / 2), 2) ))"
options[:order] ||= "#{distance} ASC"
conditions = ["#{distance} <= ?", radius]
default_near_scope_options(latitude, longitude, radius, options).merge(
:select => "#{options[:select] || "#{table_name}.*"}, " +
"#{distance} AS distance" +
(bearing ? ", #{bearing} AS bearing" : ""),
:having => "#{distance} <= #{radius}"
:conditions => add_exclude_condition(conditions, options[:exclude])
)
end
......@@ -174,11 +174,16 @@ module Geocoder::Store
distance = "(#{dy} * ABS(#{lat_attr} - #{latitude}) * #{factor}) + " +
"(#{dx} * ABS(#{lon_attr} - #{longitude}) * #{factor})"
b = Geocoder::Calculations.bounding_box([latitude, longitude], radius, options)
conditions = [
"#{lat_attr} BETWEEN ? AND ? AND #{lon_attr} BETWEEN ? AND ?"] +
[b[0], b[2], b[1], b[3]
]
default_near_scope_options(latitude, longitude, radius, options).merge(
:select => "#{options[:select] || "#{table_name}.*"}, " +
"#{distance} AS distance" +
(bearing ? ", #{bearing} AS bearing" : ""),
:order => distance
:conditions => add_exclude_condition(conditions, options[:exclude])
)
end
......@@ -186,24 +191,24 @@ module Geocoder::Store
# Options used for any near-like scope.
#
def default_near_scope_options(latitude, longitude, radius, options)
lat_attr = geocoder_options[:latitude]
lon_attr = geocoder_options[:longitude]
b = Geocoder::Calculations.bounding_box([latitude, longitude], radius, options)
conditions = \
["#{lat_attr} BETWEEN ? AND ? AND #{lon_attr} BETWEEN ? AND ?"] +
[b[0], b[2], b[1], b[3]]
if obj = options[:exclude]
conditions[0] << " AND #{table_name}.id != ?"
conditions << obj.id
end
{
:group => columns.map{ |c| "#{table_name}.#{c.name}" }.join(','),
:order => options[:order],
:order => options[:order] || "distance",
:limit => options[:limit],
:offset => options[:offset],
:conditions => conditions
:offset => options[:offset]
}
end
##
# Adds a condition to exclude a given object by ID.
# The given conditions MUST be an array.
#
def add_exclude_condition(conditions, exclude)
if exclude
conditions[0] << " AND #{table_name}.id != ?"
conditions << exclude.id
end
conditions
end
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