djsun-mongo_mapper 0.5.6.6 → 0.5.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.gitignore +3 -1
  2. data/Rakefile +13 -8
  3. data/VERSION +1 -1
  4. data/djsun-mongo_mapper.gemspec +17 -21
  5. data/lib/mongo_mapper/associations/base.rb +32 -36
  6. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +0 -2
  7. data/lib/mongo_mapper/associations/many_documents_proxy.rb +19 -10
  8. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +2 -2
  9. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +21 -36
  10. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +1 -1
  11. data/lib/mongo_mapper/associations/proxy.rb +3 -2
  12. data/lib/mongo_mapper/associations.rb +114 -8
  13. data/lib/mongo_mapper/callbacks.rb +18 -0
  14. data/lib/mongo_mapper/document.rb +173 -37
  15. data/lib/mongo_mapper/dynamic_finder.rb +1 -1
  16. data/lib/mongo_mapper/embedded_document.rb +9 -13
  17. data/lib/mongo_mapper/finder_options.rb +67 -44
  18. data/lib/mongo_mapper/pagination.rb +2 -0
  19. data/lib/mongo_mapper/serialization.rb +1 -1
  20. data/lib/mongo_mapper/serializers/json_serializer.rb +1 -1
  21. data/lib/mongo_mapper/support.rb +9 -0
  22. data/lib/mongo_mapper/validations.rb +12 -42
  23. data/lib/mongo_mapper.rb +11 -5
  24. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  25. data/test/functional/associations/test_belongs_to_proxy.rb +29 -31
  26. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  27. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +27 -3
  28. data/test/functional/associations/test_many_embedded_proxy.rb +58 -38
  29. data/test/functional/associations/test_many_polymorphic_proxy.rb +45 -3
  30. data/test/functional/associations/test_many_proxy.rb +61 -11
  31. data/test/functional/test_associations.rb +3 -3
  32. data/test/functional/test_binary.rb +1 -1
  33. data/test/functional/test_callbacks.rb +1 -1
  34. data/test/functional/test_dirty.rb +3 -3
  35. data/test/functional/test_document.rb +62 -58
  36. data/test/functional/test_embedded_document.rb +1 -1
  37. data/test/functional/test_pagination.rb +1 -1
  38. data/test/functional/test_rails_compatibility.rb +1 -1
  39. data/test/functional/test_validations.rb +46 -14
  40. data/test/models.rb +87 -35
  41. data/test/support/{test_timing.rb → timing.rb} +1 -1
  42. data/test/test_helper.rb +8 -13
  43. data/test/unit/serializers/test_json_serializer.rb +0 -4
  44. data/test/unit/test_association_base.rb +24 -8
  45. data/test/unit/test_document.rb +40 -71
  46. data/test/unit/test_embedded_document.rb +27 -67
  47. data/test/unit/test_finder_options.rb +16 -0
  48. data/test/unit/test_key.rb +5 -17
  49. data/test/unit/test_mongomapper.rb +2 -2
  50. data/test/unit/test_pagination.rb +4 -0
  51. metadata +10 -12
  52. data/mongo_mapper.gemspec +0 -170
  53. data/test/functional/associations/test_namespace.rb +0 -27
@@ -30,7 +30,7 @@ class CallbacksTest < Test::Unit::TestCase
30
30
  @history = nil
31
31
  end
32
32
  end
33
- @document.collection.clear
33
+ @document.collection.remove
34
34
  end
35
35
 
36
36
  should "get the order right for creating documents" do
@@ -8,10 +8,10 @@ class DirtyTest < Test::Unit::TestCase
8
8
  set_collection_name 'test'
9
9
  key :phrase, String
10
10
  end
11
- @document.collection.clear
11
+ @document.collection.remove
12
12
 
13
- Status.collection.clear
14
- Project.collection.clear
13
+ Status.collection.remove
14
+ Project.collection.remove
15
15
  end
