mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -11,7 +11,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
11
11
  key :title, String
12
12
  end
13
13
  @post_class.many :comments, :class => @comment_class
14
-
14
+
15
15
  @pet_class = EDoc do
16
16
  key :name, String
17
17
  end
@@ -21,17 +21,17 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
21
21
  end
22
22
  @person_class.key :child, @person_class
23
23
  @person_class.many :pets, :class => @pet_class
24
-
24
+
25
25
  @owner_class = Doc do
26
26
  key :name, String
27
27
  end
28
28
  @owner_class.many :pets, :class => @pet_class
29
29
  end
30
-
30
+
31
31
  should "default reader to empty array" do
32
32
  @post_class.new.comments.should == []
33
33
  end
34
-
34
+
35
35
  should "allow adding to association like it was an array" do
36
36
  post = @post_class.new
37
37
  post.comments << @comment_class.new
@@ -54,33 +54,33 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
54
54
  post.comments[1].should == bill
55
55
  post.comments[1].new?.should == false
56
56
  end
57
-
57
+
58
58
  should "allow embedding arbitrarily deep" do
59
59
  @klass = Doc()
60
60
  @klass.key :person, @person_class
61
-
61
+
62
62
  meg = @person_class.new(:name => 'Meg')
63
63
  meg.child = @person_class.new(:name => 'Steve')
64
64
  meg.child.child = @person_class.new(:name => 'Linda')
65
-
65
+
66
66
  doc = @klass.new(:person => meg)
67
67
  doc.save
68
68
  doc.reload
69
-
69
+
70
70
  doc.person.name.should == 'Meg'
71
71
  doc.person.child.name.should == 'Steve'
72
72
  doc.person.child.child.name.should == 'Linda'
73
73
  end
74
-
74
+
75
75
  should "allow assignment of many embedded documents using a hash" do
76
- person_attributes = {
77
- 'name' => 'Mr. Pet Lover',
76
+ person_attributes = {
77
+ 'name' => 'Mr. Pet Lover',
78
78
  'pets' => [
79
79
  {'name' => 'Jimmy', 'species' => 'Cocker Spainel'},
80
- {'name' => 'Sasha', 'species' => 'Siberian Husky'},
81
- ]
80
+ {'name' => 'Sasha', 'species' => 'Siberian Husky'},
81
+ ]
82
82
  }
83
-
83
+
84
84
  owner = @owner_class.new(person_attributes)
85
85
  owner.name.should == 'Mr. Pet Lover'
86
86
  owner.pets[0].name.should == 'Jimmy'
@@ -109,12 +109,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
109
109
  @pet_class.new(:name => 'Sparky', :species => 'Dog'),
110
110
  @pet_class.new(:name => 'Koda', :species => 'Dog')
111
111
  ])
112
-
112
+
113
113
  doc = @klass.new
114
114
  doc.people << meg
115
115
  doc.save
116
116
  doc.reload
117
-
117
+
118
118
  doc.people.first.name.should == 'Meg'
119
119
  doc.people.first.pets.should_not == []
120
120
  doc.people.first.pets.first.name.should == 'Sparky'
@@ -127,7 +127,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
127
127
  doc = @klass.new
128
128
  meg = @person_class.new(:name => 'Meg')
129
129
  pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
130
-
130
+
131
131
  doc.people << meg
132
132
  meg.pets << pet
133
133
 
@@ -138,7 +138,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
138
138
  doc = @klass.new
139
139
  meg = @person_class.new(:name => 'Meg')
140
140
  pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
141
-
141
+
142
142
  doc.people << meg
143
143
  meg.pets << pet
144
144
 
@@ -161,7 +161,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
161
161
  doc = @klass.new
162
162
  meg = @person_class.new(:name => 'Meg')
163
163
  pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
164
-
164
+
165
165
  doc.people << meg
166
166
  meg.pets << pet
167
167
  doc.save
@@ -175,7 +175,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
175
175
  doc = @klass.new
176
176
  meg = @person_class.new(:name => 'Meg')
177
177
  pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
178
-
178
+
179
179
  doc.people << meg
180
180
  meg.pets << pet
181
181
  doc.save
@@ -184,20 +184,20 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
184
184
  doc.people.first.pets.first.person.should == doc.people.first
185
185
  end
186
186
  end
187
-
187
+
188
188
  should "allow finding by id" do
189
189
  sparky = @pet_class.new(:name => 'Sparky', :species => 'Dog')
190
190
  meg = @owner_class.create(:name => 'Meg', :pets => [sparky])
191
-
191
+
192
192
  meg.pets.find(sparky._id).should == sparky # oid
