pwnash-mongo_mapper 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_mapper.rb +6 -8
- data/lib/mongo_mapper/document.rb +6 -281
- data/lib/mongo_mapper/embedded_document.rb +5 -45
- data/lib/mongo_mapper/extensions.rb +190 -0
- data/lib/mongo_mapper/plugins.rb +27 -18
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +4 -3
- data/lib/mongo_mapper/plugins/clone.rb +1 -0
- data/lib/mongo_mapper/plugins/descendants.rb +1 -0
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +40 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +42 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +2 -36
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +1 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +8 -11
- data/lib/mongo_mapper/plugins/indexes.rb +11 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys.rb +7 -9
- data/lib/mongo_mapper/plugins/keys/key.rb +12 -2
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +3 -2
- data/lib/mongo_mapper/plugins/pagination.rb +1 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/persistence.rb +1 -0
- data/lib/mongo_mapper/plugins/protected.rb +1 -0
- data/lib/mongo_mapper/plugins/query_logger.rb +5 -10
- data/lib/mongo_mapper/plugins/querying.rb +236 -0
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/sci.rb +31 -0
- data/lib/mongo_mapper/plugins/serialization.rb +1 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +5 -1
- data/lib/mongo_mapper/query.rb +53 -120
- data/lib/mongo_mapper/support.rb +2 -213
- data/lib/mongo_mapper/support/descendant_appends.rb +3 -6
- data/lib/mongo_mapper/version.rb +2 -1
- metadata +156 -190
- data/.gitignore +0 -10
- data/Rakefile +0 -37
- data/mongo_mapper.gemspec +0 -216
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/NOTE_ON_TESTING +0 -1
- data/test/active_model_lint_test.rb +0 -13
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
- data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
- data/test/functional/associations/test_in_array_proxy.rb +0 -325
- data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
- data/test/functional/associations/test_many_documents_proxy.rb +0 -536
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
- data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
- data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
- data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
- data/test/functional/associations/test_one_proxy.rb +0 -196
- data/test/functional/test_associations.rb +0 -44
- data/test/functional/test_binary.rb +0 -27
- data/test/functional/test_callbacks.rb +0 -151
- data/test/functional/test_dirty.rb +0 -163
- data/test/functional/test_document.rb +0 -1219
- data/test/functional/test_embedded_document.rb +0 -210
- data/test/functional/test_identity_map.rb +0 -507
- data/test/functional/test_indexing.rb +0 -44
- data/test/functional/test_logger.rb +0 -20
- data/test/functional/test_modifiers.rb +0 -416
- data/test/functional/test_pagination.rb +0 -93
- data/test/functional/test_protected.rb +0 -163
- data/test/functional/test_string_id_compatibility.rb +0 -67
- data/test/functional/test_timestamps.rb +0 -64
- data/test/functional/test_userstamps.rb +0 -28
- data/test/functional/test_validations.rb +0 -342
- data/test/models.rb +0 -227
- data/test/support/custom_matchers.rb +0 -37
- data/test/support/timing.rb +0 -16
- data/test/test_helper.rb +0 -64
- data/test/unit/associations/test_base.rb +0 -212
- data/test/unit/associations/test_proxy.rb +0 -105
- data/test/unit/serializers/test_json_serializer.rb +0 -202
- data/test/unit/test_descendant_appends.rb +0 -71
- data/test/unit/test_document.rb +0 -225
- data/test/unit/test_dynamic_finder.rb +0 -123
- data/test/unit/test_embedded_document.rb +0 -657
- data/test/unit/test_keys.rb +0 -216
- data/test/unit/test_mongo_mapper.rb +0 -118
- data/test/unit/test_pagination.rb +0 -160
- data/test/unit/test_plugins.rb +0 -50
- data/test/unit/test_query.rb +0 -374
- data/test/unit/test_rails.rb +0 -181
- data/test/unit/test_rails_compatibility.rb +0 -52
- data/test/unit/test_serialization.rb +0 -51
- data/test/unit/test_support.rb +0 -390
- data/test/unit/test_time_zones.rb +0 -39
- data/test/unit/test_validations.rb +0 -544
@@ -1,325 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class InArrayProxyTest < Test::Unit::TestCase
|
4
|
-
context "description" do
|
5
|
-
setup do
|
6
|
-
class ::List
|
7
|
-
include MongoMapper::Document
|
8
|
-
key :name, String, :required => true
|
9
|
-
end
|
10
|
-
|
11
|
-
class ::User
|
12
|
-
include MongoMapper::Document
|
13
|
-
key :name, String, :required => true
|
14
|
-
key :list_ids, Array
|
15
|
-
many :lists, :in => :list_ids
|
16
|
-
end
|
17
|
-
User.collection.remove
|
18
|
-
List.collection.remove
|
19
|
-
end
|
20
|
-
|
21
|
-
teardown do
|
22
|
-
Object.send :remove_const, 'List' if defined?(::List)
|
23
|
-
Object.send :remove_const, 'User' if defined?(::User)
|
24
|
-
end
|
25
|
-
|
26
|
-
should "default reader to empty array" do
|
27
|
-
User.new.lists.should == []
|
28
|
-
end
|
29
|
-
|
30
|
-
should "allow adding to association like it was an array" do
|
31
|
-
user = User.new(:name => 'John')
|
32
|
-
user.lists << List.new(:name => 'Foo1!')
|
33
|
-
user.lists.push List.new(:name => 'Foo2!')
|
34
|
-
user.lists.concat List.new(:name => 'Foo3!')
|
35
|
-
user.lists.size.should == 3
|
36
|
-
end
|
37
|
-
|
38
|
-
should "ignore adding duplicate ids" do
|
39
|
-
user = User.create(:name => 'John')
|
40
|
-
list = List.create(:name => 'Foo')
|
41
|
-
user.lists << list
|
42
|
-
user.lists << list
|
43
|
-
user.lists << list
|
44
|
-
|
45
|
-
user.list_ids.should == [list.id]
|
46
|
-
user.lists.count.should == 1
|
47
|
-
end
|
48
|
-
|
49
|
-
should "be able to replace the association" do
|
50
|
-
user = User.new(:name => 'John')
|
51
|
-
list = List.new(:name => 'Foo')
|
52
|
-
user.lists = [list]
|
53
|
-
user.save.should be_true
|
54
|
-
|
55
|
-
user.reload
|
56
|
-
user.list_ids.should == [list.id]
|
57
|
-
user.lists.size.should == 1
|
58
|
-
user.lists[0].name.should == 'Foo'
|
59
|
-
end
|
60
|
-
|
61
|
-
context "create" do
|
62
|
-
setup do
|
63
|
-
@user = User.create(:name => 'John')
|
64
|
-
@list = @user.lists.create(:name => 'Foo!')
|
65
|
-
end
|
66
|
-
|
67
|
-
should "add id to key" do
|
68
|
-
@user.list_ids.should include(@list.id)
|
69
|
-
end
|
70
|
-
|
71
|
-
should "persist id addition to key in database" do
|
72
|
-
@user.reload
|
73
|
-
@user.list_ids.should include(@list.id)
|
74
|
-
end
|
75
|
-
|
76
|
-
should "add doc to association" do
|
77
|
-
@user.lists.should include(@list)
|
78
|
-
end
|
79
|
-
|
80
|
-
should "save doc" do
|
81
|
-
@list.should_not be_new
|
82
|
-
end
|
83
|
-
|
84
|
-
should "reset cache" do
|
85
|
-
@user.lists.size.should == 1
|
86
|
-
@user.lists.create(:name => 'Moo!')
|
87
|
-
@user.lists.size.should == 2
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context "create!" do
|
92
|
-
setup do
|
93
|
-
@user = User.create(:name => 'John')
|
94
|
-
@list = @user.lists.create!(:name => 'Foo!')
|
95
|
-
end
|
96
|
-
|
97
|
-
should "add id to key" do
|
98
|
-
@user.list_ids.should include(@list.id)
|
99
|
-
end
|
100
|
-
|
101
|
-
should "persist id addition to key in database" do
|
102
|
-
@user.reload
|
103
|
-
@user.list_ids.should include(@list.id)
|
104
|
-
end
|
105
|
-
|
106
|
-
should "add doc to association" do
|
107
|
-
@user.lists.should include(@list)
|
108
|
-
end
|
109
|
-
|
110
|
-
should "save doc" do
|
111
|
-
@list.should_not be_new
|
112
|
-
end
|
113
|
-
|
114
|
-
should "raise exception if invalid" do
|
115
|
-
assert_raises(MongoMapper::DocumentNotValid) do
|
116
|
-
@user.lists.create!
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
should "reset cache" do
|
121
|
-
@user.lists.size.should == 1
|
122
|
-
@user.lists.create!(:name => 'Moo!')
|
123
|
-
@user.lists.size.should == 2
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "Finding scoped to association" do
|
128
|
-
setup do
|
129
|
-
@user = User.create(:name => 'John')
|
130
|
-
@user2 = User.create(:name => 'Brandon')
|
131
|
-
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
132
|
-
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
133
|
-
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
134
|
-
end
|
135
|
-
|
136
|
-
context "all" do
|
137
|
-
should "work" do
|
138
|
-
@user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
|
139
|
-
@user.lists.all(:order => :position.asc).should == [@list1, @list2]
|
140
|
-
end
|
141
|
-
|
142
|
-
should "work with conditions" do
|
143
|
-
@user.lists.find(:all, :name => 'Foo 1').should == [@list1]
|
144
|
-
@user.lists.all(:name => 'Foo 1').should == [@list1]
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
context "first" do
|
149
|
-
should "work" do
|
150
|
-
@user.lists.find(:first, :order => 'position').should == @list1
|
151
|
-
@user.lists.first(:order => 'position').should == @list1
|
152
|
-
end
|
153
|
-
|
154
|
-
should "work with conditions" do
|
155
|
-
@user.lists.find(:first, :position => 2).should == @list2
|
156
|
-
@user.lists.first(:position => 2).should == @list2
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
context "last" do
|
161
|
-
should "work" do
|
162
|
-
@user.lists.find(:last, :order => 'position').should == @list2
|
163
|
-
@user.lists.last(:order => 'position').should == @list2
|
164
|
-
end
|
165
|
-
|
166
|
-
should "work with conditions" do
|
167
|
-
@user.lists.find(:last, :position => 2, :order => 'position').should == @list2
|
168
|
-
@user.lists.last(:position => 2, :order => 'position').should == @list2
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
context "with one id" do
|
173
|
-
should "work for id in association" do
|
174
|
-
@user.lists.find(@list1.id).should == @list1
|
175
|
-
end
|
176
|
-
|
177
|
-
should "work with string ids" do
|
178
|
-
@user.lists.find(@list1.id.to_s).should == @list1
|
179
|
-
end
|
180
|
-
|
181
|
-
should "not work for id not in association" do
|
182
|
-
@user.lists.find(@list3.id).should be_nil
|
183
|
-
end
|
184
|
-
|
185
|
-
should "raise error when using ! and not found" do
|
186
|
-
assert_raises MongoMapper::DocumentNotFound do
|
187
|
-
@user.lists.find!(@list3.id)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
context "with multiple ids" do
|
193
|
-
should "work for ids in association" do
|
194
|
-
@user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
|
195
|
-
end
|
196
|
-
|
197
|
-
should "not work for ids not in association" do
|
198
|
-
@user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
context "with #paginate" do
|
203
|
-
setup do
|
204
|
-
@lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
|
205
|
-
end
|
206
|
-
|
207
|
-
should "return total pages" do
|
208
|
-
@lists.total_pages.should == 2
|
209
|
-
end
|
210
|
-
|
211
|
-
should "return total entries" do
|
212
|
-
@lists.total_entries.should == 2
|
213
|
-
end
|
214
|
-
|
215
|
-
should "return the subject" do
|
216
|
-
@lists.collect(&:name).should == ['Foo 1']
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
context "dynamic finders" do
|
221
|
-
should "work with single key" do
|
222
|
-
@user.lists.find_by_name('Foo 1').should == @list1
|
223
|
-
@user.lists.find_by_name!('Foo 1').should == @list1
|
224
|
-
@user.lists.find_by_name('Foo 3').should be_nil
|
225
|
-
end
|
226
|
-
|
227
|
-
should "work with multiple keys" do
|
228
|
-
@user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
|
229
|
-
@user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
|
230
|
-
@user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
|
231
|
-
end
|
232
|
-
|
233
|
-
should "raise error when using ! and not found" do
|
234
|
-
assert_raises(MongoMapper::DocumentNotFound) do
|
235
|
-
@user.lists.find_by_name!('Foo 3')
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
context "find_or_create_by" do
|
240
|
-
should "not create document if found" do
|
241
|
-
lambda {
|
242
|
-
list = @user.lists.find_or_create_by_name('Foo 1')
|
243
|
-
list.should == @list1
|
244
|
-
}.should_not change { List.count }
|
245
|
-
end
|
246
|
-
|
247
|
-
should "create document if not found" do
|
248
|
-
lambda {
|
249
|
-
list = @user.lists.find_or_create_by_name('Home')
|
250
|
-
@user.lists.should include(list)
|
251
|
-
}.should change { List.count }
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
context "count" do
|
258
|
-
setup do
|
259
|
-
@user = User.create(:name => 'John')
|
260
|
-
@user2 = User.create(:name => 'Brandon')
|
261
|
-
@list1 = @user.lists.create!(:name => 'Foo 1')
|
262
|
-
@list2 = @user.lists.create!(:name => 'Foo 2')
|
263
|
-
@list3 = @user2.lists.create!(:name => 'Foo 3')
|
264
|
-
end
|
265
|
-
|
266
|
-
should "return number of ids" do
|
267
|
-
@user.lists.count.should == 2
|
268
|
-
@user2.lists.count.should == 1
|
269
|
-
end
|
270
|
-
|
271
|
-
should "return correct count when given criteria" do
|
272
|
-
@user.lists.count(:name => 'Foo 1').should == 1
|
273
|
-
@user2.lists.count(:name => 'Foo 1').should == 0
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
context "Removing documents" do
|
278
|
-
setup do
|
279
|
-
@user = User.create(:name => 'John')
|
280
|
-
@user2 = User.create(:name => 'Brandon')
|
281
|
-
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
282
|
-
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
283
|
-
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
284
|
-
end
|
285
|
-
|
286
|
-
context "destroy_all" do
|
287
|
-
should "work" do
|
288
|
-
@user.lists.count.should == 2
|
289
|
-
@user.lists.destroy_all
|
290
|
-
@user.lists.count.should == 0
|
291
|
-
end
|
292
|
-
|
293
|
-
should "work with conditions" do
|
294
|
-
@user.lists.count.should == 2
|
295
|
-
@user.lists.destroy_all(:name => 'Foo 1')
|
296
|
-
@user.lists.count.should == 1
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
context "delete_all" do
|
301
|
-
should "work" do
|
302
|
-
@user.lists.count.should == 2
|
303
|
-
@user.lists.delete_all
|
304
|
-
@user.lists.count.should == 0
|
305
|
-
end
|
306
|
-
|
307
|
-
should "work with conditions" do
|
308
|
-
@user.lists.count.should == 2
|
309
|
-
@user.lists.delete_all(:name => 'Foo 1')
|
310
|
-
@user.lists.count.should == 1
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
should "work with nullify" do
|
315
|
-
@user.lists.count.should == 2
|
316
|
-
|
317
|
-
lambda {
|
318
|
-
@user.lists.nullify
|
319
|
-
}.should_not change { List.count }
|
320
|
-
|
321
|
-
@user.lists.count.should == 0
|
322
|
-
end
|
323
|
-
end
|
324
|
-
end
|
325
|
-
end
|
@@ -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
|