16
16
 
17
17
  context "marking changes" do
@@ -9,28 +9,10 @@ class DocumentTest < Test::Unit::TestCase
9
9
 
10
10
  key :first_name, String
11
11
  key :last_name, String
12
- key :nick_name, String, :default => ""
13
12
  key :age, Integer
14
13
  key :date, Date
15
14
  end
16
- @document.collection.clear
17
- end
18
-
19
- context "Accessing attributes on a document" do
20
- setup do
21
- @doc = @document.new(:first_name => 'John', :age => '27')
22
- end
23
-
24
- should "access existing attributes" do
25
- @doc[:first_name].should == 'John'
26
- @doc[:age].should == 27
27
- end
28
-
29
- should "raise an exception for non-existing attributes" do
30
- lambda {
31
- @doc[:not_here].should == nil
32
- }.should raise_error(MongoMapper::KeyNotFound)
33
- end
15
+ @document.collection.remove
34
16
  end
35
17
 
36
18
  context "Saving a document with a custom id" do
@@ -212,7 +194,7 @@ class DocumentTest < Test::Unit::TestCase
212
194
  include MongoMapper::Document
213
195
  set_collection_name 'test'
214
196
  end
215
- @document.collection.clear
197
+ @document.collection.remove
216
198
  end
217
199
 
218
200
  should "create the document" do
@@ -306,8 +288,12 @@ class DocumentTest < Test::Unit::TestCase
306
288
  @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
307
289
  end
308
290
 
309
- should "raise document not found if nothing provided" do
310
- lambda { @document.find }.should raise_error(MongoMapper::DocumentNotFound)
291
+ should "return nil if nothing provided for find" do
292
+ @document.find.should be_nil
293
+ end
294
+
295
+ should "raise document not found if nothing provided for find!" do
296
+ lambda { @document.find! }.should raise_error(MongoMapper::DocumentNotFound)
311
297
  end
312
298
 
313
299
  context "with a single id" do
@@ -315,9 +301,13 @@ class DocumentTest < Test::Unit::TestCase
315
301
  @document.find(@doc1.id).should == @doc1
316
302
  end
317
303
 
318
- should "raise error if document not found" do
304
+ should "return nil if document not found with find" do
305
+ @document.find(123).should be_nil
306
+ end
307
+
308
+ should "raise error if document not found with find!" do
319
309
  lambda {
320
- @document.find(123)
310
+ @document.find!(123)
321
311
  }.should raise_error(MongoMapper::DocumentNotFound)
322
312
  end
323
313
  end
@@ -587,13 +577,13 @@ class DocumentTest < Test::Unit::TestCase
587
577
  class ::Property
588
578
  include MongoMapper::Document
589
579
  end
590
- Property.collection.clear
580
+ Property.collection.remove
591
581
 
592
582
  class ::Thing
593
583
  include MongoMapper::Document
594
584
  key :name, String
595
585
  end
596
- Thing.collection.clear
586
+ Thing.collection.remove
597
587
  end
598
588
 
599
589
  teardown do
@@ -719,7 +709,7 @@ class DocumentTest < Test::Unit::TestCase
719
709
  include MongoMapper::Document
720
710
  set_collection_name 'foobarbazwickdoesnotexist'
721
711
  end
722
- @document.collection.clear
712
+ @document.collection.remove
723
713
 
724
714
  klass.count.should == 0
725
715
  end
@@ -755,8 +745,8 @@ class DocumentTest < Test::Unit::TestCase
755
745
  should "allow creating index on multiple keys" do
756
746
  @document.ensure_index [[:first_name, 1], [:last_name, -1]]
757
747
  MongoMapper.ensure_indexes!
758
-
759
- # order is different for different versions of ruby so instead of
748
+
749
+ # order is different for different versions of ruby so instead of
760
750
  # just checking have_index('first_name_1_last_name_-1') I'm checking