193
193
  meg.pets.find(sparky.id.to_s).should == sparky # string
194
194
  end
195
-
195
+
196
196
  context "count" do
197
197
  should "default to 0" do
198
198
  @owner_class.new.pets.count.should == 0
199
199
  end
200
-
200
+
201
201
  should "return correct count if any are embedded" do
202
202
  owner = @owner_class.new(:name => 'Meg')
203
203
  owner.pets = [@pet_class.new, @pet_class.new]
@@ -207,7 +207,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
207
207
  owner.pets.count.should == 2
208
208
  end
209
209
  end
210
-
210
+
211
211
  context "extending the association" do
212
212
  setup do
213
213
  @address_class = EDoc do
@@ -216,27 +216,27 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
216
216
  key :state, String
217
217
  key :zip, Integer
218
218
  end
219
-
219
+
220
220
  @project_class = Doc do
221
221
  key :name, String
222
222
  end
223
223
  end
224
-
224
+
225
225
  should "work using a block passed to many" do
226
226
  @project_class.many :addresses, :class => @address_class do
227
227
  def find_all_by_state(state)
228
228
  find_all { |a| a.state == state }
229
229
  end
230
230
  end
231
-
231
+
232
232
  addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
233
233
  addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
234
234
  addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
235
235
  project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
236
-
236
+
237
237
  project.addresses.find_all_by_state("CA").should == [addr1, addr2]
238
238
  end
239
-
239
+
240
240
  should "work using many's :extend option" do
241
241
  module FindByCity
242
242
  def find_by_city(city)
@@ -244,12 +244,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
244
244
  end
245
245
  end
246
246
  @project_class.many :addresses, :class => @address_class, :extend => FindByCity
247
-
247
+
248
248
  addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
249
249
  addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
250
250
  addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
251
251
  project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
252
-
252
+
253
253
  project.addresses.find_by_city('Burbank').should == [addr2]
254
254
  end
255
255
  end
@@ -6,15 +6,15 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
6
6
  Room.collection.remove
7
7
  Message.collection.remove
8
8
  end
9
-
9
+
10
10
  should "default reader to empty array" do
11
11
  Room.new.messages.should == []
12
12
  end
13
-
13
+
14
14
  should "add type key to polymorphic class base" do
15
15
  Message.keys.keys.should include('_type')
16
16
  end
17
-
17
+
18
18
  should "allow adding to assiciation like it was an array" do
19
19
  room = Room.new
20
20
  room.messages << Enter.new
@@ -22,10 +22,10 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
22
22
  room.messages.concat Exit.new
23
23
  room.messages.size.should == 3
24
24
  end
25
-
25
+
26
26
  should "be able to replace the association" do
27
27
  room = Room.create(:name => 'Lounge')
28
-
28
+
29
29
  lambda {
30
30
  room.messages = [
31
31
  Enter.new(:body => 'John entered room', :position => 1),
@@ -33,7 +33,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
33
33
  Exit.new(:body => 'John exited room', :position => 3)
34
34
  ]
35
35
  }.should change { Message.count }.by(3)
36
-
36
+
37
37
  room = room.reload
38
38
  messages = room.messages.all :order => "position"
39
39
  messages.size.should == 3
@@ -41,89 +41,89 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
41
41
  messages[1].body.should == 'Heyyyoooo!'
42
42
  messages[2].body.should == 'John exited room'
43
43
  end
44
-
44
+
45
45
  should "correctly store type when using <<, push and concat" do
46
46
  room = Room.new
47
47
  room.messages << Enter.new(:body => 'John entered the room', :position => 1)
48
48
  room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
49
49
  room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
50
-
50
+
51
51
  room = room.reload
52
52
  messages = room.messages.all :order => "position"
53
53
  messages[0]._type.should == 'Enter'
54
54
  messages[1]._type.should == 'Exit'
55
55
  messages[2]._type.should == 'Chat'
56
56
  end
57
-
57
+
58
58
  context "build" do
59
59
  should "assign foreign key" do
60
60
  room = Room.create
61
61
  message = room.messages.build
62
62
  message.room_id.should == room._id
63
63
  end
64
-
64
+
65
65
  should "assign _type" do
66
66
  room = Room.create
67
67
  message = room.messages.build
68
68
  message._type.should == 'Message'
69
69
  end
70
-
70
+
71
71
  should "allow assigning attributes" do
72
72
  room = Room.create
73
73
  message = room.messages.build(:body => 'Foo!')
74
74
  message.body.should == 'Foo!'
75
75
  end
76
76
  end
77
-
77
+
78
78
  context "create" do
79
79
  should "assign foreign key" do
