mongo_mapper_ign 0.7.4 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/README.rdoc +1 -1
  2. data/lib/mongo_mapper.rb +5 -8
  3. data/lib/mongo_mapper/document.rb +24 -53
  4. data/lib/mongo_mapper/embedded_document.rb +1 -0
  5. data/lib/mongo_mapper/plugins.rb +1 -0
  6. data/lib/mongo_mapper/plugins/associations.rb +2 -1
  7. data/lib/mongo_mapper/plugins/associations/base.rb +5 -4
  8. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +2 -1
  9. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +2 -1
  10. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  11. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -0
  12. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +1 -0
  13. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +1 -0
  14. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +2 -1
  15. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  16. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -0
  17. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  18. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +1 -0
  19. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +2 -1
  20. data/lib/mongo_mapper/plugins/associations/proxy.rb +1 -0
  21. data/lib/mongo_mapper/plugins/callbacks.rb +1 -0
  22. data/lib/mongo_mapper/plugins/clone.rb +1 -0
  23. data/lib/mongo_mapper/plugins/descendants.rb +1 -0
  24. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  25. data/lib/mongo_mapper/plugins/equality.rb +4 -3
  26. data/lib/mongo_mapper/plugins/identity_map.rb +9 -7
  27. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  28. data/lib/mongo_mapper/plugins/keys.rb +50 -73
  29. data/lib/mongo_mapper/plugins/keys/key.rb +54 -0
  30. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  31. data/lib/mongo_mapper/plugins/modifiers.rb +10 -5
  32. data/lib/mongo_mapper/plugins/pagination.rb +1 -0
  33. data/lib/mongo_mapper/plugins/pagination/proxy.rb +4 -3
  34. data/lib/mongo_mapper/plugins/persistence.rb +1 -0
  35. data/lib/mongo_mapper/plugins/protected.rb +1 -0
  36. data/lib/mongo_mapper/plugins/rails.rb +1 -0
  37. data/lib/mongo_mapper/plugins/serialization.rb +4 -3
  38. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  39. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  40. data/lib/mongo_mapper/plugins/validations.rb +3 -2
  41. data/lib/mongo_mapper/query.rb +10 -129
  42. data/lib/mongo_mapper/support.rb +18 -39
  43. data/lib/mongo_mapper/support/descendant_appends.rb +1 -0
  44. data/lib/mongo_mapper/support/find.rb +1 -0
  45. data/lib/mongo_mapper/version.rb +2 -1
  46. metadata +32 -134
  47. data/.gitignore +0 -10
  48. data/Rakefile +0 -37
  49. data/mongo_mapper.gemspec +0 -214
  50. data/mongo_mapper_ign.gemspec +0 -217
  51. data/performance/read_write.rb +0 -52
  52. data/specs.watchr +0 -51
  53. data/test/NOTE_ON_TESTING +0 -1
  54. data/test/active_model_lint_test.rb +0 -13
  55. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
  56. data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
  57. data/test/functional/associations/test_in_array_proxy.rb +0 -325
  58. data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
  59. data/test/functional/associations/test_many_documents_proxy.rb +0 -536
  60. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
  61. data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
  62. data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
  63. data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
  64. data/test/functional/associations/test_one_proxy.rb +0 -196
  65. data/test/functional/test_associations.rb +0 -44
  66. data/test/functional/test_binary.rb +0 -27
  67. data/test/functional/test_callbacks.rb +0 -151
  68. data/test/functional/test_dirty.rb +0 -163
  69. data/test/functional/test_document.rb +0 -1219
  70. data/test/functional/test_embedded_document.rb +0 -210
  71. data/test/functional/test_identity_map.rb +0 -507
  72. data/test/functional/test_indexing.rb +0 -44
  73. data/test/functional/test_logger.rb +0 -20
  74. data/test/functional/test_modifiers.rb +0 -394
  75. data/test/functional/test_pagination.rb +0 -93
  76. data/test/functional/test_protected.rb +0 -163
  77. data/test/functional/test_string_id_compatibility.rb +0 -67
  78. data/test/functional/test_timestamps.rb +0 -64
  79. data/test/functional/test_userstamps.rb +0 -28
  80. data/test/functional/test_validations.rb +0 -342
  81. data/test/models.rb +0 -227
  82. data/test/support/custom_matchers.rb +0 -37
  83. data/test/support/timing.rb +0 -16
  84. data/test/test_helper.rb +0 -64
  85. data/test/unit/associations/test_base.rb +0 -212
  86. data/test/unit/associations/test_proxy.rb +0 -105
  87. data/test/unit/serializers/test_json_serializer.rb +0 -202
  88. data/test/unit/test_descendant_appends.rb +0 -71
  89. data/test/unit/test_document.rb +0 -225
  90. data/test/unit/test_dynamic_finder.rb +0 -123
  91. data/test/unit/test_embedded_document.rb +0 -657
  92. data/test/unit/test_keys.rb +0 -185
  93. data/test/unit/test_mongo_mapper.rb +0 -118
  94. data/test/unit/test_pagination.rb +0 -160
  95. data/test/unit/test_plugins.rb +0 -50
  96. data/test/unit/test_query.rb +0 -374
  97. data/test/unit/test_rails.rb +0 -181
  98. data/test/unit/test_rails_compatibility.rb +0 -52
  99. data/test/unit/test_serialization.rb +0 -51
  100. data/test/unit/test_support.rb +0 -382
  101. data/test/unit/test_time_zones.rb +0 -39
  102. data/test/unit/test_validations.rb +0 -544