761
751
  # the values of the indexes to make sure the index creation was successful
762
752
  @document.collection.index_information.detect do |index|
@@ -794,10 +784,6 @@ class DocumentTest < Test::Unit::TestCase
794
784
  @doc.age.should == 27
795
785
  end
796
786
 
797
- should "set defaults correctly" do
798
- @doc.nick_name.should == ""
799
- end
800
-
801
787
  should "update attributes in the database" do
802
788
  from_db = @document.find(@doc.id)
803
789
  from_db.should == @doc
@@ -974,12 +960,12 @@ class DocumentTest < Test::Unit::TestCase
974
960
  key :_type, String
975
961
  key :name, String
976
962
  end
977
- DocParent.collection.clear
978
-
963
+ DocParent.collection.remove
964
+
979
965
  class ::DocDaughter < ::DocParent; end
980
966
  class ::DocSon < ::DocParent; end
981
967
  class ::DocGrandSon < ::DocSon; end
982
-
968
+
983
969
  @parent = DocParent.new({:name => "Daddy Warbucks"})
984
970
  @daughter = DocDaughter.new({:name => "Little Orphan Annie"})
985
971
  end
@@ -1003,7 +989,7 @@ class DocumentTest < Test::Unit::TestCase
1003
989
  should "load the document with the assigned type" do
1004
990
  @parent.save
1005
991
  @daughter.save
1006
-
992
+
1007
993
  collection = DocParent.find(:all)
1008
994
  collection.size.should == 2
1009
995
  collection.first.should be_kind_of(DocParent)
@@ -1021,92 +1007,98 @@ class DocumentTest < Test::Unit::TestCase
1021
1007
  collection.last.should == doc
1022
1008
  collection.last.should be_kind_of(DocParent)
1023
1009
  end
1024
-
1010
+
1025
1011
  should "find scoped to class" do
1026
1012
  john = DocSon.create(:name => 'John')
1027
1013
  steve = DocSon.create(:name => 'Steve')
1028
1014
  steph = DocDaughter.create(:name => 'Steph')
1029
1015
  carrie = DocDaughter.create(:name => 'Carrie')
1030
-
1016
+
1031
1017
  DocGrandSon.all(:order => 'name').should == []
1032
1018
  DocSon.all(:order => 'name').should == [john, steve]
1033
1019
  DocDaughter.all(:order => 'name').should == [carrie, steph]
1034
1020
  DocParent.all(:order => 'name').should == [carrie, john, steph, steve]
1035
1021
  end
1036
1022
 
1023
+ should "work with nested hash conditions" do
1024
+ john = DocSon.create(:name => 'John')
1025
+ steve = DocSon.create(:name => 'Steve')
1026
+ DocSon.all(:name => {'$ne' => 'Steve'}).should == [john]
1027
+ end
1028
+
1037
1029
  should "raise error if not found scoped to class" do
1038
1030
  john = DocSon.create(:name => 'John')
1039
1031
  steph = DocDaughter.create(:name => 'Steph')
1040
-
1032
+
1041
1033
  lambda {
1042
- DocSon.find(steph.id)
1034
+ DocSon.find!(steph.id)
1043
1035
  }.should raise_error(MongoMapper::DocumentNotFound)
1044
1036
  end
1045
-
1037
+
1046
1038
  should "not raise error for find with parent" do
1047
1039
  john = DocSon.create(:name => 'John')
1048
-
1049
- DocParent.find(john.id).should == john
1040
+
1041
+ DocParent.find!(john.id).should == john
1050
1042
  end
1051
-
1043
+
1052
1044
  should "count scoped to class" do
1053
1045
  john = DocSon.create(:name => 'John')
1054
1046
  steve = DocSon.create(:name => 'Steve')
1055
1047
  steph = DocDaughter.create(:name => 'Steph')
1056
1048
  carrie = DocDaughter.create(:name => 'Carrie')
