mongoid_geo 0.5.4.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.rspec +1 -0
  2. data/Changelog.textile +18 -0
  3. data/Gemfile +17 -0
  4. data/README.textile +45 -4
  5. data/Rakefile +41 -11
  6. data/VERSION +1 -0
  7. data/lib/mongoid/geo/index.rb +14 -2
  8. data/sandbox/haversine.rb +17 -0
  9. data/sandbox/location.rb +38 -0
  10. data/sandbox/test.rb +12 -0
  11. data/spec/dummy/Rakefile +7 -0
  12. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  13. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  14. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  15. data/spec/dummy/config.ru +4 -0
  16. data/spec/dummy/config/application.rb +44 -0
  17. data/spec/dummy/config/boot.rb +10 -0
  18. data/spec/dummy/config/environment.rb +5 -0
  19. data/spec/dummy/config/environments/development.rb +26 -0
  20. data/spec/dummy/config/environments/production.rb +49 -0
  21. data/spec/dummy/config/environments/test.rb +35 -0
  22. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  23. data/spec/dummy/config/initializers/inflections.rb +10 -0
  24. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  25. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  26. data/spec/dummy/config/initializers/session_store.rb +8 -0
  27. data/spec/dummy/config/locales/en.yml +5 -0
  28. data/spec/dummy/config/mongoid.yml +23 -0
  29. data/spec/dummy/config/routes.rb +58 -0
  30. data/spec/dummy/public/404.html +26 -0
  31. data/spec/dummy/public/422.html +26 -0
  32. data/spec/dummy/public/500.html +26 -0
  33. data/spec/dummy/public/favicon.ico +0 -0
  34. data/spec/dummy/public/javascripts/application.js +2 -0
  35. data/spec/dummy/public/javascripts/controls.js +965 -0
  36. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  37. data/spec/dummy/public/javascripts/effects.js +1123 -0
  38. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  39. data/spec/dummy/public/javascripts/rails.js +175 -0
  40. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  41. data/spec/dummy/script/rails +6 -0
  42. data/spec/integration/navigation_spec.rb +9 -0
  43. data/spec/models/address.rb +23 -0
  44. data/spec/models/person.rb +9 -0
  45. data/spec/mongoid/db_v1_8/geo_near_places_spec.rb +30 -0
  46. data/spec/mongoid/db_v1_8/geo_near_spec.rb +55 -0
  47. data/spec/mongoid/db_v1_8/geonear_benchmark_spec.rb +48 -0
  48. data/spec/mongoid/db_v1_8/spherical_calc_spec.rb +149 -0
  49. data/spec/mongoid/geo/geo_fields_spec.rb +140 -0
  50. data/spec/mongoid/geo/geo_inclusions_spec.rb +20 -0
  51. data/spec/mongoid/geo/geo_inflections_spec.rb +237 -0
  52. data/spec/mongoid/geo/geo_near_spec.rb +58 -0
  53. data/spec/mongoid/geo/geo_near_to_model_spec.rb +74 -0
  54. data/spec/mongoid/geo/geo_spherical_mode_spec.rb +137 -0
  55. data/spec/mongoid/spec_helper.rb +23 -0
  56. data/spec/spec_helper.rb +31 -0
  57. metadata +174 -29
