From ba149bd4e0102f34d696d2d22ce4623c0fad5617 Mon Sep 17 00:00:00 2001 From: Constantine Mavromoustakos <cmavromoustakos@gmail.com> Date: Sat, 28 Apr 2012 13:06:51 -0400 Subject: [PATCH] Added skip_index option to the geocoded_by and reverse_geocoded_by methods to avoid creating an index by default. --- README.rdoc | 6 ++++++ lib/geocoder/models/mongo_base.rb | 6 ++++-- lib/geocoder/models/mongo_mapper.rb | 6 ++++-- lib/geocoder/models/mongoid.rb | 6 ++++-- test/mongoid_test.rb | 5 +++++ test/mongoid_test_helper.rb | 8 ++++++++ 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.rdoc b/README.rdoc index 883788e5..eafabe84 100644 --- a/README.rdoc +++ b/README.rdoc @@ -83,6 +83,12 @@ Be sure to read <i>Latitude/Longitude Order</i> in the <i>Notes on MongoDB</i> s MongoMapper is very similar to Mongoid, just be sure to include <tt>Geocoder::Model::MongoMapper</tt>. +=== MongoMapper, Mongoid Indexes + +By default the methods geocoded_by and reverse_geocoded_by will create a geospatial index. If you want to skip this option call the methids with the :skip_index option + include Geocoder::Model::Mongoid + geocoded_by :address, :skip_index => true + === Bulk Geocoding If you have just added geocoding to an existing application with a lot of objects you can use this Rake task to geocode them all: diff --git a/lib/geocoder/models/mongo_base.rb b/lib/geocoder/models/mongo_base.rb index 58cad07a..29da5bcd 100644 --- a/lib/geocoder/models/mongo_base.rb +++ b/lib/geocoder/models/mongo_base.rb @@ -16,7 +16,8 @@ module Geocoder :geocode => true, :user_address => address_attr, :coordinates => options[:coordinates] || :coordinates, - :geocode_block => block + :geocode_block => block, + :skip_index => options[:skip_index] || false ) end @@ -28,7 +29,8 @@ module Geocoder :reverse_geocode => true, :fetched_address => options[:address] || :address, :coordinates => coordinates_attr, - :reverse_block => block + :reverse_block => block, + :skip_index => options[:skip_index] || false ) end diff --git a/lib/geocoder/models/mongo_mapper.rb b/lib/geocoder/models/mongo_mapper.rb index a093f6bf..17d83402 100644 --- a/lib/geocoder/models/mongo_mapper.rb +++ b/lib/geocoder/models/mongo_mapper.rb @@ -16,8 +16,10 @@ module Geocoder def geocoder_init(options) super(options) - ensure_index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], - :min => -180, :max => 180 # create 2d index + if options[:skip_index] == false + ensure_index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], + :min => -180, :max => 180 # create 2d index + end end end end diff --git a/lib/geocoder/models/mongoid.rb b/lib/geocoder/models/mongoid.rb index e3ca53a3..a7124e5b 100644 --- a/lib/geocoder/models/mongoid.rb +++ b/lib/geocoder/models/mongoid.rb @@ -16,8 +16,10 @@ module Geocoder def geocoder_init(options) super(options) - index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], - :min => -180, :max => 180 # create 2d index + if options[:skip_index] == false + index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], + :min => -180, :max => 180 # create 2d index + end end end end diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb index 2703f4f9..9fb801fc 100644 --- a/test/mongoid_test.rb +++ b/test/mongoid_test.rb @@ -19,4 +19,9 @@ class MongoidTest < Test::Unit::TestCase p = Place.near(location) assert_equal p.selector[:location]['$nearSphere'], location.reverse end + + def test_index_is_skipped_if_skip_option_flag + result = PlaceWithoutIndex.index_options.keys.flatten[0] == :coordinates + assert_equal result, false + end end diff --git a/test/mongoid_test_helper.rb b/test/mongoid_test_helper.rb index 68c2af82..572db2b9 100644 --- a/test/mongoid_test_helper.rb +++ b/test/mongoid_test_helper.rb @@ -29,3 +29,11 @@ class Place write_attribute :address, address end end + +class PlaceWithoutIndex + include Mongoid::Document + include Geocoder::Model::Mongoid + + field :location, :type => Array + geocoded_by :location, :skip_index => true +end -- GitLab