Skip to content

Commit

Permalink
some fixes and remove a few remains of mongoid 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Sep 12, 2012
1 parent 4961a2a commit 7e359e6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 58 deletions.
6 changes: 3 additions & 3 deletions lib/mongoid_geospatial/criterion/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ module Criterion #:nodoc:
# becomes:
# <tt> { :field.lt => "value }</tt>
class Complex

attr_accessor :key, :operator

def initialize(opts = {})
@key, @operator = opts[:key], opts[:operator]
end


def to_mongo_query v
{"$#{operator}" => v}
end

end
end
end
1 change: 0 additions & 1 deletion lib/mongoid_geospatial/criterion/inclusion.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# module Mongoid #:nodoc:
# module Criterion #:nodoc:
# module Inclusion
Expand Down
5 changes: 4 additions & 1 deletion lib/mongoid_geospatial/fields/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(x, y)
def mongoize
[x, y]
end
alias :to_a :mongoize
alias :to_xy :mongoize

def [](args)
mongoize[args]
Expand All @@ -27,7 +29,8 @@ def demongoize(object)
def mongoize(object)
case object
when Point then object.mongoize
when Hash then [object[:x], object[:y]]
when Array then object.to_xy
when Hash then object.to_xy
else object
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid_geospatial/geospatial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ module Geospatial
:sm => EARTH_RADIUS_KM*0.53995680345572 # sea mile
}

mattr_accessor :lng_symbol
mattr_accessor :lat_symbol
mattr_accessor :lng_symbols
mattr_accessor :lat_symbols
mattr_accessor :earth_radius
mattr_accessor :geo_factory

mattr_accessor :paginator
mattr_accessor :default_per_page

@@lng_symbol = LNG_SYMBOLS[0]
@@lat_symbol = LAT_SYMBOLS[0]
@@lng_symbols = LNG_SYMBOLS.dup
@@lat_symbols = LAT_SYMBOLS.dup
@@earth_radius = EARTH_RADIUS.dup
@@paginator = :array
@@default_per_page = 25
Expand Down
4 changes: 1 addition & 3 deletions spec/models/phone.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class Phone
include Mongoid::Document
field :number
if Mongoid::VERSION < '3'
key :number
end

embeds_one :country_code
embedded_in :person
end
5 changes: 3 additions & 2 deletions spec/mongoid_geospatial/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "spec_helper"

describe (Mongoid::VERSION > '3' ? Mongoid::Contextual::Mongo : Mongoid::Contexts::Mongo) do
describe Mongoid::Contextual::Mongo do

describe "#geo_near" do

before do
Expand Down Expand Up @@ -118,7 +119,7 @@
end

# check results['results'] in GeoNearResults
it 'should find 25 items' do
it 'should find 25 items' do
near.size.should == 25
end

Expand Down
13 changes: 3 additions & 10 deletions spec/mongoid_geospatial/criterion/inclusion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
context "with untyped criteria" do

it "typecasts integers" do
pending "seems typecasts not work fo mongoid 3.0" if Mongoid::VERSION > '3'
Person.where(:age => "33").should == [ person ]
end

Expand Down Expand Up @@ -240,11 +239,7 @@
context "#match" do

it "returns those matching a partial element in a list" do
if Mongoid::VERSION > '3'
Person.where({'things.phone' => "HTC Incredible" }).should == [person]
else
Person.where(:things.matches => { :phone => "HTC Incredible" }).should == [person]
end
Person.where({'things.phone' => "HTC Incredible" }).should == [person]
end
end

Expand Down Expand Up @@ -279,7 +274,6 @@
end

it "returns the documents sorted closest to furthest" do
pending "NearSpatial#to_mongo_query seems not work for mongoid 3 " if Mongoid::VERSION > '3'
Bar.where(:location.near => {:point=>[ 41.23, 2.9 ],:max => 20}).should == [ paris, prague, berlin ]
end

Expand All @@ -297,7 +291,6 @@

context ":box, :polygon" do
before do
Bar.delete_all
Bar.create_indexes
end

Expand All @@ -314,7 +307,7 @@
end

it "returns the documents within a box" do
Bar.where(:location.within(:box) => [[ 47, 1 ],[ 49, 3 ]]).should == [ paris ]
Bar.where(:location.within(:box) => [[ 47, 1 ],[ 49, 3 ]]).to_a.should == [ paris ]
end

it "returns the documents within a polygon" do
Expand All @@ -330,9 +323,9 @@
end

end

context ":circle :center_sphere" do
before do
Bar.delete_all
Bar.create_indexes
end
let!(:mile1) do
Expand Down
35 changes: 16 additions & 19 deletions spec/mongoid_geospatial/fields/point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
end


it "should accept an RGeo object" do
point = RGeo::Geographic.spherical_factory.point 1, 2
bar = Bar.create!(location: point)
bar.location.x.should be_within(0.1).of(1)
bar.location.y.should be_within(0.1).of(2)
end

describe "methods" do

it "should have a .to_a" do
Expand All @@ -49,23 +42,18 @@
end

it "should calculate distance between points" do
pending
bar = Bar.create!(location: [5,5])
bar2 = Bar.create!(location: [15,15])
bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
end

it "should calculate distance between points miles" do
pending
bar = Bar.create!(location: [5,5])
bar2 = Bar.create!(location: [15,15])
bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
end

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)
end
it "should calculate 3d distances by default" do
pending
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)
end

end

Expand All @@ -91,6 +79,15 @@
geom.to_geo.class.should eql(RGeo::Geographic::SphericalPointImpl)
end

it "should accept an RGeo object" do
pending
point = RGeo::Geographic.spherical_factory.point 1, 2
bar = Bar.create!(location: point)
bar.location.x.should be_within(0.1).of(1)
bar.location.y.should be_within(0.1).of(2)
end


describe "instantiated" do

let(:bar) { Bar.create!(name: 'Vitinho', location: [10,10]) }
Expand Down
16 changes: 1 addition & 15 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,13 @@
Dir[ File.join(SUPPORT, "*.rb") ].each { |file| require File.basename(file) }

def bson_object_id_class
Mongoid::VERSION > '3' ? Moped::BSON:: ObjectId : BSON::ObjectId
Moped::BSON::ObjectId
end

RSpec.configure do |config|
config.mock_with(:mocha)

config.before(:each) do
Mongoid.purge!
# Mongoid.database.collections.each do |collection|
# unless collection.name =~ /^system\./
# collection.remove
# end
# end
end

# We filter out the specs that require authentication if the database has not
# had the mongoid user set up properly.
# user_configured = Support::Authentication.configured?
# warn(Support::Authentication.message) unless user_configured

# config.filter_run_excluding(:config => lambda { |value|
# return true if value == :user && !user_configured
# })
end

0 comments on commit 7e359e6

Please sign in to comment.