80
80
  room = Room.create
81
81
  message = room.messages.create
82
82
  message.room_id.should == room._id
83
83
  end
84
-
84
+
85
85
  should "assign _type" do
86
86
  room = Room.create
87
87
  message = room.messages.create
88
88
  message._type.should == 'Message'
89
89
  end
90
-
90
+
91
91
  should "save record" do
92
92
  room = Room.create
93
93
  lambda {
94
94
  room.messages.create
95
95
  }.should change { Message.count }
96
96
  end
97
-
97
+
98
98
  should "allow passing attributes" do
99
99
  room = Room.create
100
100
  message = room.messages.create(:body => 'Foo!')
101
101
  message.body.should == 'Foo!'
102
102
  end
103
103
  end
104
-
104
+
105
105
  context "count" do
106
106
  should "work scoped to association" do
107
107
  room = Room.create
108
108
  3.times { room.messages.create }
109
-
109
+
110
110
  other_room = Room.create
111
111
  2.times { other_room.messages.create }
112
-
112
+
113
113
  room.messages.count.should == 3
114
114
  other_room.messages.count.should == 2
115
115
  end
116
-
116
+
117
117
  should "work with conditions" do
118
118
  room = Room.create
119
119
  room.messages.create(:body => 'Foo')
120
120
  room.messages.create(:body => 'Other 1')
121
121
  room.messages.create(:body => 'Other 2')
122
-
122
+
123
123
  room.messages.count(:body => 'Foo').should == 1
124
124
  end
125
125
  end
126
-
126
+
127
127
  context "Finding scoped to association" do
128
128
  setup do
129
129
  @lounge = Room.create(:name => 'Lounge')
@@ -131,7 +131,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
131
131
  @lm2 = Message.create(:body => 'I love loungin!', :position => 2)
132
132
  @lounge.messages = [@lm1, @lm2]
133
133
  @lounge.save
134
-
134
+
135
135
  @hall = Room.create(:name => 'Hall')
136
136
  @hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
137
137
  @hm3 = Message.create(:body => 'Loungin!', :position => 3)
@@ -139,24 +139,24 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
139
139
  @hall.messages = [@hm1, @hm2, @hm3]
140
140
  @hall.save
141
141
  end
142
-
142
+
143
143
  context "dynamic finders" do
144
144
  should "work with single key" do
145
145
  @lounge.messages.find_by_position(1).should == @lm1
146
146
  @hall.messages.find_by_position(2).should == @hm2
147
147
  end
148
-
148
+
149
149
  should "work with multiple keys" do
150
150
  @lounge.messages.find_by_body_and_position('Loungin!', 1).should == @lm1
151
151
  @lounge.messages.find_by_body_and_position('Loungin!', 2).should be_nil
152
152
  end
153
-
153
+
154
154
  should "raise error when using !" do
155
155
  lambda {
156
156
  @lounge.messages.find_by_position!(222)
157
157
  }.should raise_error(MongoMapper::DocumentNotFound)
158
158
  end
159
-
159
+
160
160
  context "find_or_create_by" do
161
161
  should "not create document if found" do
