From cbe663a011ecf44ae0b86645d6c04549d870f318 Mon Sep 17 00:00:00 2001 From: "Peter M. Goldstein" <peter.m.goldstein@gmail.com> Date: Mon, 23 Jul 2012 20:11:26 -0700 Subject: [PATCH] Update code to account for Mongo 3.0.x changes while preserving backwards compatibility --- lib/geocoder/models/mongoid.rb | 10 ++++++++-- test/mongoid_test.rb | 3 ++- test/mongoid_test_helper.rb | 8 ++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/geocoder/models/mongoid.rb b/lib/geocoder/models/mongoid.rb index a7124e5b..9e0b9fd8 100644 --- a/lib/geocoder/models/mongoid.rb +++ b/lib/geocoder/models/mongoid.rb @@ -17,8 +17,14 @@ module Geocoder def geocoder_init(options) super(options) if options[:skip_index] == false - index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], - :min => -180, :max => 180 # create 2d index + # create 2d index + if (::Mongoid::VERSION >= "3") + index({ geocoder_options[:coordinates].to_sym => '2d' }, + {:min => -180, :max => 180}) + else + index [[ geocoder_options[:coordinates], '2d' ]], + :min => -180, :max => 180 + end end end end diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb index 0cc6b93a..1af5e9cd 100644 --- a/test/mongoid_test.rb +++ b/test/mongoid_test.rb @@ -17,7 +17,8 @@ class MongoidTest < Test::Unit::TestCase def test_custom_coordinate_field_near_scope location = [40.750354, -73.993371] p = Place.near(location) - assert_equal p.selector[:location]['$nearSphere'], location.reverse + key = Mongoid::VERSION >= "3" ? "location" : :location + assert_equal p.selector[key]['$nearSphere'], location.reverse end def test_model_configuration diff --git a/test/mongoid_test_helper.rb b/test/mongoid_test_helper.rb index 572db2b9..cf37bb01 100644 --- a/test/mongoid_test_helper.rb +++ b/test/mongoid_test_helper.rb @@ -7,8 +7,12 @@ require 'geocoder/models/mongoid' $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -Mongoid.configure do |config| - config.logger = Logger.new($stderr, :debug) +if (::Mongoid::VERSION >= "3") + Mongoid.logger = Logger.new($stderr, :debug) +else + Mongoid.configure do |config| + config.logger = Logger.new($stderr, :debug) + end end ## -- GitLab