mongoid_geospatial 1.0.0rc0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. data/.document +5 -0
  2. data/.gitignore +49 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +11 -0
  5. data/README.md +330 -0
  6. data/Rakefile +18 -0
  7. data/lib/mongoid_geospatial/contexts/mongo.rb +115 -0
  8. data/lib/mongoid_geospatial/criteria.rb +5 -0
  9. data/lib/mongoid_geospatial/criterion/complex.rb +19 -0
  10. data/lib/mongoid_geospatial/criterion/inclusion.rb +14 -0
  11. data/lib/mongoid_geospatial/criterion/near_spatial.rb +50 -0
  12. data/lib/mongoid_geospatial/criterion/within_spatial.rb +60 -0
  13. data/lib/mongoid_geospatial/criterion.rb +3 -0
  14. data/lib/mongoid_geospatial/extensions/hash.rb +22 -0
  15. data/lib/mongoid_geospatial/extensions/symbol.rb +46 -0
  16. data/lib/mongoid_geospatial/field_option.rb +16 -0
  17. data/lib/mongoid_geospatial/fields/line_string.rb +18 -0
  18. data/lib/mongoid_geospatial/fields/point.rb +51 -0
  19. data/lib/mongoid_geospatial/fields/polygon.rb +22 -0
  20. data/lib/mongoid_geospatial/finders.rb +5 -0
  21. data/lib/mongoid_geospatial/geospatial/core_ext.rb +27 -0
  22. data/lib/mongoid_geospatial/geospatial/geo_near_results.rb +140 -0
  23. data/lib/mongoid_geospatial/geospatial.rb +86 -0
  24. data/lib/mongoid_geospatial/version.rb +5 -0
  25. data/lib/mongoid_geospatial.rb +16 -0
  26. data/mongoid_geospatial.gemspec +28 -0
  27. data/spec/config/mongod.conf +3 -0
  28. data/spec/config/mongoid.yml +18 -0
  29. data/spec/functional/contexts/mongo_spec.rb +127 -0
  30. data/spec/functional/criterion/inclusion_spec.rb +356 -0
  31. data/spec/functional/mongoid_geospatial_spec.rb +54 -0
  32. data/spec/functional/spatial/geo_near_results_spec.rb +78 -0
  33. data/spec/models/account.rb +19 -0
  34. data/spec/models/acolyte.rb +9 -0
  35. data/spec/models/address.rb +62 -0
  36. data/spec/models/address_component.rb +5 -0
  37. data/spec/models/agent.rb +10 -0
  38. data/spec/models/alert.rb +5 -0
  39. data/spec/models/animal.rb +21 -0
  40. data/spec/models/answer.rb +4 -0
  41. data/spec/models/bar.rb +9 -0
  42. data/spec/models/birthday.rb +13 -0
  43. data/spec/models/book.rb +5 -0
  44. data/spec/models/business.rb +7 -0
  45. data/spec/models/callbacks.rb +57 -0
  46. data/spec/models/category.rb +13 -0
  47. data/spec/models/circus.rb +7 -0
  48. data/spec/models/comment.rb +13 -0
  49. data/spec/models/country_code.rb +6 -0
  50. data/spec/models/description.rb +11 -0
  51. data/spec/models/division.rb +5 -0
  52. data/spec/models/drug.rb +5 -0
  53. data/spec/models/employer.rb +5 -0
  54. data/spec/models/entry.rb +6 -0
  55. data/spec/models/event.rb +20 -0
  56. data/spec/models/farm.rb +10 -0
  57. data/spec/models/favorite.rb +6 -0
  58. data/spec/models/fruits.rb +11 -0
  59. data/spec/models/game.rb +18 -0
  60. data/spec/models/ghost.rb +7 -0
  61. data/spec/models/house.rb +4 -0
  62. data/spec/models/inheritance.rb +90 -0
  63. data/spec/models/league.rb +5 -0
  64. data/spec/models/location.rb +5 -0
  65. data/spec/models/login.rb +6 -0
  66. data/spec/models/membership.rb +4 -0
  67. data/spec/models/mixed_drink.rb +4 -0
  68. data/spec/models/name.rb +13 -0
  69. data/spec/models/namespacing.rb +11 -0
  70. data/spec/models/observed.rb +41 -0
  71. data/spec/models/override.rb +16 -0
  72. data/spec/models/owner.rb +6 -0
  73. data/spec/models/page.rb +5 -0
  74. data/spec/models/page_question.rb +4 -0
  75. data/spec/models/paranoid_post.rb +18 -0
  76. data/spec/models/parents.rb +32 -0
  77. data/spec/models/patient.rb +15 -0
  78. data/spec/models/person.rb +146 -0
  79. data/spec/models/pet.rb +7 -0
  80. data/spec/models/pet_owner.rb +6 -0
  81. data/spec/models/phone.rb +7 -0
  82. data/spec/models/player.rb +23 -0
  83. data/spec/models/post.rb +26 -0
  84. data/spec/models/preference.rb +9 -0
  85. data/spec/models/question.rb +8 -0
  86. data/spec/models/quiz.rb +6 -0
  87. data/spec/models/rating.rb +8 -0
  88. data/spec/models/river.rb +20 -0
  89. data/spec/models/role.rb +5 -0
  90. data/spec/models/service.rb +6 -0
  91. data/spec/models/shelf.rb +5 -0
  92. data/spec/models/slave_address_numbers.rb +14 -0
  93. data/spec/models/survey.rb +5 -0
  94. data/spec/models/tag.rb +6 -0
  95. data/spec/models/tracking_id_validation_history.rb +25 -0
  96. data/spec/models/translation.rb +5 -0
  97. data/spec/models/tree.rb +9 -0
  98. data/spec/models/user.rb +9 -0
  99. data/spec/models/user_account.rb +10 -0
  100. data/spec/models/vet_visit.rb +5 -0
  101. data/spec/models/video.rb +9 -0
  102. data/spec/models/wiki_page.rb +6 -0
  103. data/spec/spec_helper.rb +51 -0
  104. data/spec/support/authentication.rb +29 -0
  105. data/spec/unit/criterion/complex_spec.rb +15 -0
  106. data/spec/unit/criterion/inclusion_spec.rb +0 -0
  107. data/spec/unit/criterion/near_spatial_spec.rb +39 -0
  108. data/spec/unit/criterion/within_spatial_spec.rb +52 -0
  109. metadata +339 -0
