Permalink
Browse files

fixing distance

  • Loading branch information...
1 parent 2717d88 commit 8156e41de0eb13d87083b1541fd5bd9066c74e6e @nofxx nofxx committed Sep 13, 2012
@@ -1,6 +1,7 @@
require 'mongoid'
require 'active_support/core_ext/string/inflections'
require 'active_support/concern'
+require 'mongoid_geospatial/geospatial'
require 'mongoid_geospatial/extensions/core_ext'
require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'
require 'mongoid_geospatial/field_option'
@@ -10,5 +11,3 @@
%w{point circle box line polygon}.each do |type|
require "mongoid_geospatial/fields/#{type}"
end
-
-require 'mongoid_geospatial/geospatial'
@@ -5,9 +5,21 @@ module Mongoid
module Geospatial
class Point
+ delegate :distance, :to => :to_geo
+
def to_geo
RGeo::Geographic.spherical_factory.point x, y
end
+
+ def self.mongoize(obj)
+ case obj
+ when RGeo::Geographic::SphericalPointImpl then [obj.x, obj.y]
+ when Point then obj.mongoize
+ when Array then obj.to_xy
+ when Hash then obj.to_xy
+ else obj
+ end
+ end
end
@@ -120,11 +120,6 @@
bar[:location].should == [3,2]
end
- it "should not calculate distance between points w/o external" do
- bar = Bar.create!(location: [5,5])
- bar.location.should_not respond_to(:distance)
- end
-
end
# should raise
@@ -7,6 +7,12 @@
Bar.count.should eql(1)
end
+ it "should not respond to distance before loading external" do
+ bar = Bar.create!(location: [5,5])
+ bar.location.should_not respond_to(:distance)
+ end
+
+
describe "queryable" do
before do
@@ -40,7 +46,7 @@
it "should calculate 3d distances by default" do
bar = Bar.create! location: [-73.77694444, 40.63861111 ]
bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
- bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
+ bar.location.distance(bar2.location.to_geo).to_i.should be_within(1).of(3978262)
end
end

0 comments on commit 8156e41

Please sign in to comment.