1057
-
1049
+
1058
1050
  DocGrandSon.count.should == 0
1059
1051
  DocSon.count.should == 2
1060
1052
  DocDaughter.count.should == 2
1061
1053
  DocParent.count.should == 4
1062
1054
  end
1063
-
1055
+
1064
1056
  should "know if it is single_collection_inherited?" do
1065
1057
  DocParent.single_collection_inherited?.should be_false
1066
-
1058
+
1067
1059
  DocDaughter.single_collection_inherited?.should be_true
1068
1060
  DocSon.single_collection_inherited?.should be_true
1069
1061
  end
1070
-
1062
+
1071
1063
  should "know if single_collection_inherited_superclass?" do
1072
1064
  DocParent.single_collection_inherited_superclass?.should be_false
1073
-
1065
+
1074
1066
  DocDaughter.single_collection_inherited_superclass?.should be_true
1075
1067
  DocSon.single_collection_inherited_superclass?.should be_true
1076
1068
  DocGrandSon.single_collection_inherited_superclass?.should be_true
1077
1069
  end
1078
-
1070
+
1079
1071
  should "not be able to destroy each other" do
1080
1072
  john = DocSon.create(:name => 'John')
1081
1073
  steph = DocDaughter.create(:name => 'Steph')
1082
-
1074
+
1083
1075
  lambda {
1084
1076
  DocSon.destroy(steph.id)
1085
1077
  }.should raise_error(MongoMapper::DocumentNotFound)
1086
1078
  end
1087
-
1079
+
1088
1080
  should "not be able to delete each other" do
1089
1081
  john = DocSon.create(:name => 'John')
1090
1082
  steph = DocDaughter.create(:name => 'Steph')
1091
-
1083
+
1092
1084
  lambda {
1093
1085
  DocSon.delete(steph.id)
1094
1086
  }.should_not change { DocParent.count }
1095
1087
  end
1096
-
1088
+
1097
1089
  should "be able to destroy using parent" do
1098
1090
  john = DocSon.create(:name => 'John')
1099
1091
  steph = DocDaughter.create(:name => 'Steph')
1100
-
1092
+
1101
1093
  lambda {
1102
1094
  DocParent.destroy_all
1103
1095
  }.should change { DocParent.count }.by(-2)
1104
1096
  end
1105
-
1097
+
1106
1098
  should "be able to delete using parent" do
1107
1099
  john = DocSon.create(:name => 'John')
1108
1100
  steph = DocDaughter.create(:name => 'Steph')
1109
-
1101
+
1110
1102
  lambda {
1111
1103
  DocParent.delete_all
1112
1104
  }.should change { DocParent.count }.by(-2)
@@ -1178,4 +1170,16 @@ class DocumentTest < Test::Unit::TestCase
1178
1170
  @document.exists?(:first_name => "Jean").should == false
1179
1171
  end
1180
1172
  end
1173
+
1174
+ context "reload" do
1175
+ setup do
1176
+ @doc_instance_1 = @document.create({:first_name => 'Ryan', :last_name => 'Koopmans', :age => '37'})
1177
+ @doc_instance_2 = @document.update(@doc_instance_1.id, {:age => '39'})
1178
+ end
1179
+
1180
+ should "load fresh information from the database" do
1181
+ @doc_instance_1.age.should == 37
1182
+ @doc_instance_1.reload.age.should == 39
1183
+ end
1184
+ end
1181
1185
  end
@@ -10,7 +10,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
10
10
  key :first_name, String
11
11
  key :last_name, String
12
12
  end
13
- @document.collection.clear
13
+ @document.collection.remove
14
14
  end
15
15
 
16
16
  context "Saving a document with an embedded document" do
@@ -13,7 +13,7 @@ class PaginationTest < Test::Unit::TestCase
13
13
 
14
14
  def self.per_page; 1 end
15
15
  end
16
- @document.collection.clear
16
+ @document.collection.remove
17
17
 
