drogus-mongo_mapper 0.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +10 -0
- data/LICENSE +20 -0
- data/README.rdoc +29 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper.rb +131 -0
- data/lib/mongo_mapper/document.rb +417 -0
- data/lib/mongo_mapper/embedded_document.rb +55 -0
- data/lib/mongo_mapper/finder_options.rb +127 -0
- data/lib/mongo_mapper/plugins.rb +30 -0
- data/lib/mongo_mapper/plugins/associations.rb +104 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +134 -0
- data/lib/mongo_mapper/plugins/clone.rb +13 -0
- data/lib/mongo_mapper/plugins/descendants.rb +16 -0
- data/lib/mongo_mapper/plugins/dirty.rb +119 -0
- data/lib/mongo_mapper/plugins/equality.rb +23 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
- data/lib/mongo_mapper/plugins/inspect.rb +14 -0
- data/lib/mongo_mapper/plugins/keys.rb +324 -0
- data/lib/mongo_mapper/plugins/logger.rb +17 -0
- data/lib/mongo_mapper/plugins/pagination.rb +85 -0
- data/lib/mongo_mapper/plugins/protected.rb +45 -0
- data/lib/mongo_mapper/plugins/rails.rb +45 -0
- data/lib/mongo_mapper/plugins/serialization.rb +105 -0
- data/lib/mongo_mapper/plugins/validations.rb +57 -0
- data/lib/mongo_mapper/support.rb +217 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
- data/lib/mongo_mapper/support/find.rb +77 -0
- data/mongo_mapper.gemspec +195 -0
- data/performance/read_write.rb +52 -0
- data/specs.watchr +51 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
- data/test/functional/associations/test_in_array_proxy.rb +309 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +431 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_proxy.rb +161 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_callbacks.rb +81 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +1264 -0
- data/test/functional/test_embedded_document.rb +125 -0
- data/test/functional/test_identity_map.rb +508 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +252 -0
- data/test/functional/test_pagination.rb +93 -0
- data/test/functional/test_protected.rb +155 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_validations.rb +329 -0
- data/test/models.rb +232 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +60 -0
- data/test/unit/associations/test_base.rb +207 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +231 -0
- data/test/unit/test_dynamic_finder.rb +123 -0
- data/test/unit/test_embedded_document.rb +663 -0
- data/test/unit/test_finder_options.rb +329 -0
- data/test/unit/test_keys.rb +169 -0
- data/test/unit/test_mongo_mapper.rb +65 -0
- data/test/unit/test_pagination.rb +127 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +123 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_support.rb +354 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +544 -0
- metadata +290 -0
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@post_class = Doc()
|
7
|
+
@comment_class = Doc do
|
8
|
+
key :post_id, String
|
9
|
+
end
|
10
|
+
|
11
|
+
@comment_class.belongs_to :post, :class => @post_class
|
12
|
+
end
|
13
|
+
|
14
|
+
should "default to nil" do
|
15
|
+
@comment_class.new.post.nil?.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
should "send object id to target" do
|
19
|
+
post = @post_class.new(:name => 'mongomapper')
|
20
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
21
|
+
comment.save
|
22
|
+
|
23
|
+
comment.post.object_id.should == comment.post.target.object_id
|
24
|
+
end
|
25
|
+
|
26
|
+
should "have boolean presence method" do
|
27
|
+
comment = @comment_class.new(:name => 'Foo!')
|
28
|
+
comment.post?.should be_false
|
29
|
+
|
30
|
+
comment.post = @post_class.new(:name => 'mongomapper')
|
31
|
+
comment.post?.should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be able to replace the association" do
|
35
|
+
post = @post_class.new(:name => 'mongomapper')
|
36
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
37
|
+
comment.save.should be_true
|
38
|
+
|
39
|
+
comment = comment.reload
|
40
|
+
comment.post.should == post
|
41
|
+
comment.post.nil?.should be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
should "unset the association" do
|
45
|
+
post = @post_class.new(:name => 'mongomapper')
|
46
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
47
|
+
comment.save.should be_true
|
48
|
+
|
49
|
+
comment = comment.reload
|
50
|
+
comment.post = nil
|
51
|
+
comment.post.nil?.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return nil if id set but document not found" do
|
55
|
+
id = Mongo::ObjectID.new
|
56
|
+
@comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
context ":dependent" do
|
60
|
+
setup do
|
61
|
+
# FIXME: make use of already defined models
|
62
|
+
class ::Property
|
63
|
+
include MongoMapper::Document
|
64
|
+
end
|
65
|
+
Property.collection.remove
|
66
|
+
|
67
|
+
class ::Thing
|
68
|
+
include MongoMapper::Document
|
69
|
+
key :name, String
|
70
|
+
end
|
71
|
+
Thing.collection.remove
|
72
|
+
end
|
73
|
+
|
74
|
+
teardown do
|
75
|
+
Object.send :remove_const, 'Property' if defined?(::Property)
|
76
|
+
Object.send :remove_const, 'Thing' if defined?(::Thing)
|
77
|
+
end
|
78
|
+
|
79
|
+
context "=> destroy" do
|
80
|
+
setup do
|
81
|
+
Property.key :thing_id, ObjectId
|
82
|
+
Property.belongs_to :thing, :dependent => :destroy
|
83
|
+
Thing.many :properties
|
84
|
+
|
85
|
+
@thing = Thing.create(:name => "Tree")
|
86
|
+
@property1 = Property.create
|
87
|
+
@property2 = Property.create
|
88
|
+
@property3 = Property.create
|
89
|
+
@thing.properties << @property1
|
90
|
+
@thing.properties << @property2
|
91
|
+
@thing.properties << @property3
|
92
|
+
end
|
93
|
+
|
94
|
+
should "not execute on a belongs_to association" do
|
95
|
+
Thing.count.should == 1
|
96
|
+
@property1.destroy
|
97
|
+
Thing.count.should == 1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,309 @@
|
|
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
|
+
end
|
84
|
+
|
85
|
+
context "create!" do
|
86
|
+
setup do
|
87
|
+
@user = User.create(:name => 'John')
|
88
|
+
@list = @user.lists.create!(:name => 'Foo!')
|
89
|
+
end
|
90
|
+
|
91
|
+
should "add id to key" do
|
92
|
+
@user.list_ids.should include(@list.id)
|
93
|
+
end
|
94
|
+
|
95
|
+
should "persist id addition to key in database" do
|
96
|
+
@user.reload
|
97
|
+
@user.list_ids.should include(@list.id)
|
98
|
+
end
|
99
|
+
|
100
|
+
should "add doc to association" do
|
101
|
+
@user.lists.should include(@list)
|
102
|
+
end
|
103
|
+
|
104
|
+
should "save doc" do
|
105
|
+
@list.should_not be_new
|
106
|
+
end
|
107
|
+
|
108
|
+
should "raise exception if invalid" do
|
109
|
+
assert_raises(MongoMapper::DocumentNotValid) do
|
110
|
+
@user.lists.create!
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "Finding scoped to association" do
|
116
|
+
setup do
|
117
|
+
@user = User.create(:name => 'John')
|
118
|
+
@user2 = User.create(:name => 'Brandon')
|
119
|
+
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
120
|
+
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
121
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
122
|
+
end
|
123
|
+
|
124
|
+
context "all" do
|
125
|
+
should "work" do
|
126
|
+
@user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
|
127
|
+
@user.lists.all(:order => :position.asc).should == [@list1, @list2]
|
128
|
+
end
|
129
|
+
|
130
|
+
should "work with conditions" do
|
131
|
+
@user.lists.find(:all, :name => 'Foo 1').should == [@list1]
|
132
|
+
@user.lists.all(:name => 'Foo 1').should == [@list1]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "first" do
|
137
|
+
should "work" do
|
138
|
+
@user.lists.find(:first, :order => 'position').should == @list1
|
139
|
+
@user.lists.first(:order => 'position').should == @list1
|
140
|
+
end
|
141
|
+
|
142
|
+
should "work with conditions" do
|
143
|
+
@user.lists.find(:first, :position => 2).should == @list2
|
144
|
+
@user.lists.first(:position => 2).should == @list2
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "last" do
|
149
|
+
should "work" do
|
150
|
+
@user.lists.find(:last, :order => 'position').should == @list2
|
151
|
+
@user.lists.last(:order => 'position').should == @list2
|
152
|
+
end
|
153
|
+
|
154
|
+
should "work with conditions" do
|
155
|
+
@user.lists.find(:last, :position => 2, :order => 'position').should == @list2
|
156
|
+
@user.lists.last(:position => 2, :order => 'position').should == @list2
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context "with one id" do
|
161
|
+
should "work for id in association" do
|
162
|
+
@user.lists.find(@list1.id).should == @list1
|
163
|
+
end
|
164
|
+
|
165
|
+
should "not work for id not in association" do
|
166
|
+
@user.lists.find(@list3.id).should be_nil
|
167
|
+
end
|
168
|
+
|
169
|
+
should "raise error when using ! and not found" do
|
170
|
+
assert_raises MongoMapper::DocumentNotFound do
|
171
|
+
@user.lists.find!(@list3.id)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "with multiple ids" do
|
177
|
+
should "work for ids in association" do
|
178
|
+
@user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
|
179
|
+
end
|
180
|
+
|
181
|
+
should "not work for ids not in association" do
|
182
|
+
@user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "with #paginate" do
|
187
|
+
setup do
|
188
|
+
@lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
|
189
|
+
end
|
190
|
+
|
191
|
+
should "return total pages" do
|
192
|
+
@lists.total_pages.should == 2
|
193
|
+
end
|
194
|
+
|
195
|
+
should "return total entries" do
|
196
|
+
@lists.total_entries.should == 2
|
197
|
+
end
|
198
|
+
|
199
|
+
should "return the subject" do
|
200
|
+
@lists.collect(&:name).should == ['Foo 1']
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "dynamic finders" do
|
205
|
+
should "work with single key" do
|
206
|
+
@user.lists.find_by_name('Foo 1').should == @list1
|
207
|
+
@user.lists.find_by_name!('Foo 1').should == @list1
|
208
|
+
@user.lists.find_by_name('Foo 3').should be_nil
|
209
|
+
end
|
210
|
+
|
211
|
+
should "work with multiple keys" do
|
212
|
+
@user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
|
213
|
+
@user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
|
214
|
+
@user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
|
215
|
+
end
|
216
|
+
|
217
|
+
should "raise error when using ! and not found" do
|
218
|
+
assert_raises(MongoMapper::DocumentNotFound) do
|
219
|
+
@user.lists.find_by_name!('Foo 3')
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context "find_or_create_by" do
|
224
|
+
should "not create document if found" do
|
225
|
+
lambda {
|
226
|
+
list = @user.lists.find_or_create_by_name('Foo 1')
|
227
|
+
list.should == @list1
|
228
|
+
}.should_not change { List.count }
|
229
|
+
end
|
230
|
+
|
231
|
+
should "create document if not found" do
|
232
|
+
lambda {
|
233
|
+
list = @user.lists.find_or_create_by_name('Home')
|
234
|
+
@user.lists.should include(list)
|
235
|
+
}.should change { List.count }
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context "count" do
|
242
|
+
setup do
|
243
|
+
@user = User.create(:name => 'John')
|
244
|
+
@user2 = User.create(:name => 'Brandon')
|
245
|
+
@list1 = @user.lists.create!(:name => 'Foo 1')
|
246
|
+
@list2 = @user.lists.create!(:name => 'Foo 2')
|
247
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3')
|
248
|
+
end
|
249
|
+
|
250
|
+
should "return number of ids" do
|
251
|
+
@user.lists.count.should == 2
|
252
|
+
@user2.lists.count.should == 1
|
253
|
+
end
|
254
|
+
|
255
|
+
should "return correct count when given criteria" do
|
256
|
+
@user.lists.count(:name => 'Foo 1').should == 1
|
257
|
+
@user2.lists.count(:name => 'Foo 1').should == 0
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context "Removing documents" do
|
262
|
+
setup do
|
263
|
+
@user = User.create(:name => 'John')
|
264
|
+
@user2 = User.create(:name => 'Brandon')
|
265
|
+
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
266
|
+
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
267
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
268
|
+
end
|
269
|
+
|
270
|
+
context "destroy_all" do
|
271
|
+
should "work" do
|
272
|
+
@user.lists.count.should == 2
|
273
|
+
@user.lists.destroy_all
|
274
|
+
@user.lists.count.should == 0
|
275
|
+
end
|
276
|
+
|
277
|
+
should "work with conditions" do
|
278
|
+
@user.lists.count.should == 2
|
279
|
+
@user.lists.destroy_all(:name => 'Foo 1')
|
280
|
+
@user.lists.count.should == 1
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
context "delete_all" do
|
285
|
+
should "work" do
|
286
|
+
@user.lists.count.should == 2
|
287
|
+
@user.lists.delete_all
|
288
|
+
@user.lists.count.should == 0
|
289
|
+
end
|
290
|
+
|
291
|
+
should "work with conditions" do
|
292
|
+
@user.lists.count.should == 2
|
293
|
+
@user.lists.delete_all(:name => 'Foo 1')
|
294
|
+
@user.lists.count.should == 1
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
should "work with nullify" do
|
299
|
+
@user.lists.count.should == 2
|
300
|
+
|
301
|
+
lambda {
|
302
|
+
@user.lists.nullify
|
303
|
+
}.should_not change { List.count }
|
304
|
+
|
305
|
+
@user.lists.count.should == 0
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
@@ -0,0 +1,229 @@
|
|
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
|