mongo_doc 0.3.2 → 0.4.0
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/README.textile +32 -16
- data/VERSION +1 -1
- data/examples/simple_document.rb +10 -10
- data/features/step_definitions/documents.rb +15 -15
- data/lib/mongo_doc/associations.rb +100 -0
- data/lib/mongo_doc/attributes.rb +20 -104
- data/lib/mongo_doc/contexts/mongo.rb +1 -1
- data/lib/mongo_doc/contexts.rb +1 -1
- data/lib/mongo_doc/document.rb +2 -0
- data/lib/mongo_doc.rb +1 -1
- data/mongo_doc.gemspec +5 -2
- data/spec/associations/collection_proxy_spec.rb +2 -2
- data/spec/associations/hash_proxy_spec.rb +2 -2
- data/spec/associations_spec.rb +230 -0
- data/spec/attributes_accessor_spec.rb +3 -3
- data/spec/attributes_spec.rb +19 -203
- data/spec/contexts/enumerable_spec.rb +2 -2
- data/spec/contexts/ids_spec.rb +2 -2
- data/spec/contexts/mongo_spec.rb +2 -2
- data/spec/contexts_spec.rb +1 -1
- data/spec/document_spec.rb +28 -28
- data/spec/embedded_save_spec.rb +7 -7
- data/spec/finders_spec.rb +1 -1
- data/spec/new_record_spec.rb +5 -5
- data/spec/scope_spec.rb +2 -2
- metadata +7 -4
@@ -0,0 +1,230 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "MongoDoc::Associations" do
|
4
|
+
|
5
|
+
context ".embed" do
|
6
|
+
class TestDoc
|
7
|
+
include MongoDoc::Document
|
8
|
+
|
9
|
+
embed :sub_doc
|
10
|
+
end
|
11
|
+
|
12
|
+
class SubDoc
|
13
|
+
include MongoDoc::Document
|
14
|
+
|
15
|
+
attr_accessor :data
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestHasOne
|
19
|
+
include MongoDoc::Document
|
20
|
+
|
21
|
+
has_one :sub_doc
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:subdoc) { SubDoc.new }
|
25
|
+
let(:doc) { TestDoc.new(:sub_doc => subdoc) }
|
26
|
+
|
27
|
+
it "uses a proxy" do
|
28
|
+
MongoDoc::Associations::DocumentProxy.should === doc.sub_doc
|
29
|
+
end
|
30
|
+
|
31
|
+
it ".has_one is an alias for embed" do
|
32
|
+
MongoDoc::Associations::DocumentProxy.should === TestHasOne.new(:sub_doc => SubDoc.new).sub_doc
|
33
|
+
end
|
34
|
+
|
35
|
+
it "sets the subdocuments parent to the proxy" do
|
36
|
+
doc.sub_doc.should == subdoc._parent
|
37
|
+
end
|
38
|
+
|
39
|
+
it "set the subdocuments root" do
|
40
|
+
doc.should == subdoc._root
|
41
|
+
end
|
42
|
+
|
43
|
+
context "validations" do
|
44
|
+
class EmbedValidationTest
|
45
|
+
include MongoDoc::Document
|
46
|
+
|
47
|
+
attr_accessor :data
|
48
|
+
validates_presence_of :data
|
49
|
+
end
|
50
|
+
|
51
|
+
it "cascades validations down" do
|
52
|
+
invalid = EmbedValidationTest.new
|
53
|
+
TestDoc.new(:sub_doc => invalid).should have(1).error_on(:sub_doc)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context ".embed_many" do
|
59
|
+
|
60
|
+
class SubEmbedManyDoc
|
61
|
+
include MongoDoc::Document
|
62
|
+
|
63
|
+
attr_accessor :data
|
64
|
+
end
|
65
|
+
|
66
|
+
class TestEmbedManyDoc
|
67
|
+
include MongoDoc::Document
|
68
|
+
|
69
|
+
embed_many :sub_docs, :class_name => 'SubEmbedManyDoc'
|
70
|
+
end
|
71
|
+
|
72
|
+
class TestEmbedManyDoc2
|
73
|
+
include MongoDoc::Document
|
74
|
+
|
75
|
+
embed_many :sub_docs, :class_name => :sub_embed_many_doc
|
76
|
+
end
|
77
|
+
|
78
|
+
class TestImplicitEmbedManyDoc
|
79
|
+
include MongoDoc::Document
|
80
|
+
|
81
|
+
embed_many :sub_embed_many_docs
|
82
|
+
end
|
83
|
+
|
84
|
+
class TestHasManyDoc
|
85
|
+
include MongoDoc::Document
|
86
|
+
|
87
|
+
has_many :sub_docs, :class_name => 'SubEmbedManyDoc'
|
88
|
+
end
|
89
|
+
|
90
|
+
let(:subdoc) { SubEmbedManyDoc.new }
|
91
|
+
let(:doc) { TestEmbedManyDoc.new(:sub_docs => [subdoc]) }
|
92
|
+
|
93
|
+
it "uses a proxy" do
|
94
|
+
MongoDoc::Associations::CollectionProxy.should === TestEmbedManyDoc.new.sub_docs
|
95
|
+
end
|
96
|
+
|
97
|
+
it ".has_many is an alias for .embed_many" do
|
98
|
+
MongoDoc::Associations::CollectionProxy.should === TestHasManyDoc.new.sub_docs
|
99
|
+
end
|
100
|
+
|
101
|
+
it "sets the subdocuments parent to the proxy" do
|
102
|
+
doc.sub_docs.should == subdoc._parent
|
103
|
+
end
|
104
|
+
|
105
|
+
it "set the subdocuments root to the root" do
|
106
|
+
doc.should == subdoc._root
|
107
|
+
end
|
108
|
+
|
109
|
+
it "uses the association name to find the children's class name" do
|
110
|
+
TestImplicitEmbedManyDoc.new.sub_embed_many_docs.assoc_class.should == SubEmbedManyDoc
|
111
|
+
end
|
112
|
+
|
113
|
+
it "uses class_name attribute for the children's class name" do
|
114
|
+
TestEmbedManyDoc.new.sub_docs.assoc_class.should == SubEmbedManyDoc
|
115
|
+
end
|
116
|
+
|
117
|
+
it "uses class_name attribute for the children's class name" do
|
118
|
+
TestEmbedManyDoc2.new.sub_docs.assoc_class.should == SubEmbedManyDoc
|
119
|
+
end
|
120
|
+
|
121
|
+
context "validations" do
|
122
|
+
class EmbedManyValidationChild
|
123
|
+
include MongoDoc::Document
|
124
|
+
|
125
|
+
attr_accessor :data
|
126
|
+
validates_presence_of :data
|
127
|
+
end
|
128
|
+
|
129
|
+
class EmbedManyValidationTest
|
130
|
+
include MongoDoc::Document
|
131
|
+
|
132
|
+
embed_many :subdocs, :class_name => 'HasManyValidationChild'
|
133
|
+
end
|
134
|
+
|
135
|
+
let(:invalid_child) { EmbedManyValidationChild.new }
|
136
|
+
let(:doc) { EmbedManyValidationTest.new(:subdocs => [invalid_child]) }
|
137
|
+
|
138
|
+
it "cascades validations and marks it in the parent" do
|
139
|
+
doc.should have(1).error_on(:subdocs)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "cascades validations and marks it in the child" do
|
143
|
+
invalid_child.should have(1).error_on(:data)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "ignores non-document children" do
|
147
|
+
EmbedManyValidationTest.new(:subdocs => ['not a doc']).should be_valid
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context ".embed_hash" do
|
153
|
+
class SubEmbedHashDoc
|
154
|
+
include MongoDoc::Document
|
155
|
+
|
156
|
+
attr_accessor :data
|
157
|
+
end
|
158
|
+
|
159
|
+
class TestEmbedHashDoc
|
160
|
+
include MongoDoc::Document
|
161
|
+
|
162
|
+
embed_hash :sub_docs, :class_name => 'SubEmbedHashDoc'
|
163
|
+
end
|
164
|
+
|
165
|
+
class TestImplicitEmbedHashDoc
|
166
|
+
include MongoDoc::Document
|
167
|
+
|
168
|
+
embed_hash :sub_embed_hash_docs
|
169
|
+
end
|
170
|
+
|
171
|
+
class TestHasHashDoc
|
172
|
+
include MongoDoc::Document
|
173
|
+
|
174
|
+
has_hash :sub_embed_hash_docs
|
175
|
+
end
|
176
|
+
|
177
|
+
let(:subdoc) { SubEmbedHashDoc.new }
|
178
|
+
let(:doc) { TestEmbedHashDoc.new(:sub_docs => {:key => subdoc}) }
|
179
|
+
|
180
|
+
it "uses a proxy" do
|
181
|
+
MongoDoc::Associations::HashProxy.should === TestEmbedHashDoc.new.sub_docs
|
182
|
+
end
|
183
|
+
|
184
|
+
it ".has_hash is an alias for embed_hash" do
|
185
|
+
MongoDoc::Associations::HashProxy.should === TestEmbedHashDoc.new.sub_docs
|
186
|
+
end
|
187
|
+
|
188
|
+
it "sets the subdocuments parent to the proxy" do
|
189
|
+
doc.sub_docs.should == subdoc._parent
|
190
|
+
end
|
191
|
+
|
192
|
+
it "set the subdocuments root to the root" do
|
193
|
+
doc.should == subdoc._root
|
194
|
+
end
|
195
|
+
|
196
|
+
it "uses the association name to find the children's class name" do
|
197
|
+
TestImplicitEmbedHashDoc.new.sub_embed_hash_docs.assoc_class.should == SubEmbedHashDoc
|
198
|
+
end
|
199
|
+
|
200
|
+
context "validations" do
|
201
|
+
class EmbedHashValidationChild
|
202
|
+
include MongoDoc::Document
|
203
|
+
|
204
|
+
attr_accessor :data
|
205
|
+
validates_presence_of :data
|
206
|
+
end
|
207
|
+
|
208
|
+
class EmbedHashValidationTest
|
209
|
+
include MongoDoc::Document
|
210
|
+
|
211
|
+
embed_hash :subdocs, :class_name => 'EmbedHashValidationChild'
|
212
|
+
end
|
213
|
+
|
214
|
+
let(:invalid_child) { EmbedHashValidationChild.new }
|
215
|
+
let(:doc) { EmbedHashValidationTest.new(:subdocs => {:key => invalid_child}) }
|
216
|
+
|
217
|
+
it "cascades validations and marks it in the parent" do
|
218
|
+
doc.should have(1).error_on(:subdocs)
|
219
|
+
end
|
220
|
+
|
221
|
+
it "cascades validations and marks it in the child" do
|
222
|
+
invalid_child.should have(1).error_on(:data)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "ignores non-document children" do
|
226
|
+
EmbedHashValidationTest.new(:subdocs => {:key => 'data'}).should be_valid
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
@@ -4,9 +4,9 @@ describe "MongoDoc::Attributes attributes accessor" do
|
|
4
4
|
class AttributesTest
|
5
5
|
include MongoDoc::Attributes
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :age
|
9
|
+
attr_accessor :birthdate, :type => Date
|
10
10
|
end
|
11
11
|
|
12
12
|
context "#attributes" do
|
data/spec/attributes_spec.rb
CHANGED
@@ -11,10 +11,22 @@ describe "MongoDoc::Attributes" do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context ".key" do
|
14
|
+
class AttributeAccessorTest
|
15
|
+
include MongoDoc::Attributes
|
16
|
+
|
17
|
+
key :date, :default => Date.today, :type => Date
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is an alias for attr_accessor" do
|
21
|
+
AttributeAccessorTest._keys.should include(:date)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context ".attr_accessor" do
|
14
26
|
class TestKeys
|
15
27
|
include MongoDoc::Attributes
|
16
28
|
|
17
|
-
|
29
|
+
attr_accessor :attr1, :attr2
|
18
30
|
end
|
19
31
|
|
20
32
|
it "adds its arguments to _keys" do
|
@@ -39,7 +51,7 @@ describe "MongoDoc::Attributes" do
|
|
39
51
|
class TestDefault
|
40
52
|
include MongoDoc::Attributes
|
41
53
|
|
42
|
-
|
54
|
+
attr_accessor :with_default, :default => 'value'
|
43
55
|
end
|
44
56
|
|
45
57
|
let(:object) { TestDefault.new }
|
@@ -68,7 +80,7 @@ describe "MongoDoc::Attributes" do
|
|
68
80
|
class TestType
|
69
81
|
include MongoDoc::Attributes
|
70
82
|
|
71
|
-
|
83
|
+
attr_accessor :birthdate, :type => Date
|
72
84
|
end
|
73
85
|
|
74
86
|
let(:object) { TestType.new }
|
@@ -97,11 +109,11 @@ describe "MongoDoc::Attributes" do
|
|
97
109
|
class TestParent
|
98
110
|
include MongoDoc::Attributes
|
99
111
|
|
100
|
-
|
112
|
+
attr_accessor :parent_attr
|
101
113
|
end
|
102
114
|
|
103
115
|
class TestChild < TestParent
|
104
|
-
|
116
|
+
attr_accessor :child_attr
|
105
117
|
end
|
106
118
|
|
107
119
|
it "has its own keys" do
|
@@ -118,212 +130,16 @@ describe "MongoDoc::Attributes" do
|
|
118
130
|
end
|
119
131
|
end
|
120
132
|
|
121
|
-
context ".has_one" do
|
122
|
-
class TestDoc
|
123
|
-
include MongoDoc::Document
|
124
|
-
|
125
|
-
has_one :sub_doc
|
126
|
-
end
|
127
|
-
|
128
|
-
class SubDoc
|
129
|
-
include MongoDoc::Document
|
130
|
-
|
131
|
-
key :data
|
132
|
-
end
|
133
|
-
|
134
|
-
let(:subdoc) { SubDoc.new }
|
135
|
-
let(:doc) { TestDoc.new(:sub_doc => subdoc) }
|
136
|
-
|
137
|
-
it "uses a proxy" do
|
138
|
-
MongoDoc::Associations::DocumentProxy.should === doc.sub_doc
|
139
|
-
end
|
140
|
-
|
141
|
-
it "sets the subdocuments parent to the proxy" do
|
142
|
-
doc.sub_doc.should == subdoc._parent
|
143
|
-
end
|
144
|
-
|
145
|
-
it "set the subdocuments root" do
|
146
|
-
doc.should == subdoc._root
|
147
|
-
end
|
148
|
-
|
149
|
-
context "validations" do
|
150
|
-
class HasOneValidationTest
|
151
|
-
include MongoDoc::Document
|
152
|
-
|
153
|
-
key :data
|
154
|
-
validates_presence_of :data
|
155
|
-
end
|
156
|
-
|
157
|
-
it "cascades validations down" do
|
158
|
-
invalid = HasOneValidationTest.new
|
159
|
-
TestDoc.new(:sub_doc => invalid).should have(1).error_on(:sub_doc)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
133
|
context "._attributes" do
|
165
134
|
class TestHasOneDoc
|
166
135
|
include MongoDoc::Document
|
167
136
|
|
168
|
-
|
169
|
-
|
137
|
+
attr_accessor :key
|
138
|
+
embed :embed
|
170
139
|
end
|
171
140
|
|
172
141
|
it "is _keys + _associations" do
|
173
142
|
TestHasOneDoc._attributes.should == TestHasOneDoc._keys + TestHasOneDoc._associations
|
174
143
|
end
|
175
144
|
end
|
176
|
-
|
177
|
-
context ".has_many" do
|
178
|
-
|
179
|
-
class SubHasManyDoc
|
180
|
-
include MongoDoc::Document
|
181
|
-
|
182
|
-
key :data
|
183
|
-
end
|
184
|
-
|
185
|
-
class TestHasManyDoc
|
186
|
-
include MongoDoc::Document
|
187
|
-
|
188
|
-
has_many :sub_docs, :class_name => 'SubHasManyDoc'
|
189
|
-
end
|
190
|
-
|
191
|
-
class TestHasManyDoc2
|
192
|
-
include MongoDoc::Document
|
193
|
-
|
194
|
-
has_many :sub_docs, :class_name => :sub_has_many_doc
|
195
|
-
end
|
196
|
-
|
197
|
-
class TestImplicitHasManyDoc
|
198
|
-
include MongoDoc::Document
|
199
|
-
|
200
|
-
has_many :sub_has_many_docs
|
201
|
-
end
|
202
|
-
|
203
|
-
let(:subdoc) { SubHasManyDoc.new }
|
204
|
-
let(:doc) { TestHasManyDoc.new(:sub_docs => [subdoc]) }
|
205
|
-
|
206
|
-
it "uses a proxy" do
|
207
|
-
MongoDoc::Associations::CollectionProxy.should === TestHasManyDoc.new.sub_docs
|
208
|
-
end
|
209
|
-
|
210
|
-
it "sets the subdocuments parent to the proxy" do
|
211
|
-
doc.sub_docs.should == subdoc._parent
|
212
|
-
end
|
213
|
-
|
214
|
-
it "set the subdocuments root to the root" do
|
215
|
-
doc.should == subdoc._root
|
216
|
-
end
|
217
|
-
|
218
|
-
it "uses the association name to find the children's class name" do
|
219
|
-
TestImplicitHasManyDoc.new.sub_has_many_docs.assoc_class.should == SubHasManyDoc
|
220
|
-
end
|
221
|
-
|
222
|
-
it "uses class_name attribute for the children's class name" do
|
223
|
-
TestHasManyDoc.new.sub_docs.assoc_class.should == SubHasManyDoc
|
224
|
-
end
|
225
|
-
|
226
|
-
it "uses class_name attribute for the children's class name" do
|
227
|
-
TestHasManyDoc2.new.sub_docs.assoc_class.should == SubHasManyDoc
|
228
|
-
end
|
229
|
-
|
230
|
-
context "validations" do
|
231
|
-
class HasManyValidationChild
|
232
|
-
include MongoDoc::Document
|
233
|
-
|
234
|
-
key :data
|
235
|
-
validates_presence_of :data
|
236
|
-
end
|
237
|
-
|
238
|
-
class HasManyValidationTest
|
239
|
-
include MongoDoc::Document
|
240
|
-
|
241
|
-
has_many :subdocs, :class_name => 'HasManyValidationChild'
|
242
|
-
end
|
243
|
-
|
244
|
-
let(:invalid_child) { HasManyValidationChild.new }
|
245
|
-
let(:doc) { HasManyValidationTest.new(:subdocs => [invalid_child]) }
|
246
|
-
|
247
|
-
it "cascades validations and marks it in the parent" do
|
248
|
-
doc.should have(1).error_on(:subdocs)
|
249
|
-
end
|
250
|
-
|
251
|
-
it "cascades validations and marks it in the child" do
|
252
|
-
invalid_child.should have(1).error_on(:data)
|
253
|
-
end
|
254
|
-
|
255
|
-
it "ignores non-document children" do
|
256
|
-
HasManyValidationTest.new(:subdocs => ['not a doc']).should be_valid
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
context ".has_hash" do
|
262
|
-
class SubHasHashDoc
|
263
|
-
include MongoDoc::Document
|
264
|
-
|
265
|
-
key :data
|
266
|
-
end
|
267
|
-
|
268
|
-
class TestHasHashDoc
|
269
|
-
include MongoDoc::Document
|
270
|
-
|
271
|
-
has_hash :sub_docs, :class_name => 'SubHasHashDoc'
|
272
|
-
end
|
273
|
-
|
274
|
-
class TestImplicitHasHashDoc
|
275
|
-
include MongoDoc::Document
|
276
|
-
|
277
|
-
has_hash :sub_has_hash_docs
|
278
|
-
end
|
279
|
-
|
280
|
-
let(:subdoc) { SubHasHashDoc.new }
|
281
|
-
let(:doc) { TestHasHashDoc.new(:sub_docs => {:key => subdoc}) }
|
282
|
-
|
283
|
-
it "uses a proxy" do
|
284
|
-
MongoDoc::Associations::HashProxy.should === TestHasHashDoc.new.sub_docs
|
285
|
-
end
|
286
|
-
|
287
|
-
it "sets the subdocuments parent to the proxy" do
|
288
|
-
doc.sub_docs.should == subdoc._parent
|
289
|
-
end
|
290
|
-
|
291
|
-
it "set the subdocuments root to the root" do
|
292
|
-
doc.should == subdoc._root
|
293
|
-
end
|
294
|
-
|
295
|
-
it "uses the association name to find the children's class name" do
|
296
|
-
TestImplicitHasHashDoc.new.sub_has_hash_docs.assoc_class.should == SubHasHashDoc
|
297
|
-
end
|
298
|
-
|
299
|
-
context "validations" do
|
300
|
-
class HasHashValidationChild
|
301
|
-
include MongoDoc::Document
|
302
|
-
|
303
|
-
key :data
|
304
|
-
validates_presence_of :data
|
305
|
-
end
|
306
|
-
|
307
|
-
class HasHashValidationTest
|
308
|
-
include MongoDoc::Document
|
309
|
-
|
310
|
-
has_hash :subdocs, :class_name => 'HasHashValidationChild'
|
311
|
-
end
|
312
|
-
|
313
|
-
let(:invalid_child) { HasHashValidationChild.new }
|
314
|
-
let(:doc) { HasHashValidationTest.new(:subdocs => {:key => invalid_child}) }
|
315
|
-
|
316
|
-
it "cascades validations and marks it in the parent" do
|
317
|
-
doc.should have(1).error_on(:subdocs)
|
318
|
-
end
|
319
|
-
|
320
|
-
it "cascades validations and marks it in the child" do
|
321
|
-
invalid_child.should have(1).error_on(:data)
|
322
|
-
end
|
323
|
-
|
324
|
-
it "ignores non-document children" do
|
325
|
-
HasHashValidationTest.new(:subdocs => {:key => 'data'}).should be_valid
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
145
|
end
|
data/spec/contexts/ids_spec.rb
CHANGED
data/spec/contexts/mongo_spec.rb
CHANGED