18
18
  @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
19
19
  @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@@ -14,7 +14,7 @@ class TestRailsCompatibility < Test::Unit::TestCase
14
14
 
15
15
  context "Document" do
16
16
  setup do
17
- Order.collection.clear
17
+ Order.collection.remove
18
18
  end
19
19
 
20
20
  should "alias new to new_record?" do
@@ -8,7 +8,7 @@ class ValidationsTest < Test::Unit::TestCase
8
8
  set_collection_name 'test'
9
9
  key :name, String, :required => true
10
10
  end
11
- @document.collection.clear
11
+ @document.collection.remove
12
12
  end
13
13
 
14
14
  should "not insert document" do
@@ -32,7 +32,7 @@ class ValidationsTest < Test::Unit::TestCase
32
32
  set_collection_name 'test'
33
33
  key :name, String, :required => true
34
34
  end
35
- @document.collection.clear
35
+ @document.collection.remove
36
36
  end
37
37
 
38
38
  should "raise error" do
@@ -48,7 +48,7 @@ class ValidationsTest < Test::Unit::TestCase
48
48
  set_collection_name 'test'
49
49
  key :name, String, :required => true
50
50
  end
51
- @document.collection.clear
51
+ @document.collection.remove
52
52
  end
53
53
 
54
54
  should "raise error" do
@@ -68,7 +68,7 @@ class ValidationsTest < Test::Unit::TestCase
68
68
  set_collection_name 'test'
69
69
  key :name, String, :required => true
70
70
  end
71
- @document.collection.clear
71
+ @document.collection.remove
72
72
 
73
73
  @doc = @document.create(:name => 'John Nunemaker')
74
74
  end
@@ -97,7 +97,7 @@ class ValidationsTest < Test::Unit::TestCase
97
97
  errors.add(:action, 'is invalid') if action.blank?
98
98
  end
99
99
  end
100
- @document.collection.clear
100
+ @document.collection.remove
101
101
  end
102
102
 
103
103
  should "work with validate_on_create callback" do
@@ -140,7 +140,7 @@ class ValidationsTest < Test::Unit::TestCase
140
140
  key :name, String
141
141
  validates_uniqueness_of :name
142
142
  end
143
- @document.collection.clear
143
+ @document.collection.remove
144
144
  end
145
145
 
146
146
  should "not fail if object is new" do
@@ -212,7 +212,39 @@ class ValidationsTest < Test::Unit::TestCase
212
212
  doc2 = document.new("name" => "")
213
213
  doc2.should_not have_error_on(:name)
214
214
  end
215
-
215
+
216
+ should "allow entries that differ only in case by default" do
217
+ document = Class.new do
218
+ include MongoMapper::Document
219
+ set_collection_name 'test'
220
+
221
+ key :name
222
+ validates_uniqueness_of :name
223
+ end
224
+
225
+ doc = document.new("name" => "BLAMMO")
226
+ doc.save.should be_true
227
+
228
+ doc2 = document.new("name" => "blammo")
229
+ doc2.should_not have_error_on(:name)
230
+ end
231
+
232
+ should "fail on entries that differ only in case if :case_sensitive => false" do
233
+ document = Class.new do
234
+ include MongoMapper::Document
235
+ set_collection_name 'test'
236
+
237
+ key :name
238
+ validates_uniqueness_of :name, :case_sensitive => false
239
+ end
240
+
241
+ doc = document.new("name" => "BLAMMO")
242
+ doc.save.should be_true
243
+
244
+ doc2 = document.new("name" => "blammo")
245
+ doc2.should have_error_on(:name)
246
+ end
247
+
216
248
  context "scoped by a single attribute" do
217
249
  setup do
218
250
  @document = Class.new do
@@ -223,9 +255,9 @@ class ValidationsTest < Test::Unit::TestCase
223
255
  key :scope, String