@@ -1,229 +0,0 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class ManyDocumentsAsProxyTest < Test::Unit::TestCase
5
- def setup
6
- Post.collection.remove
7
- PostComment.collection.remove
8
- end
9
-
10
- should "default reader to empty array" do
11
- Post.new.comments.should == []
12
- end
13
-
14
- should "add type and id key to polymorphic class base" do
15
- PostComment.keys.keys.should include('commentable_type')
16
- PostComment.keys.keys.should include('commentable_id')
17
- end
18
-
19
- should "allow adding to association like it was an array" do
20
- post = Post.new
21
- post.comments << PostComment.new(:body => 'foo bar')
22
- post.comments << PostComment.new(:body => 'baz')
23
- post.comments.concat PostComment.new(:body => 'baz')
24
-
25
- post.comments.size.should == 3
26
- end
27
-
28
- should "be able to replace the association" do
29
- post = Post.new
30
-
31
- lambda {
32
- post.comments = [
33
- PostComment.new(:body => 'foo'),
34
- PostComment.new(:body => 'bar'),
35
- PostComment.new(:body => 'baz')
36
- ]
37
- }.should change { PostComment.count }.by(3)
38
-
39
- post = post.reload
40
- post.comments.size.should == 3
41
- bodies = post.comments.collect(&:body)
42
- bodies.should include('foo')
43
- bodies.should include('bar')
44
- bodies.should include('baz')
45
- end
46
-
47
- context "build" do
48
- should "assign foreign key" do
49
- post = Post.new
50
- comment = post.comments.build
51
- comment.commentable_id.should == post._id
52
- end
53
-
54
- should "assign _type" do
55
- post = Post.new
56
- comment = post.comments.build
57
- comment.commentable_type.should == "Post"
58
- end
59
-
60
- should "allow assigning attributes" do
61
- post = Post.new
62
- comment = post.comments.build(:body => 'foo bar')
63
- comment.body.should == 'foo bar'
64
- end
65
- end
66
-
67
- context "create" do
68
- should "assign foreign key" do
69
- post = Post.new
70
- comment = post.comments.create
71
- comment.commentable_id.should == post._id
72
- end
73
-
74
- should "assign _type" do
75
- post = Post.new
76
- comment = post.comments.create
77
- comment.commentable_type.should == "Post"
78
- end
79
-
80
- should "save record" do
81
- post = Post.new
82
- lambda {
83
- post.comments.create(:body => 'baz')
84
- }.should change { PostComment.count }
85
- end
86
-
87
- should "allow passing attributes" do
88
- post = Post.create
89
- comment = post.comments.create(:body => 'foo bar')
90
- comment.body.should == 'foo bar'
91
- end
92
- end
93
-
94
- context "count" do
95
- should "work scoped to association" do
96
- post = Post.create
97
- 3.times { post.comments.create(:body => 'foo bar') }
98
-
99
- other_post = Post.create
100
- 2.times { other_post.comments.create(:body => 'baz') }
101
-
102
- post.comments.count.should == 3
103
- other_post.comments.count.should == 2
104
- end
105
-
106
- should "work with conditions" do
107
- post = Post.create
108
- post.comments.create(:body => 'foo bar')
109
- post.comments.create(:body => 'baz')
110
- post.comments.create(:body => 'foo bar')
111
-
112
- post.comments.count(:body => 'foo bar').should == 2
113
- end
114
- end
115
-
116
- context "Finding scoped to association" do
117
- setup do
118
- @post = Post.new
119
-
120
- @comment1 = PostComment.create(:body => 'comment1', :name => 'John')
121
- @comment2 = PostComment.create(:body => 'comment2', :name => 'Steve')
122
- @comment3 = PostComment.create(:body => 'comment3', :name => 'John')
123
- @post.comments = [@comment1, @comment2]
124
- @post.save
125
-
126
- @post2 = Post.create(:body => "post #2")
127
- @comment4 = PostComment.create(:body => 'comment1', :name => 'Chas')
128
- @comment5 = PostComment.create(:body => 'comment2', :name => 'Dan')
129
- @comment6 = PostComment.create(:body => 'comment3', :name => 'Ed')
130
- @post2.comments = [@comment4, @comment5, @comment6]
131
- @post2.save
132
- end
133
-
134
- context "with #all" do
135
- should "work" do
136
- @post.comments.all.should include(@comment1)
137
- @post.comments.all.should include(@comment2)
138
- end
139
-
140
- should "work with conditions" do
141
- comments = @post.comments.all(:body => 'comment1')
142
- comments.should == [@comment1]
143
- end
144
-
145
- should "work with order" do
146
- comments = @post.comments.all(:order => 'body desc')
147
- comments.should == [@comment2, @comment1]
148
- end
149
- end
150
-
151
- context "with one id" do
152
- should "work for id in association" do
153
- @post.comments.find(@comment2._id).should == @comment2
154
- end
155
-
156
- should "not work for id not in association" do
157
- assert_raises(MongoMapper::DocumentNotFound) do
158
- @post.comments.find!(@comment5._id)
159
- end
160
- end
161
- end
162
-
163
- context "with multiple ids" do
164
- should "work for ids in association" do
165
- posts = @post.comments.find!(@comment1._id, @comment2._id)
166
- posts.should == [@comment1, @comment2]
167
- end
168
-
169
- should "not work for ids not in association" do
170
- assert_raises(MongoMapper::DocumentNotFound) do
171
- @post.comments.find!(@comment1._id, @comment2._id, @comment4._id)
172
- end
173
- end
174
- end
175
-
176
- context "dynamic finders" do
177
- should "work with single key" do
178
- @post.comments.find_by_body('comment1').should == @comment1
179
- @post2.comments.find_by_body('comment1').should == @comment4
180
- end
181
-
182
- should "work with multiple keys" do
183
- @post.comments.find_by_body_and_name('comment1', 'John').should == @comment1
184
- @post.comments.find_by_body_and_name('comment1', 'Frank').should be_nil
185
- end
186
-
187
- should "raise error when using !" do
188
- lambda {
189
- @post.comments.find_by_body!('asdf')
190
- }.should raise_error(MongoMapper::DocumentNotFound)
191
- end
192
-
193
- context "find_or_create_by" do
194
- should "not create document if found" do
195
- lambda {
196
- comment = @post.comments.find_or_create_by_name('Steve')
197
- comment.commentable.should == @post
198
- comment.should == @comment2
199
- }.should_not change { PostComment.count }
200
- end
201
-
202
- should "create document if not found" do
203
- lambda {
204
- @post.comments.find_or_create_by_name('Chas')
205
- }.should change { PostComment.count }.by(1)
206
- end
207
- end
208
- end
209
-
210
- context "with #paginate" do
211
- setup do
212
- @comments = @post2.comments.paginate(:per_page => 2, :page => 1, :order => 'name')
213
- end
214
-
215
- should "return total pages" do
216
- @comments.total_pages.should == 2
217
- end
218
-
219
- should "return total entries" do
220
- @comments.total_entries.should == 3
221
- end
222
-
223
- should "return the subject" do
224
- @comments.should include(@comment4)
225
- @comments.should include(@comment5)
226
- end
227
- end
228
- end
229
- end
@@ -1,536 +0,0 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class ManyDocumentsProxyTest < Test::Unit::TestCase
5
- def setup
6
- Project.collection.remove
7
- Status.collection.remove
8
-
9
- @pet_class = Doc do
10
- key :name, String
11
- key :owner_id, ObjectId
12
- end
13
-
14
- @owner_class = Doc do
15
- key :name, String
16
- end
17
- @owner_class.many :pets, :class => @pet_class, :foreign_key => :owner_id, :order => 'name'
18
- end
19
-
20
- should "default reader to empty array" do
21
- project = Project.new
22
- project.statuses.should == []
23
- end
24
-
25
- should "allow assignment of many associated documents using a hash" do
26
- person_attributes = {
27
- 'name' => 'Mr. Pet Lover',
28
- 'pets' => [
29
- {'name' => 'Jimmy', 'species' => 'Cocker Spainel'},
30
- {'name' => 'Sasha', 'species' => 'Siberian Husky'},
31
- ]
32
- }
33
-
34
- owner = @owner_class.new(person_attributes)
35
- owner.name.should == 'Mr. Pet Lover'
36
- owner.pets[0].name.should == 'Jimmy'
37
- owner.pets[0].species.should == 'Cocker Spainel'
38
- owner.pets[1].name.should == 'Sasha'
39
- owner.pets[1].species.should == 'Siberian Husky'
40
-
41
- owner.save.should be_true
42
- owner.reload
43
-
44
- owner.name.should == 'Mr. Pet Lover'
45
- owner.pets[0].name.should == 'Jimmy'
46
- owner.pets[0].species.should == 'Cocker Spainel'
47
- owner.pets[1].name.should == 'Sasha'
48
- owner.pets[1].species.should == 'Siberian Husky'
49
- end
50
-
51
- should "allow adding to association like it was an array" do
52
- project = Project.new
53
- project.statuses << Status.new(:name => 'Foo1!')
54
- project.statuses.push Status.new(:name => 'Foo2!')
55
- project.statuses.concat Status.new(:name => 'Foo3!')
56
- project.statuses.size.should == 3
57
- end
58
-
59
- context "replacing the association" do
60
- context "with objects of the class" do
61
- should "work" do
62
- project = Project.new
63
- project.statuses = [Status.new(:name => "ready")]
64
- project.save.should be_true
65
-
66
- project.reload
67
- project.statuses.size.should == 1
68
- project.statuses[0].name.should == "ready"
69
- end
70
- end
71
-
72
- context "with Hashes" do
73
- should "convert to objects of the class and work" do
74
- project = Project.new
75
- project.statuses = [{ 'name' => 'ready' }]
76
- project.save.should be_true
77
-
78
- project.reload
79
- project.statuses.size.should == 1
80
- project.statuses[0].name.should == "ready"
81
- end
82
- end
83
- end
84
-
85
- context "using <<, push and concat" do
86
- context "with objects of the class" do
87
- should "correctly assign foreign key" do
88
- project = Project.new
89
- project.statuses << Status.new(:name => '<<')
90
- project.statuses.push Status.new(:name => 'push')
91
- project.statuses.concat Status.new(:name => 'concat')
92
-
93
- project.reload
94
- project.statuses[0].project_id.should == project.id
95
- project.statuses[1].project_id.should == project.id
96
- project.statuses[2].project_id.should == project.id
97
- end
98
- end
99
-
100
- context "with Hashes" do
101
- should "correctly convert to objects and assign foreign key" do
102
- project = Project.new
103
- project.statuses << { 'name' => '<<' }
104
- project.statuses.push( { 'name' => 'push' })
105
- project.statuses.concat({ 'name' => 'concat' })
106
-
107
- project.reload
108
- project.statuses[0].project_id.should == project.id
109
- project.statuses[1].project_id.should == project.id
110
- project.statuses[2].project_id.should == project.id
111
- end
112
- end
113
- end
114
-
115
- context "build" do
116
- should "assign foreign key" do
117
- project = Project.create
118
- status = project.statuses.build
119
- status.project_id.should == project.id
120
- end
121
-
122
- should "allow assigning attributes" do
123
- project = Project.create
124
- status = project.statuses.build(:name => 'Foo')
125
- status.name.should == 'Foo'
126
- end
127
-
128
- should "reset cache" do
129
- project = Project.create
130
- project.statuses.size.should == 0
131
- status = project.statuses.build(:name => 'Foo')
132
- status.save!
133
- project.statuses.size.should == 1
134
- end
135
-
136
- should "update collection without save" do
137
- project = Project.create
138
- project.statuses.build(:name => 'Foo')
139
- project.statuses.size.should == 1
140
- end
141
-
142
- should "save built document when saving parent" do
143
- project = Project.create
144
- status = project.statuses.build(:name => 'Foo')
145
- project.save!
146
- status.should_not be_new
147
- end
148
- end
149
-
150
- context "create" do
151
- should "assign foreign key" do
152
- project = Project.create
153
- status = project.statuses.create(:name => 'Foo!')
154
- status.project_id.should == project.id
155
- end
156
-
157
- should "save record" do
158
- project = Project.create
159
- lambda {
160
- project.statuses.create(:name => 'Foo!')
161
- }.should change { Status.count }
162
- end
163
-
164
- should "allow passing attributes" do
165
- project = Project.create
166
- status = project.statuses.create(:name => 'Foo!')
167
- status.name.should == 'Foo!'
168
- end
169
-
170
- should "reset cache" do
171
- project = Project.create
172
- project.statuses.size.should == 0
173
- project.statuses.create(:name => 'Foo!')
174
- project.statuses.size.should == 1
175
- end
176
- end
177
-
178
- context "create!" do
179
- should "assign foreign key" do
180
- project = Project.create
181
- status = project.statuses.create!(:name => 'Foo!')
182
- status.project_id.should == project.id
183
- end
184
-
185
- should "save record" do
186
- project = Project.create
187
- lambda {
188
- project.statuses.create!(:name => 'Foo!')
189
- }.should change { Status.count }
190
- end
191
-
192
- should "allow passing attributes" do
193
- project = Project.create
194
- status = project.statuses.create!(:name => 'Foo!')
195
- status.name.should == 'Foo!'
196
- end
197
-
198
- should "raise exception if not valid" do
199
- project = Project.create
200
- lambda {
201
- project.statuses.create!(:name => nil)
202
- }.should raise_error(MongoMapper::DocumentNotValid)
203
- end
204
-
205
- should "reset cache" do
206
- project = Project.create
207
- project.statuses.size.should == 0
208
- project.statuses.create!(:name => 'Foo!')
209
- project.statuses.size.should == 1
210
- end
211
- end
212
-
213
- context "count" do
214
- should "work scoped to association" do
215
- project = Project.create
216
- 3.times { project.statuses.create(:name => 'Foo!') }
217
-
218
- other_project = Project.create
219
- 2.times { other_project.statuses.create(:name => 'Foo!') }
220
-
221
- project.statuses.count.should == 3
222
- other_project.statuses.count.should == 2
223
- end
224
-
225
- should "work with conditions" do
226
- project = Project.create
227
- project.statuses.create(:name => 'Foo')
228
- project.statuses.create(:name => 'Other 1')
229
- project.statuses.create(:name => 'Other 2')
230
-
231
- project.statuses.count(:name => 'Foo').should == 1
232
- end
233
- end
234
-
235
- context "Unassociating documents" do
236
- setup do
237
- @project = Project.create
238
- @project.statuses << Status.create(:name => '1')
239
- @project.statuses << Status.create(:name => '2')
240
-
241
- @project2 = Project.create
242
- @project2.statuses << Status.create(:name => '1')
243
- @project2.statuses << Status.create(:name => '2')
244
- end
245
-
246
- should "work with destroy all" do
247
- @project.statuses.count.should == 2
248
- @project.statuses.destroy_all
249
- @project.statuses.count.should == 0
250
-
251
- @project2.statuses.count.should == 2
252
- Status.count.should == 2
253
- end
254
-
255
- should "work with destroy all and conditions" do
256
- @project.statuses.count.should == 2
257
- @project.statuses.destroy_all(:name => '1')
258
- @project.statuses.count.should == 1
259
-
260
- @project2.statuses.count.should == 2
261
- Status.count.should == 3
262
- end
263
-
264
- should "work with delete all" do
265
- @project.statuses.count.should == 2
266
- @project.statuses.delete_all
267
- @project.statuses.count.should == 0
268
-
269
- @project2.statuses.count.should == 2
270
- Status.count.should == 2
271
- end
272
-
273
- should "work with delete all and conditions" do
274
- @project.statuses.count.should == 2
275
- @project.statuses.delete_all(:name => '1')
276
- @project.statuses.count.should == 1
277
-
278
- @project2.statuses.count.should == 2
279
- Status.count.should == 3
280
- end
281
-
282
- should "work with nullify" do
283
- @project.statuses.count.should == 2
284
- @project.statuses.nullify
285
- @project.statuses.count.should == 0
286
-
287
- @project2.statuses.count.should == 2
288
- Status.count.should == 4
289
- Status.count(:name => '1').should == 2
290
- Status.count(:name => '2').should == 2
291
- end
292
- end
293
-
294
- context "Finding scoped to association" do
295
- setup do
296
- @project1 = Project.new(:name => 'Project 1')
297
- @brand_new = Status.create(:name => 'New', :position => 1 )
298
- @complete = Status.create(:name => 'Complete', :position => 2)
299
- @project1.statuses = [@brand_new, @complete]
300
- @project1.save
301
-
302
- @project2 = Project.create(:name => 'Project 2')
303
- @in_progress = Status.create(:name => 'In Progress')
304
- @archived = Status.create(:name => 'Archived')
305
- @another_complete = Status.create(:name => 'Complete')
306
- @project2.statuses = [@in_progress, @archived, @another_complete]
307
- @project2.save
308
- end
309
-
310
- context "dynamic finders" do
311
- should "work with single key" do
312
- @project1.statuses.find_by_name('New').should == @brand_new
313
- @project1.statuses.find_by_name!('New').should == @brand_new
314
- @project2.statuses.find_by_name('In Progress').should == @in_progress
315
- @project2.statuses.find_by_name!('In Progress').should == @in_progress
316
- end
317
-
318
- should "work with multiple keys" do
319
- @project1.statuses.find_by_name_and_position('New', 1).should == @brand_new
320
- @project1.statuses.find_by_name_and_position!('New', 1).should == @brand_new
321
- @project1.statuses.find_by_name_and_position('New', 2).should be_nil
322
- end
323
-
324
- should "raise error when using !" do
325
- lambda {
326
- @project1.statuses.find_by_name!('Fake')
327
- }.should raise_error(MongoMapper::DocumentNotFound)
328
- end
329
-
330
- context "find_or_create_by" do
331
- should "not create document if found" do
332
- lambda {
333
- status = @project1.statuses.find_or_create_by_name('New')
334
- status.project.should == @project1
335
- status.should == @brand_new
336
- }.should_not change { Status.count }
337
- end
338
-
339
- should "create document if not found" do
340
- lambda {
341
- status = @project1.statuses.find_or_create_by_name('Delivered')
342
- status.project.should == @project1
343
- }.should change { Status.count }
344
- end
345
- end
346
- end
347
-
348
- context "all" do
349
- should "work" do
350
- @project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
351
- end
352
-
353
- should "work with conditions" do
354
- @project1.statuses.all(:name => 'Complete').should == [@complete]
355
- end
356
- end
357
-
358
- context "first" do
359
- should "work" do
360
- @project1.statuses.first(:order => 'name').should == @complete
361
- end
362
-
363
- should "work with conditions" do
364
- @project1.statuses.first(:name => 'Complete').should == @complete
365
- end
366
- end
367
-
368
- context "last" do
369
- should "work" do
370
- @project1.statuses.last(:order => "position asc").should == @complete
371
- end
372
-
373
- should "work with conditions" do
374
- @project1.statuses.last(:order => 'position', :name => 'New').should == @brand_new
375
- end
376
- end
377
-
378
- context "with one id" do
379
- should "work for id in association" do
380
- @project1.statuses.find(@complete.id).should == @complete
381
- end
382
-
383
- should "not work for id not in association" do
384
- lambda {
385
- @project1.statuses.find!(@archived.id)
386
- }.should raise_error(MongoMapper::DocumentNotFound)
387
- end
388
- end
389
-
390
- context "with multiple ids" do
391
- should "work for ids in association" do
392
- statuses = @project1.statuses.find(@brand_new.id, @complete.id)
393
- statuses.should == [@brand_new, @complete]
394
- end
395
-
396
- should "not work for ids not in association" do
397
- assert_raises(MongoMapper::DocumentNotFound) do
398
- @project1.statuses.find!(@brand_new.id, @complete.id, @archived.id)
399
- end
400
- end
401
- end
402
-
403
- context "with #paginate" do
404
- setup do
405
- @statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => 'name asc')
406
- end
407
-
408
- should "return total pages" do
409
- @statuses.total_pages.should == 2
410
- end
411
-
412
- should "return total entries" do
413
- @statuses.total_entries.should == 3
414
- end
415
-
416
- should "return the subject" do
417
- @statuses.collect(&:name).should == %w(Archived Complete)
418
- end
419
- end
420
- end
421
-
422
- context "extending the association" do
423
- should "work using a block passed to many" do
424
- project = Project.new(:name => "Some Project")
425
- status1 = Status.new(:name => "New")
426
- status2 = Status.new(:name => "Assigned")
427
- status3 = Status.new(:name => "Closed")
428
- project.statuses = [status1, status2, status3]
429
- project.save
430
-
431
- open_statuses = project.statuses.open
432
- open_statuses.should include(status1)
433
- open_statuses.should include(status2)
434
- open_statuses.should_not include(status3)
435
- end
436
-
437
- should "work using many's :extend option" do
438
- project = Project.new(:name => "Some Project")
439
- collaborator1 = Collaborator.new(:name => "zing")
440
- collaborator2 = Collaborator.new(:name => "zang")
441
- project.collaborators = [collaborator1, collaborator2]
442
- project.save
443
- project.collaborators.top.should == collaborator1
444
- end
445
- end
446
-
447
- context ":dependent" do
448
- setup do
449
- # FIXME: make use of already defined models
450
- class ::Property
451
- include MongoMapper::Document
452
- end
453
- Property.collection.remove
454
-
455
- class ::Thing
456
- include MongoMapper::Document
457
- key :name, String
458
- end
459
- Thing.collection.remove
460
- end
461
-
462
- teardown do
463
- Object.send :remove_const, 'Property' if defined?(::Property)
464
- Object.send :remove_const, 'Thing' if defined?(::Thing)
465
- end
466
-
467
- context "=> destroy" do
468
- setup do
469
- Property.key :thing_id, ObjectId
470
- Property.belongs_to :thing, :dependent => :destroy
471
- Thing.many :properties, :dependent => :destroy
472
-
473
- @thing = Thing.create(:name => "Tree")
474
- @property1 = Property.create
475
- @property2 = Property.create
476
- @property3 = Property.create
477
- @thing.properties << @property1
478
- @thing.properties << @property2
479
- @thing.properties << @property3
480
- end
481
-
482
- should "should destroy the associated documents" do
483
- @thing.properties.count.should == 3
484
- @thing.destroy
485
- @thing.properties.count.should == 0
486
- Property.count.should == 0
487
- end
488
- end
489
-
490
- context "=> delete_all" do
491
- setup do
492
- Property.key :thing_id, ObjectId
493
- Property.belongs_to :thing
494
- Thing.has_many :properties, :dependent => :delete_all
495
-
496
- @thing = Thing.create(:name => "Tree")
497
- @property1 = Property.create
498
- @property2 = Property.create
499
- @property3 = Property.create
500
- @thing.properties << @property1
501
- @thing.properties << @property2
502
- @thing.properties << @property3
503
- end
504
-
505
- should "should delete associated documents" do
506
- @thing.properties.count.should == 3
507
- @thing.destroy
508
- @thing.properties.count.should == 0
509
- Property.count.should == 0
510
- end
511
- end
512
-
513
- context "=> nullify" do
514
- setup do
515
- Property.key :thing_id, ObjectId
516
- Property.belongs_to :thing
517
- Thing.has_many :properties, :dependent => :nullify
518
-
519
- @thing = Thing.create(:name => "Tree")
520
- @property1 = Property.create
521
- @property2 = Property.create
522
- @property3 = Property.create
523
- @thing.properties << @property1
524
- @thing.properties << @property2
525
- @thing.properties << @property3
526
- end
527
-
528
- should "should nullify relationship but not destroy associated documents" do
529
- @thing.properties.count.should == 3
530
- @thing.destroy
531
- @thing.properties.count.should == 0
532
- Property.count.should == 3
533
- end
534
- end
535
- end
536
- end