@@ -0,0 +1,140 @@
1
+ require "mongoid/spec_helper"
2
+
3
+ describe Mongoid::Fields do
4
+
5
+ let(:address) do
6
+ Address.new
7
+ end
8
+
9
+ let(:point) do
10
+ b = (Struct.new :lat, :lng).new
11
+ b.lat = 72
12
+ b.lng = -44
13
+ b
14
+ end
15
+
16
+ let(:location) do
17
+ a = (Struct.new :location).new
18
+ a.location = point
19
+ a
20
+ end
21
+
22
+ let(:position) do
23
+ a = (Struct.new :position).new
24
+ a.position = point
25
+ a
26
+ end
27
+
28
+
29
+ let(:other_point) do
30
+ b = (Struct.new :latitude, :longitude).new
31
+ b.latitude = 72
32
+ b.longitude = -44
33
+ b
34
+ end
35
+
36
+
37
+ describe "Special geo Array setter" do
38
+ it "should split a String into parts and convert to floats" do
39
+ address.location = "23.5, -47"
40
+ address.location.should == [23.5, -47]
41
+ end
42
+
43
+ it "should convert Strings into floats" do
44
+ address.location = "23.5", "-48"
45
+ address.location.should == [23.5, -48]
46
+ end
47
+
48
+ it "should work with point Hash, keys :lat, :lng" do
49
+ address.location = {:lat => 23.5, :lng => -49}
50
+ address.location.should == [23.5, -49]
51
+ end
52
+
53
+ it "should work with point Hash, keys :latitude, :longitude" do
54
+ address.location = {:latitude => 23.5, :longitude => -49}
55
+ address.location.should == [23.5, -49]
56
+ end
57
+
58
+ it "should work with point Hash keys 0 and 1" do
59
+ address.location = {"0" => 41, "1" => -71}
60
+ address.location.should == [41, -71]
61
+ end
62
+
63
+
64
+ it "should work with point hashes using the first point only" do
65
+ address.location = [{:lat => 23.5, :lng => -49}, {:lat => 72, :lng => -49}]
66
+ address.location.should == [23.5, -49]
67
+ end
68
+
69
+ it "should work with point object has #lat and #lng methods" do
70
+ address.location = point
71
+ address.location.should == [72, -44]
72
+ end
73
+
74
+ it 'should work with point object that has location attribute' do
75
+ address.location = location
76
+ address.location.should == [72, -44]
77
+ end
78
+
79
+ it 'should work with point object that has position attribute add should add #lat and #lng methods' do
80
+ address.location = position
81
+ address.location.should == [72, -44]
82
+ address.lat.should == 72
83
+ address.lng.should == -44
84
+ end
85
+
86
+ it 'should work with point object that has position attribute and should add #latitude and #longitude methods' do
87
+ address.pos = position
88
+ address.pos.should == [72, -44]
89
+ address.latitude.should == 72
90
+ address.latitude = 45
91
+ address.latitude.should == 45
92
+ address.longitude.should == -44
93
+ end
94
+
95
+ it 'should work with point object that has #latitude and #longitude methods' do
96
+ address.location = other_point
97
+ address.location.should == [72, -44]
98
+ end
99
+
100
+ it "should work with point objects using the first point only" do
101
+ address.location = [point, {:lat => 72, :lng => -49}]
102
+ address.location.should == [72, -44]
103
+ end
104
+
105
+ it "should drop nils" do
106
+ address.location = [nil, point, {:lat => 72, :lng => -49}]
107
+ address.location.should == [72, -44]
108
+ end
109
+
110
+ it "should default to normal behavior" do
111
+ address.location = 23.5, -49
112
+ address.location.should == [23.5, -49]
113
+
114
+ address.location = [23.5, -50]
115
+ address.location.should == [23.5, -50]
116
+ end
117
+
118
+ it "should handle nil values" do
119
+ address.location = nil
120
+ address.location.should be_nil
121
+ address.lat.should be_nil
122
+ address.lng.should be_nil
123
+ end
124
+
125
+ it "should allow lat then lon assignment when nil" do
126
+ address.location = nil
127
+ address.lat = 41
128
+ address.lng = -71
129
+ address.location.should == [41, -71]
130
+ end
131
+
132
+ it "should allow lon then lat assignment when nil" do
133
+ address.location = nil
134
+ address.lng = -71
135
+ address.lat = 41
136
+ address.location.should == [41, -71]
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,20 @@
1
+ require "mongoid/spec_helper"
2
+
3
+ describe Mongoid::Criterion::Inclusion do
4
+
5
+ let(:base) do
6
+ Mongoid::Criteria.new(Address)
7
+ end
8
+
9
+ describe "#near" do
10
+
11
+ let(:criteria) do
12
+ base.nearSphere(:location => [ 72, -44 ])
13
+ end
14
+
15
+ it "adds the $near modifier to the selector" do
16
+ criteria.selector.should ==
17
+ { :location => { "$nearSphere" => [ 72, -44 ] } }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,237 @@
1
+ require "mongoid/spec_helper"
2
+
3
+ describe Mongoid::Extensions::Symbol::Inflections do
4
+
5
+ let(:base) do
6
+ Mongoid::Criteria.new(Address)
7
+ end
8
+
9
+ describe "#nearSphere" do
10
+ let(:criteria) do
11
+ base.where(:location.nearSphere => [ 72, -44 ])
12
+ end
13
+
14
+ it "returns a selector matching a ne clause" do
15
+ criteria.selector.should ==
16
+ { :location => { "$nearSphere" => [ 72, -44 ] } }
17
+ end
18
+ end
19
+
20
+ describe "#nearMax" do
21
+
22
+ let(:criteria) do
23
+ base.where(:location.nearMax => [{:latitude => 72, :longitude => -44 }, 5])
24
+ end
25
+
26
+ it "adds the $near and $maxDistance modifiers to the selector" do
27
+ criteria.selector.should ==
28
+ { :location => { "$near" => [ 72, -44 ], "$maxDistance" => 5 } }
29
+ end
30
+ end
31
+
32
+ # this could be used to optimize calculation speed
33
+ describe "#nearMax sphere and flat" do
34
+
35
+ let(:criteria) do
36
+ base.where(:location.nearMax(:flat, :sphere) => [{:lat => 72, :lng => -44 }, 5])
37
+ end
38
+
39
+ it "adds the $near and $maxDistance modifiers to the selector" do
40
+ criteria.selector.should ==
41
+ { :location => { "$near" => [ 72, -44 ], "$maxDistanceSphere" => 5 } }
42
+ end
43
+ end
44
+
45
+ # this could be used to optimize calculation speed
46
+ describe "#nearMax hash values" do
47
+
48
+ let(:point) do
49
+ b = (Struct.new :lat, :lng).new
50
+ b.lat = 72
51
+ b.lng = -44
52
+ b
53
+ end
54
+
55
+ let(:criteria) do
56
+ base.where(:location.nearMax => {:point => point, :distance => 5})
57
+ end
58
+
59
+ it "adds the $near and $maxDistance modifiers to the selector" do
60
+ criteria.selector.should ==
61
+ { :location => { "$near" => [ 72, -44 ], "$maxDistance" => 5 } }
62
+ end
63
+ end
64
+
65
+ # this could be used to optimize calculation speed
66
+ describe "#nearMax hash values" do
67
+ let(:point_distance) do
68
+ b = (Struct.new :point, :distance).new
69
+ b.point = [72, -44]
70
+ b.distance = 5
71
+ b
72
+ end
73
+
74
+ let(:criteria) do
75
+ base.where(:location.nearMax => point_distance)
76
+ end
77
+
78
+ it "adds the $near and $maxDistance modifiers to the selector" do
79
+ criteria.selector.should ==
80
+ { :location => { "$near" => [ 72, -44 ], "$maxDistance" => 5 } }
81
+ end
82
+ end
83
+
84
+ describe "#nearMax sphere" do
85
+
86
+ let(:criteria) do
87
+ base.where(:location.nearMax(:sphere) => [[ 72, -44 ], 5])
88
+ end
89
+
90
+ it "adds the $near and $maxDistance modifiers to the selector" do
91
+ criteria.selector.should ==
92
+ { :location => { "$nearSphere" => [ 72, -44 ], "$maxDistanceSphere" => 5 } }
93
+ end
94
+ end
95
+
96
+
97
+ describe "#withinBox" do
98
+ let(:point_a) { [ 72, -44 ] }
99
+ let(:point_b) { [ 71, -45 ] }
100
+
101
+ let(:criteria) do
102
+ base.where(:location.withinBox => [point_a, point_b])
103
+ end
104
+
105
+ it "adds the $near and $maxDistance modifiers to the selector" do
106
+ criteria.selector.should ==
107
+ { :location => { "$within" => { "$box" => [point_a, point_b] } } }
108
+ end
109
+ end
110
+
111
+ describe "#withinBox, one hash point" do
112
+ let(:point_a) do
113
+ {:lat => 72, :lng => -44 }
114
+ end
115
+ let(:point_b) { [ 71, -45 ] }
116
+
117
+ let(:criteria) do
118
+ base.where(:location.withinBox => [point_a, point_b])
119
+ end
120
+
121
+ it "adds the $near and $maxDistance modifiers to the selector" do
122
+ criteria.selector.should ==
123
+ { :location => { "$within" => { "$box" => [[72, -44], [ 71, -45 ]] } } }
124
+ end
125
+ end
126
+
127
+
128
+ describe "#withinBox hash box" do
129
+ let(:point_a) { [ 72, -44 ] }
130
+ let(:point_b) { [ 71, -45 ] }
131
+ let(:box) do
132
+ {:lower_left => point_a, :upper_right => point_b}
133
+ end
134
+
135
+ let(:criteria) do
136
+ base.where(:location.withinBox => box)
137
+ end
138
+
139
+ it "adds the $near and $maxDistance modifiers to the selector" do
140
+ criteria.selector.should ==
141
+ { :location => { "$within" => { "$box" => [point_a, point_b] } } }
142
+ end
143
+ end
144
+
145
+ describe "#withinBox Struct box" do
146
+ let(:point_a) { [ 72, -44 ] }
147
+ let(:point_b) { [ 71, -45 ] }
148
+ let(:box) do
149
+ b = (Struct.new :lower_left, :upper_right).new
150
+ b.lower_left = point_a
151
+ b.upper_right = point_b
152
+ b
153
+ end
154
+
155
+ let(:criteria) do
156
+ base.where(:location.withinBox => box)
157
+ end
158
+
159
+ it "adds the $near and $maxDistance modifiers to the selector" do
160
+ criteria.selector.should ==
161
+ { :location => { "$within" => { "$box" => [point_a, point_b] } } }
162
+ end
163
+ end
164
+
165
+ describe "#withinBox sphere" do
166
+ let(:point_a) { [ 72, -44 ] }
167
+ let(:point_b) { [ 71, -45 ] }
168
+
169
+ let(:criteria) do
170
+ base.where(:location.withinBox(:sphere) => [point_a, point_b])
171
+ end
172
+
173
+ it "adds the $near and $maxDistance modifiers to the selector" do
174
+ criteria.selector.should ==
175
+ { :location => { "$within" => { "$boxSphere" => [point_a, point_b] } } }
176
+ end
177
+ end
178
+
179
+ describe "#withinCenter" do
180
+ let(:criteria) do
181
+ base.where(:location.withinCenter => [[ 72, -44 ], 5])
182
+ end
183
+
184
+ it "adds the $within and $center modifiers to the selector" do
185
+ criteria.selector.should ==
186
+ { :location => { "$within" => { "$center" => [[ 72, -44 ], 5]} }}
187
+ end
188
+ end
189
+
190
+ describe "#withinCenter hash circle" do
191
+ let(:center) { [ 72, -44 ] }
192
+ let(:circle) do
193
+ {:center => center, :radius => 5}
194
+ end
195
+
196
+ let(:criteria) do
197
+ base.where(:location.withinCenter => circle)
198
+ end
199
+
200
+ it "adds the $within and $center modifiers to the selector" do
201
+ criteria.selector.should ==
202
+ { :location => { "$within" => { "$center" => [[ 72, -44 ], 5]} }}
203
+ end
204
+ end
205
+
206
+ describe "#withinBox Struct circle" do
207
+ let(:center) { [ 71, -45 ] }
208
+ let(:box) do
209
+ b = (Struct.new :center, :radius).new
210
+ b.center = center
211
+ b.radius = radius
212
+ b
213
+ end
214
+
215
+ let(:criteria) do
216
+ base.where(:location.withinCenter => [[ 72, -44 ], 5])
217
+ end
218
+
219
+ it "adds the $within and $center modifiers to the selector" do
220
+ criteria.selector.should ==
221
+ { :location => { "$within" => { "$center" => [[ 72, -44 ], 5]} }}
222
+ end
223
+ end
224
+
225
+
226
+ describe "#withinCenter sphere" do
227
+ let(:criteria) do
228
+ base.where(:location.withinCenter(:sphere) => [[ 72, -44 ], 5])
229
+ end
230
+
231
+ it "adds the $within and $center modifiers to the selector" do
232
+ criteria.selector.should ==
233
+ { :location => { "$within" => { "$centerSphere" => [[ 72, -44 ], 5]} }}
234
+ end
235
+ end
236
+
237
+ end
@@ -0,0 +1,58 @@
1
+ require "mongoid/spec_helper"
2
+
3
+ Address.collection.create_index([['location', Mongo::GEO2D]], :min => -180, :max => 180)
4
+
5
+ describe Mongoid::Geo::Near do
6
+
7
+ let(:address) do
8
+ Address.new
9
+ end
10
+
11
+ before(:each) do
12
+ Address.create(:location => [45, 11], :city => 'Munich')
13
+ Address.create(:location => [46, 12], :city => 'Berlin')
14
+ end
15
+
16
+ describe "geoNear" do
17
+ it "should work with specifying specific center and different location attribute on collction" do
18
+ address.location = "23.5, -47"
19
+ Address.geoNear(address.location, :location).first.location[0].should == 45
20
+ end
21
+
22
+ it "should assume same attribute searched on both center instance and collection" do
23
+ address.location = "23.5, -47"
24
+ Address.geoNear(address, :location).first.location[0].should == 45
25
+ end
26
+
27
+ describe 'option :num' do
28
+ it "should limit number of results to 1" do
29
+ address.location = "23.5, -47"
30
+ Address.geoNear(address, :location, :num => 1).size.should == 1
31
+ end
32
+ end
33
+
34
+ describe 'option :maxDistance' do
35
+ it "should limit on maximum distance" do
36
+ address.location = "45.1, 11.1"
37
+ # db.runCommand({ geoNear : "points", near :[45.1, 11.1]}).results;
38
+ # dis: is 0.14141869255648362 and 1.2727947855285668
39
+ Address.geoNear(address, :location, :maxDistance => 0.2).size.should == 1
40
+ end
41
+ end
42
+
43
+ describe 'option :distanceMultiplier' do
44
+ it "should multiply returned distance with multiplier" do
45
+ address.location = "45.1, 11.1"
46
+ Address.geoNear(address, :location, :distanceMultiplier => 4).map(&:distance).first.should > 0
47
+ end
48
+ end
49
+
50
+ describe 'option :query' do
51
+ it "should filter using extra query option" do
52
+ address.location = "45.1, 11.1"
53
+ # two record in the collection, only one's city is Munich
54
+ Address.geoNear(address, :location, :query => {:city => 'Munich'}).size.should == 1
55
+ end
56
+ end
57
+ end
58
+ end