@@ -0,0 +1,356 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Criterion::Inclusion do
4
+
5
+ before do
6
+ Person.delete_all
7
+ end
8
+
9
+ describe "#where" do
10
+
11
+ let(:dob) do
12
+ 33.years.ago.to_date
13
+ end
14
+
15
+ let(:lunch_time) do
16
+ 30.minutes.ago
17
+ end
18
+
19
+ let!(:person) do
20
+ Person.create(
21
+ :title => "Sir",
22
+ :dob => dob,
23
+ :lunch_time => lunch_time,
24
+ :age => 33,
25
+ :aliases => [ "D", "Durran" ],
26
+ :things => [ { :phone => 'HTC Incredible' } ]
27
+ )
28
+ end
29
+
30
+ context "when providing 24 character strings" do
31
+
32
+ context "when the field is not an id field" do
33
+
34
+ let(:string) do
35
+ BSON::ObjectId.new.to_s
36
+ end
37
+
38
+ let!(:person) do
39
+ Person.create(:title => string)
40
+ end
41
+
42
+ let(:from_db) do
43
+ Person.where(:title => string)
44
+ end
45
+
46
+ it "does not convert the field to a bson id" do
47
+ from_db.should == [ person ]
48
+ end
49
+ end
50
+ end
51
+
52
+ context "when providing string object ids" do
53
+
54
+ context "when providing a single id" do
55
+
56
+ let(:from_db) do
57
+ Person.where(:_id => person.id.to_s).first
58
+ end
59
+
60
+ it "returns the matching documents" do
61
+ from_db.should == person
62
+ end
63
+ end
64
+ end
65
+
66
+ context "chaining multiple wheres" do
67
+
68
+ context "when chaining on the same key" do
69
+
70
+ let(:from_db) do
71
+ Person.where(:title => "Maam").where(:title => "Sir")
72
+ end
73
+
74
+ it "overrides the previous key" do
75
+ from_db.should == [ person ]
76
+ end
77
+ end
78
+
79
+ context "with different criteria on the same key" do
80
+
81
+ it "merges criteria" do
82
+ Person.where(:age.gt => 30).where(:age.lt => 40).should == [person]
83
+ end
84
+
85
+ it "typecasts criteria" do
86
+ before_dob = (dob - 1.month).to_s
87
+ after_dob = (dob + 1.month).to_s
88
+ Person.where(:dob.gt => before_dob).and(:dob.lt => after_dob).should == [person]
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ context "with untyped criteria" do
95
+
96
+ it "typecasts integers" do
97
+ Person.where(:age => "33").should == [ person ]
98
+ end
99
+
100
+ it "typecasts datetimes" do
101
+ Person.where(:lunch_time => lunch_time.to_s).should == [ person ]
102
+ end
103
+
104
+ it "typecasts dates" do
105
+ Person.where({:dob => dob.to_s}).should == [ person ]
106
+ end
107
+
108
+ it "typecasts times with zones" do
109
+ time = lunch_time.in_time_zone("Alaska")
110
+ Person.where(:lunch_time => time).should == [ person ]
111
+ end
112
+
113
+ it "typecasts array elements" do
114
+ Person.where(:age.in => [17, "33"]).should == [ person ]
115
+ end
116
+
117
+ it "typecasts size criterion to integer" do
118
+ Person.where(:aliases.size => "2").should == [ person ]
119
+ end
120
+
121
+ it "typecasts exists criterion to boolean" do
122
+ Person.where(:score.exists => "f").should == [ person ]
123
+ end
124
+ end
125
+
126
+ context "with multiple complex criteria" do
127
+
128
+ before do
129
+ Person.create(:title => "Mrs", :age => 29)
130
+ Person.create(:title => "Ms", :age => 41)
131
+ end
132
+
133
+ it "returns those matching both criteria" do
134
+ Person.where(:age.gt => 30, :age.lt => 40).should == [person]
135
+ end
136
+
137
+ it "returns nothing if in and nin clauses cancel each other out" do
138
+ Person.any_in(:title => ["Sir"]).not_in(:title => ["Sir"]).should == []
139
+ end
140
+
141
+ it "returns nothing if in and nin clauses cancel each other out ordered the other way" do
142
+ Person.not_in(:title => ["Sir"]).any_in(:title => ["Sir"]).should == []
143
+ end
144
+
145
+ it "returns the intersection of in and nin clauses" do
146
+ Person.any_in(:title => ["Sir", "Mrs"]).not_in(:title => ["Mrs"]).should == [person]
147
+ end
148
+
149
+ it "returns the intersection of two in clauses" do
150
+ Person.where(:title.in => ["Sir", "Mrs"]).where(:title.in => ["Sir", "Ms"]).should == [person]
151
+ end
152
+ end
153
+
154
+ context "with complex criterion" do
155
+
156
+ context "#all" do
157
+
158
+ it "returns those matching an all clause" do
159
+ Person.where(:aliases.all => ["D", "Durran"]).should == [person]
160
+ end
161
+ end
162
+
163
+ context "#exists" do
164
+
165
+ it "returns those matching an exists clause" do
166
+ Person.where(:title.exists => true).should == [person]
167
+ end
168
+ end
169
+
170
+ context "#gt" do
171
+
172
+ it "returns those matching a gt clause" do
173
+ Person.where(:age.gt => 30).should == [person]
174
+ end
175
+ end
176
+
177
+ context "#gte" do
178
+
179
+ it "returns those matching a gte clause" do
180
+ Person.where(:age.gte => 33).should == [person]
181
+ end
182
+ end
183
+
184
+ context "#in" do
185
+
186
+ it "returns those matching an in clause" do
187
+ Person.where(:title.in => ["Sir", "Madam"]).should == [person]
188
+ end
189
+
190
+ it "allows nil" do
191
+ Person.where(:ssn.in => [nil]).should == [person]
192
+ end
193
+ end
194
+
195
+ context "#lt" do
196
+
197
+ it "returns those matching a lt clause" do
198
+ Person.where(:age.lt => 34).should == [person]
199
+ end
200
+ end
201
+
202
+ context "#lte" do
203
+
204
+ it "returns those matching a lte clause" do
205
+ Person.where(:age.lte => 33).should == [person]
206
+ end
207
+ end
208
+
209
+ context "#ne" do
210
+
211
+ it "returns those matching a ne clause" do
212
+ Person.where(:age.ne => 50).should == [person]
213
+ end
214
+ end
215
+
216
+ context "#nin" do
217
+
218
+ it "returns those matching a nin clause" do
219
+ Person.where(:title.nin => ["Esquire", "Congressman"]).should == [person]
220
+ end
221
+ end
222
+
223
+ context "#size" do
224
+
225
+ it "returns those matching a size clause" do
226
+ Person.where(:aliases.size => 2).should == [person]
227
+ end
228
+ end
229
+
230
+ context "#match" do
231
+
232
+ it "returns those matching a partial element in a list" do
233
+ Person.where(:things.matches => { :phone => "HTC Incredible" }).should == [person]
234
+ end
235
+ end
236
+
237
+ end
238
+
239
+ context "Geo Spatial Complex Where" do
240
+
241
+ let!(:home) do
242
+ [-73.98,40.77]
243
+ end
244
+
245
+ describe "#near" do
246
+ before do
247
+ Bar.delete_all
248
+ Bar.create_indexes
249
+ end
250
+
251
+ let!(:berlin) do
252
+ Bar.create(:location => [ 52.30, 13.25 ])
253
+ end
254
+
255
+ let!(:prague) do
256
+ Bar.create(:location => [ 50.5, 14.26 ])
257
+ end
258
+
259
+ let!(:paris) do
260
+ Bar.create(:location => [ 48.48, 2.20 ])
261
+ end
262
+
263
+ it "returns the documents sorted closest to furthest" do
264
+ Bar.where(:location.near => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
265
+ end
266
+
267
+ it "returns the documents sorted closest to furthest" do
268
+ Bar.where(:location.near => {:point=>[ 41.23, 2.9 ],:max => 20}).should == [ paris, prague, berlin ]
269
+ end
270
+
271
+ it "returns the documents sorted closest to furthest" do
272
+ Bar.where(:location.near_sphere => [ 41.23, 2.9 ]).should == [ paris, prague, berlin ]
273
+ end
274
+
275
+ end
276
+
277
+ context "#within" do
278
+
279
+ context ":box, :polygon" do
280
+ before do
281
+ Bar.delete_all
282
+ Bar.create_indexes
283
+ end
284
+
285
+ let!(:berlin) do
286
+ Bar.create(:name => 'berlin', :location => [ 52.30, 13.25 ])
287
+ end
288
+
289
+ let!(:prague) do
290
+ Bar.create(:name => 'prague',:location => [ 50.5, 14.26 ])
291
+ end
292
+
293
+ let!(:paris) do
294
+ Bar.create(:name => 'prague',:location => [ 48.48, 2.20 ])
295
+ end
296
+
297
+ it "returns the documents within a box" do
298
+ Bar.where(:location.within(:box) => [[ 47, 1 ],[ 49, 3 ]]).should == [ paris ]
299
+ end
300
+
301
+ it "returns the documents within a polygon", :if => (Mongoid.master.connection.server_version >= '1.9') do
302
+ Bar.where(:location.within(:polygon) => [[ 47, 1 ],[49,1.5],[ 49, 3 ],[46,5]]).should == [ paris ]
303
+ end
304
+
305
+ it "returns the documents within a center" do
306
+ Bar.where(:location.within(:center) => [[ 47, 1 ],4]).should == [ paris ]
307
+ end
308
+
309
+ it "returns the documents within a center_sphere" do
310
+ Bar.where(:location.within(:center_sphere) => [[ 48, 2 ],0.1]).should == [ paris ]
311
+ end
312
+
313
+ end
314
+ context ":circle :center_sphere" do
315
+ before do
316
+ Bar.delete_all
317
+ Bar.create_indexes
318
+ end
319
+ let!(:mile1) do
320
+ Bar.create(:name => 'mile1', :location => [-73.997345, 40.759382])
321
+ end
322
+
323
+ let!(:mile3) do
324
+ Bar.create(:name => 'mile2', :location => [-73.927088, 40.752151])
325
+ end
326
+
327
+ let!(:mile7) do
328
+ Bar.create(:name => 'mile3', :location => [-74.0954913, 40.7161472])
329
+ end
330
+
331
+ let!(:mile11) do
332
+ Bar.create(:name => 'mile4', :location => [-74.0604951, 40.9178011])
333
+ end
334
+
335
+ it "returns the documents within a center_sphere" do
336
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 2, :unit => :mi}).should == [ mile1 ]
337
+ end
338
+
339
+ it "returns the documents within a center_sphere" do
340
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 4, :unit => :mi}).should include(mile3)
341
+ end
342
+
343
+ it "returns the documents within a center_sphere" do
344
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 8, :unit => :mi}).should include(mile7)
345
+ end
346
+
347
+ it "returns the documents within a center_sphere" do
348
+ Bar.where(:location.within(:center_sphere) => {:point => home,:max => 12, :unit => :mi}).should include(mile11)
349
+ end
350
+ end
351
+ end
352
+ end
353
+
354
+ end
355
+
356
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial do
4
+
5
+ it "should instantiate with no problems" do
6
+ Bar.create!(name: "Moe's")
7
+ Bar.count.should eql(1)
8
+ end
9
+
10
+ it "should have a field mapped as point" do
11
+ bar = Bar.create!(location: [5,5])
12
+ bar.location.should be_a RGeo::Geographic::SphericalPointImpl
13
+ end
14
+
15
+ it "should accept an RGeo object" do
16
+ point = RGeo::Geographic.spherical_factory.point 1, 2
17
+ bar = Bar.create!(location: point)
18
+ bar.location.x.should be_within(0.1).of(1)
19
+ bar.location.y.should be_within(0.1).of(2)
20
+ end
21
+
22
+ it "should calculate distance between points" do
23
+ bar = Bar.create!(location: [5,5])
24
+ bar2 = Bar.create!(location: [15,15])
25
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
26
+ end
27
+
28
+ it "should calculate distance between points miles" do
29
+ pending
30
+ bar = Bar.create!(location: [5,5])
31
+ bar2 = Bar.create!(location: [15,15])
32
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
33
+ end
34
+
35
+ it "should calculate 3d distances by default" do
36
+ bar = Bar.create! location: [-73.77694444, 40.63861111 ]
37
+ bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
38
+ bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
39
+ end
40
+
41
+ it "should have a nice simple way to ovewrite geo factory" do
42
+ pending
43
+ bar = Bar.create!(location: [5,5])
44
+ bar2 = Bar.create!(location: [15,15])
45
+ bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
46
+ end
47
+
48
+ it "should have a field mapped as polygon" do
49
+ farm = Farm.create!(area: [[5,5],[6,5],[6,6],[5,6]])
50
+ farm.area.should be_a RGeo::Geographic::SphericalPolygonImpl
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial::GeoNearResults do
4
+ before do
5
+ Bar.delete_all
6
+ Bar.create_indexes
7
+
8
+ 50.times do |i|
9
+ Bar.create!(:name => i.to_s, :location => [rand(358)-179,rand(358)-179])
10
+ end
11
+ end
12
+
13
+
14
+
15
+ context ":paginator :array" do
16
+ let(:bars) { Bar.geo_near([1,1]) }
17
+ let(:sorted_bars) {
18
+ bars = Bar.geo_near([1,1])
19
+ bars.sort_by! {|b| b.name.to_i}
20
+ bars
21
+ }
22
+ [nil,1,2].each do |page|
23
+ it "page=#{page} should have 25" do
24
+ bars.page(page).size.should == 25
25
+ end
26
+ end
27
+
28
+ [1,2].each do |page|
29
+ it "modified result should keep order after pagination" do
30
+ sorted_bars.page(page).should == sorted_bars.slice((page-1)*25,25)
31
+ end
32
+ end
33
+
34
+ { nil => 25, 20 => 20 , 30 => 20, 50 => 0}.each do |per, total|
35
+ it "page=2 per=#{per} should have #{total}" do
36
+ bars.per(per).page(2).size.should == total
37
+ bars.page(2).per(per).size.should == total
38
+ end
39
+ end
40
+
41
+ it "page=3 should have 0" do
42
+ bars.page(3).size.should == 0
43
+ end
44
+
45
+ it "per=5" do
46
+ bars.per(5).size.should == 5
47
+ end
48
+
49
+ it "page=10 per=5" do
50
+ bars.per(5).page(10).should == bars[45..50]
51
+ end
52
+
53
+ end
54
+
55
+ context ":paginator :kaminari" do
56
+ let!(:near) {Bar.geo_near([1,1]).page(1)}
57
+ it "should have current_page" do
58
+ near.current_page.should == 1
59
+ end
60
+
61
+ it "should have num_pages" do
62
+ near.total_entries.should == 50
63
+ near.num_pages.should == 2
64
+ end
65
+
66
+ it "should have limit_value" do
67
+ near.limit_value.should == 25
68
+ end
69
+ end
70
+
71
+
72
+ context ":paginator :num_pages" do
73
+ it "when total=55 per=10 ,num_pages should be 6" do
74
+ 5.times { |i| Bar.create(:name => i.to_s, :location => [rand(358)-179,rand(358)-179]) }
75
+ Bar.geo_near([1,1]).per(10).num_pages.should == 6
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,19 @@
1
+ class Account
2
+ include Mongoid::Document
3
+ field :number, :type => String
4
+ field :balance, :type => String
5
+ field :nickname, :type => String
6
+ field :name, :type => String
7
+ key :name
8
+
9
+ embeds_many :memberships
10
+ referenced_in :creator, :class_name => "User", :foreign_key => :creator_id
11
+ referenced_in :person
12
+ references_many :alerts
13
+ references_and_referenced_in_many :agents
14
+
15
+ attr_accessible :nickname, :name
16
+
17
+ validates_presence_of :name
18
+ validates_length_of :name, :maximum => 10, :on => :create
19
+ end
@@ -0,0 +1,9 @@
1
+ class Acolyte
2
+ include Mongoid::Document
3
+ field :status
4
+ field :name
5
+
6
+ scope :active, where(:status => "active")
7
+
8
+ default_scope asc(:name)
9
+ end
@@ -0,0 +1,62 @@
1
+ class Address
2
+ include Mongoid::Document
3
+
4
+ attr_accessor :mode
5
+
6
+ field :address_type
7
+ field :number, :type => Integer
8
+ field :street
9
+ field :city
10
+ field :state
11
+ field :post_code
12
+ field :parent_title
13
+ field :services, :type => Array
14
+ field :latlng, :type => Array
15
+ key :street
16
+ embeds_many :locations
17
+
18
+ embedded_in :addressable, :polymorphic => true do
19
+ def extension
20
+ "Testing"
21
+ end
22
+ def doctor?
23
+ title == "Dr"
24
+ end
25
+ end
26
+
27
+ accepts_nested_attributes_for :locations
28
+
29
+ referenced_in :account
30
+
31
+ scope :without_postcode, where(:postcode => nil)
32
+ named_scope :rodeo, where(:street => "Rodeo Dr") do
33
+ def mansion?
34
+ all? { |address| address.street == "Rodeo Dr" }
35
+ end
36
+ end
37
+
38
+ validates_presence_of :street, :on => :update
39
+ validates_format_of :street, :with => /\D/, :allow_nil => true
40
+
41
+ def set_parent=(set = false)
42
+ self.parent_title = addressable.title if set
43
+ end
44
+
45
+ def <=>(other)
46
+ street <=> other.street
47
+ end
48
+
49
+ class << self
50
+ def california
51
+ where(:state => "CA")
52
+ end
53
+
54
+ def homes
55
+ where(:address_type => "Home")
56
+ end
57
+
58
+ def streets
59
+ all.map(&:street)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ class AddressComponent
2
+ include Mongoid::Document
3
+ field :street, :type => String
4
+ embedded_in :person
5
+ end
@@ -0,0 +1,10 @@
1
+ class Agent
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Updated
4
+ field :title
5
+ field :number
6
+ embeds_many :names, :as => :namable
7
+ referenced_in :game
8
+
9
+ references_and_referenced_in_many :accounts
10
+ end
@@ -0,0 +1,5 @@
1
+ class Alert
2
+ include Mongoid::Document
3
+ field :message, :type => String
4
+ referenced_in :account
5
+ end
@@ -0,0 +1,21 @@
1
+ class Animal
2
+ include Mongoid::Document
3
+ field :name
4
+ field :tags, :type => Array
5
+ key :name
6
+
7
+ embedded_in :person
8
+ embedded_in :circus
9
+
10
+ validates_format_of :name, :without => /\$\$\$/
11
+
12
+ accepts_nested_attributes_for :person
13
+
14
+ def tag_list
15
+ tags.join(", ")
16
+ end
17
+
18
+ def tag_list=(_tag_list)
19
+ self.tags = _tag_list.split(",").map(&:strip)
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ class Answer
2
+ include Mongoid::Document
3
+ embedded_in :question
4
+ end
@@ -0,0 +1,9 @@
1
+ class Bar
2
+ include Mongoid::Document
3
+ include Mongoid::Geospatial
4
+
5
+ field :name, :type => String
6
+ field :location, :type => Point, :spatial => true
7
+ references_one :rating, :as => :ratable
8
+ spatial_index :location
9
+ end
@@ -0,0 +1,13 @@
1
+ class Birthday
2
+ include Mongoid::Document
3
+ field :title
4
+ field :date, :type => Date
5
+ embedded_in :owner, :inverse_of => :birthdays
6
+
7
+ def self.each_day(start_date, end_date)
8
+ groups = only(:date).asc(:date).where(:date.gte => start_date, :date.lte => end_date).group
9
+ groups.each do |date, group|
10
+ yield(date, group)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ class Book
2
+ include Mongoid::Document
3
+ field :title, :type => String
4
+ references_one :rating, :as => :ratable, :dependent => :nullify
5
+ end
@@ -0,0 +1,7 @@
1
+ class Business
2
+ include Mongoid::Document
3
+
4
+ set_database :secondary
5
+
6
+ field :name
7
+ end