Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added :nearest and :farthest support through find method.

git-svn-id: http://geokit.rubyforge.org/svn/trunk@22 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
commit de215e5670d73d7bac9996f7786332dcb52bed6e 1 parent beb6824
bill_eisenhauer authored
View
12 lib/geo_kit/acts_as_mappable.rb
@@ -68,6 +68,7 @@ def find(*args)
units = extract_units_from_options(options)
formula = extract_formula_from_options(options)
add_distance_to_select(options, origin, units, formula) if origin
+ apply_find_scope(args, options)
substitute_distance_in_conditions(options, origin, units, formula) if origin && options.has_key?(:conditions)
args.push(options)
super(*args)
@@ -113,6 +114,17 @@ def distance_sql(origin, units=default_units, formula=default_formula)
end
private
+
+ def apply_find_scope(args, options)
+ case args.first
+ when :nearest
+ args[0] = :first
+ options[:order] = "#{distance_column_name} ASC"
+ when :farthest
+ args[0] = :first
+ options[:order] = "#{distance_column_name} DESC"
+ end
+ end
# Extracts the origin instance out of the options if it exists and returns
# it. If there is no origin, looks for latitude and longitude values to
View
1  lib/geo_kit/ip_geocode_lookup.rb
@@ -40,6 +40,7 @@ def retrieve_location_from_cookie_or_service
# Either returns the real ip address or a fake one in the case where
# doing live testing via localhost.
def get_ip_address
+
address = request.remote_ip
address == '127.0.0.1' ? '12.215.42.19' : address
end
View
8 test/acts_as_mappable_test.rb
@@ -143,6 +143,10 @@ def test_find_nearest
assert_equal @loc_a, Location.find_nearest(:origin => @loc_a)
end
+ def test_find_nearest_through_find
+ assert_equal @loc_a, Location.find(:nearest, :origin => @loc_a)
+ end
+
def test_find_nearest_with_coordinates
assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
end
@@ -151,6 +155,10 @@ def test_find_farthest
assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
end
+ def test_find_farthest_through_find
+ assert_equal @loc_e, Location.find(:farthest, :origin => @loc_a)
+ end
+
def test_find_farthest_with_coordinates
assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
end
Please sign in to comment.
Something went wrong with that request. Please try again.