224
256
  validates_uniqueness_of :name, :scope => :scope
225
257
  end
226
- @document.collection.clear
258
+ @document.collection.remove
227
259
  end
228
-
260
+
229
261
  should "fail if the same name exists in the scope" do
230
262
  doc = @document.new("name" => "joe", "scope" => "one")
231
263
  doc.save.should be_true
@@ -238,11 +270,11 @@ class ValidationsTest < Test::Unit::TestCase
238
270
  doc2 = @document.new("name" => "joe", "scope" => "one")
239
271
  doc2.should have_error_on(:name)
240
272
  end
241
-
273
+
242
274
  should "pass if the same name exists in a different scope" do
243
275
  doc = @document.new("name" => "joe", "scope" => "one")
244
276
  doc.save.should be_true
245
-
277
+
246
278
  @document \
247
279
  .stubs(:first) \
248
280
  .with(:name => 'joe', :scope => 'two') \
@@ -252,7 +284,7 @@ class ValidationsTest < Test::Unit::TestCase
252
284
  doc2.should_not have_error_on(:name)
253
285
  end
254
286
  end
255
-
287
+
256
288
  context "scoped by a multiple attributes" do
257
289
  setup do
258
290
  @document = Class.new do
@@ -264,7 +296,7 @@ class ValidationsTest < Test::Unit::TestCase
264
296
  key :second_scope, String
265
297
  validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
266
298
  end
267
- @document.collection.clear
299
+ @document.collection.remove
268
300
  end
269
301
 
270
302
  should "fail if the same name exists in the scope" do
@@ -303,7 +335,7 @@ class ValidationsTest < Test::Unit::TestCase
303
335
 
304
336
  key :name, String, :unique => true
305
337
  end
306
- @document.collection.clear
338
+ @document.collection.remove
307
339
 
308
340
  doc = @document.create(:name => 'John')
309
341
  doc.should_not have_error_on(:name)
data/test/models.rb CHANGED
@@ -23,14 +23,13 @@ class WindowSize
23
23
  end
24
24
  end
25
25
 
26
-
27
26
  class Post
28
27
  include MongoMapper::Document
29
28
 
30
29
  key :title, String
31
30
  key :body, String
32
31
 
33
- has_many :comments, :as => :commentable, :class_name => 'PostComment'
32
+ many :comments, :as => :commentable, :class_name => 'PostComment'
34
33
 
35
34
  timestamps!
36
35
  end
@@ -72,11 +71,36 @@ class Enter < Message; end
72
71
  class Exit < Message; end
73
72
  class Chat < Message; end
74
73
 
74
+ module AccountsExtensions
75
+ def inactive
76
+ all(:last_logged_in => nil)
77
+ end
78
+ end
79
+
80
+ class Account
81
+ include MongoMapper::Document
82
+
83
+ key :_type, String
84
+ key :room_id, String
85
+ key :last_logged_in, Time
86
+
87
+ belongs_to :room
88
+ end
89
+ class User < Account; end
90
+ class Bot < Account; end
91
+
75
92
  class Room
76
93
  include MongoMapper::Document
77
94
 
78
95
  key :name, String
79
- many :messages, :polymorphic => true
96
+ many :messages, :polymorphic => true do
97
+ def older
98
+ all(:position => {'$gt' => 5})
99
+ end
100
+ end
101
+ many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
102
+
103
+ many :accounts, :polymorphic => true, :extend => AccountsExtensions
80
104
  end
81
105
 
82
106
  class Answer
@@ -85,12 +109,45 @@ class Answer
85
109
  key :body, String
86
110
  end
87
111
 
112
+ module PeopleExtensions
113
+ def find_by_name(name)
114
+ detect { |p| p.name == name }
115
+ end
116
+ end
117
+
118
+ module CollaboratorsExtensions
119
+ def top
120
+ first
121
+ end
122
+ end
123
+
88
124
  class Project