162
162
  lambda {
@@ -175,103 +175,103 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
175
175
  end
176
176
  end
177
177
  end
178
-
178
+
179
179
  context "with #all" do
180
180
  should "work" do
181
181
  @lounge.messages.all(:order => "position").should == [@lm1, @lm2]
182
182
  end
183
-
183
+
184
184
  should "work with conditions" do
185
185
  messages = @lounge.messages.all(:body => 'Loungin!', :order => "position")
186
186
  messages.should == [@lm1]
187
187
  end
188
-
188
+
189
189
  should "work with order" do
190
190
  messages = @lounge.messages.all(:order => 'position desc')
191
191
  messages.should == [@lm2, @lm1]
192
192
  end
193
193
  end
194
-
194
+
195
195
  context "with #first" do
196
196
  should "work" do
197
197
  @lounge.messages.first(:order => "position asc").should == @lm1
198
198
  end
199
-
199
+
200
200
  should "work with conditions" do
201
201
  message = @lounge.messages.first(:body => 'I love loungin!', :order => "position asc")
202
202
  message.should == @lm2
203
203
  end
204
204
  end
205
-
205
+
206
206
  context "with #last" do
207
207
  should "work" do
208
208
  @lounge.messages.last(:order => "position asc").should == @lm2
209
209
  end
210
-
210
+
211
211
  should "work with conditions" do
212
212
  message = @lounge.messages.last(:body => 'Loungin!', :order => "position asc")
213
213
  message.should == @lm1
214
214
  end
215
215
  end
216
-
216
+
217
217
  context "with one id" do
218
218
  should "work for id in association" do
219
219
  @lounge.messages.find(@lm2._id).should == @lm2
220
220
  end
221
-
221
+
222
222
  should "not work for id not in association" do
223
223
  lambda {
224
224
  @lounge.messages.find!(@hm2._id)
225
225
  }.should raise_error(MongoMapper::DocumentNotFound)
226
226
  end
227
227
  end
228
-
228
+
229
229
  context "with query options/criteria" do
230
230
  should "work with order on association" do
231
231
  @lounge.messages.should == [@lm1, @lm2]
232
232
  end
233
-
233
+
234
234
  should "allow overriding the order provided to the association" do
235
235
  @lounge.messages.all(:order => 'position').should == [@lm1, @lm2]
236
236
  end
237
-
237
+
238
238
  should "allow using conditions on association" do
239
239
  @hall.latest_messages.should == [@hm3, @hm2]
240
240
  end
241
241
  end
242
-
242
+
243
243
  context "with multiple ids" do
244
244
  should "work for ids in association" do
245
245
  messages = @lounge.messages.find(@lm1._id, @lm2._id)
246
246
  messages.should == [@lm1, @lm2]
247
247
  end
248
-
248
+
249
249
  should "not work for ids not in association" do
250
250
  assert_raises(MongoMapper::DocumentNotFound) do
251
251
  @lounge.messages.find!(@lm1._id, @lm2._id, @hm2._id)
252
252
  end
253
253
  end
254
254
  end
255
-
255
+
256
256
  context "with #paginate" do
257
257
  setup do
258
258
  @messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
259
259
  end
260
-
260
+
261
261
  should "return total pages" do
262
262
  @messages.total_pages.should == 2
263
263
  end
264
-
264
+
265
265
  should "return total entries" do
266
266
  @messages.total_entries.should == 3
267
267
  end
268
-
268
+
269
269
  should "return the subject" do
270
270
  @messages.should == [@hm1, @hm2]
271
271
  end
272
272
  end
273
273
  end
274
-
274
+
275
275
  context "extending the association" do
276
276
  should "work using a block passed to many" do
277
277
  room = Room.new(:name => "Amazing Room")
@@ -286,9 +286,9 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
286
286
  room.save
287
287
  room.messages.older.should == messages[3..5]
288
288
  end
289
-
289
+
290
290
  should "work using many's :extend option" do
291
-
291
+
292
292
  room = Room.new(:name => "Amazing Room")
293
293
  accounts = room.accounts = [
294
294
  Bot.new(:last_logged_in => 3.weeks.ago),
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+
3
+ class OneEmbeddedProxyTest < Test::Unit::TestCase
4
+ def setup
5
+ @post_class = Doc('Post') do
6
+ key :title, String
7
+ end
8
+ @author_class = EDoc('Author') do
9
+ key :name, String
10
+ embedded_in :post
11
+ end
12
+ end
13
+
14
+ should "default to nil" do
15
+ @post_class.one :author, :class => @author_class
16
+ @post_class.new.author.should be_nil
17
+ end
18
+
19
+ should "be able to build" do
20
+ @post_class.one :author, :class => @author_class
21
+
22
+ post = @post_class.create
23
+ author = post.author.build(:name => "John")
24
+ post.author.should be_instance_of(@author_class)
25
+ post.author.should be_new
26
+ post.author.name.should == 'John'
27
+ post.author.should == author
28
+ post.author.post.should == post
29
+ end
30
+
31
+ should "be able to replace the association" do
32
+ @post_class.one :author, :class => @author_class
33
+
34
+ post = @post_class.new
35
+ author = @author_class.new(:name => 'Frank')
36
+ post.author = author
37
+ post.save
38
+ post.reload
39
+
40
+ post.author.should == author
41
+ post.author.nil?.should be_false
42
+
43
+ new_author = @author_class.new(:name => 'Emily')
44
+ post.author = new_author
45
+ post.author.should == new_author
46
+ end
47
+
48
+ should "not have problem loading root document if embedded one is nil" do
49
+ @post_class.one :author, :class => @author_class
50
+ post = @post_class.create
51
+
52
+ lambda {
53
+ @post_class.find(post.id)
54
+ }.should_not raise_error
55
+ end
56
+
57
+ should "have boolean method for testing presence" do
58
+ @post_class.one :author, :class => @author_class
59
+
60
+ post = @post_class.new
61
+ post.author?.should be_false
62
+
63
+ post.author = @author_class.new(:name => 'Frank')
64
+ post.author?.should be_true
65
+ end
66
+
67
+ end