diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index b49a4a97c891bf7af4a98f1df35d8eaff3448802..3bd17d08d82efe2eaf8b8b4cff6f7f3ae1a01114 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -77,8 +77,6 @@ module Geocoder::Store end end - private # ---------------------------------------------------------------- - ## # Get options hash suitable for passing to ActiveRecord.find to get # records within a radius (in kilometers) of the given point. @@ -119,6 +117,8 @@ module Geocoder::Store } end + private # ---------------------------------------------------------------- + ## # SQL for calculating distance based on the current database's # capabilities (trig functions?). diff --git a/test/near_test.rb b/test/near_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..5a2dc9fae5bd16b2c36424858685c610206041c7 --- /dev/null +++ b/test/near_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' + +class NearTest < Test::Unit::TestCase + + def test_near_scope_options + result = Event.near_scope_options(1.0, 2.0, 5) + expected = { + :select => + "test_table_name.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((1.0 - test_table_name.latitude) * PI() / 180 / 2), 2) + COS(1.0 * PI() / 180) * COS(test_table_name.latitude * PI() / 180) * POWER(SIN((2.0 - test_table_name.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(test_table_name.longitude - 2.0), RADIANS(test_table_name.latitude - 1.0))) + 360 AS decimal) % 360 AS bearing", + :conditions => + ["3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((1.0 - test_table_name.latitude) * PI() / 180 / 2), 2) + COS(1.0 * PI() / 180) * COS(test_table_name.latitude * PI() / 180) * POWER(SIN((2.0 - test_table_name.longitude) * PI() / 180 / 2), 2))) <= ?", 5], + :order => "distance ASC" + } + + assert_equal expected, result + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index e390f0dbae5af55a5ca3419af546204cf7ebed94..1f592ecf259edd4d72efd06d4a19d38474579545 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,6 +4,12 @@ require 'test/unit' $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +class MysqlConnection + def adapter_name + "mysql" + end +end + ## # Simulate enough of ActiveRecord::Base that objects can be used for testing. # @@ -28,6 +34,10 @@ module ActiveRecord def self.scope(*args); end + def self.connection + MysqlConnection.new + end + def method_missing(name, *args, &block) if name.to_s[-1..-1] == "=" write_attribute name.to_s[0...-1], *args