89
125
  include MongoMapper::Document
90
126
 
91
127
  key :name, String
92
- many :statuses
93
- many :addresses
128
+
129
+ many :people, :extend => PeopleExtensions
130
+ many :collaborators, :extend => CollaboratorsExtensions
131
+
132
+ many :statuses, :order => 'position' do
133
+ def open
134
+ all(:name => %w(New Assigned))
135
+ end
136
+ end
137
+
138
+ many :addresses do
139
+ def find_all_by_state(state)
140
+ # can't use select here for some reason
141
+ find_all { |a| a.state == state }
142
+ end
143
+ end
144
+ end
145
+
146
+ class Collaborator
147
+ include MongoMapper::Document
148
+ key :project_id, String
149
+ key :name, String
150
+ belongs_to :project
94
151
  end
95
152
 
96
153
  class Status
@@ -99,7 +156,7 @@ class Status
99
156
  key :project_id, String
100
157
  key :target_id, String
101
158
  key :target_type, String
102
- key :name, String
159
+ key :name, String, :required => true
103
160
  key :position, Integer
104
161
 
105
162
  belongs_to :project
@@ -109,9 +166,13 @@ end
109
166
  class RealPerson
110
167
  include MongoMapper::Document
111
168
 
112
- many :pets
169
+ key :room_id, String
113
170
  key :name, String
114
-
171
+
172
+ belongs_to :room
173
+
174
+ many :pets
175
+
115
176
  def realname=(n)
116
177
  self.name = n
117
178
  end
@@ -138,6 +199,8 @@ class Media
138
199
 
139
200
  key :_type, String
140
201
  key :file, String
202
+
203
+ key :visible, Boolean
141
204
  end
142
205
 
143
206
  class Video < Media
@@ -155,8 +218,13 @@ end
155
218
 
156
219
  class Catalog
157
220
  include MongoMapper::Document
158
-
159
- many :medias, :polymorphic => true
221
+
222
+ many :medias, :polymorphic => true do
223
+ def visible
224
+ # for some reason we can't use select here
225
+ find_all { |m| m.visible? }
226
+ end
227
+ end
160
228
  end
161
229
 
162
230
  module TrModels
@@ -165,6 +233,7 @@ module TrModels
165
233
 
166
234
  key :_type, String
167
235
  key :license_plate, String
236
+ key :purchased_on, Date
168
237
  end
169
238
 
170
239
  class Car < TrModels::Transport
@@ -189,31 +258,14 @@ module TrModels
189
258
  class Fleet
190
259
  include MongoMapper::Document
191
260
 
192
- many :transports, :polymorphic => true, :class_name => "TrModels::Transport"
193
- key :name, String
194
- end
195
- end
196
-
197
- module Hollywood
198
- class Movie
199
- include MongoMapper::Document
200
-
201
- many :roles
202
- end
203
-
204
- class Actor
205
- include MongoMapper::Document
261
+ module TransportsExtension
262
+ def to_be_replaced
263
+ # for some reason we can't use select
264
+ find_all { |t| t.purchased_on < 2.years.ago.to_date }
265
+ end
266
+ end
206
267
 
207
- many :roles
208
- end
209
-
210
- class Role
211
- include MongoMapper::Document
212
-
213
- key :movie_id, String
214
- key :actor_id, String
215
-
216
- belongs_to :movie
217
- belongs_to :actor
268
+ many :transports, :polymorphic => true, :class_name => "TrModels::Transport", :extend => TransportsExtension
269
+ key :name, String
218
270
  end
219
271
  end
@@ -5,7 +5,7 @@ class Test::Unit::TestCase
5
5
  end_time = Time.now
6
6
 
7
7
  duration = end_time - begin_time
8
- threshold = 0.3
8
+ threshold = 0.5
9
9
 
10
10
  if duration > threshold
11
11
  puts "\nSLOW TEST: #{duration} - #{self.name}"