mongomodel 0.4.6 → 0.4.7
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/Appraisals +12 -0
- data/bin/console +1 -2
- data/gemfiles/mongo_mapper.gemfile +12 -0
- data/gemfiles/mongoid.gemfile +12 -0
- data/lib/mongomodel.rb +3 -0
- data/lib/mongomodel/compatibility/mongo_mapper.rb +23 -0
- data/lib/mongomodel/compatibility/mongoid.rb +17 -0
- data/lib/mongomodel/concerns/properties.rb +5 -0
- data/lib/mongomodel/concerns/validations.rb +5 -3
- data/lib/mongomodel/support/core_extensions.rb +4 -4
- data/lib/mongomodel/support/mongo_options.rb +4 -2
- data/lib/mongomodel/support/mongo_order.rb +4 -0
- data/lib/mongomodel/support/scope.rb +1 -1
- data/lib/mongomodel/version.rb +1 -1
- data/spec/mongomodel/attributes/store_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +13 -13
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +25 -25
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +25 -25
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +21 -21
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +3 -3
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +10 -10
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +5 -5
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +5 -5
- data/spec/mongomodel/concerns/attributes_spec.rb +13 -13
- data/spec/mongomodel/concerns/callbacks_spec.rb +11 -11
- data/spec/mongomodel/concerns/logging_spec.rb +2 -2
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +5 -5
- data/spec/mongomodel/concerns/properties_spec.rb +6 -6
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +6 -6
- data/spec/mongomodel/concerns/timestamps_spec.rb +10 -10
- data/spec/mongomodel/concerns/translation_spec.rb +1 -1
- data/spec/mongomodel/concerns/validations_spec.rb +12 -4
- data/spec/mongomodel/document/callbacks_spec.rb +10 -10
- data/spec/mongomodel/document/collection_modifiers_spec.rb +1 -1
- data/spec/mongomodel/document/dynamic_finders_spec.rb +5 -5
- data/spec/mongomodel/document/finders_spec.rb +9 -9
- data/spec/mongomodel/document/indexes_spec.rb +19 -19
- data/spec/mongomodel/document/optimistic_locking_spec.rb +8 -8
- data/spec/mongomodel/document/persistence_spec.rb +38 -38
- data/spec/mongomodel/document/scopes_spec.rb +8 -8
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +9 -9
- data/spec/mongomodel/document/validations_spec.rb +16 -16
- data/spec/mongomodel/document_spec.rb +11 -11
- data/spec/mongomodel/embedded_document_spec.rb +13 -13
- data/spec/mongomodel/mongomodel_spec.rb +4 -4
- data/spec/mongomodel/support/collection_spec.rb +40 -40
- data/spec/mongomodel/support/map_spec.rb +41 -41
- data/spec/mongomodel/support/mongo_operator_spec.rb +11 -9
- data/spec/mongomodel/support/mongo_options_spec.rb +17 -17
- data/spec/mongomodel/support/mongo_order_spec.rb +22 -22
- data/spec/mongomodel/support/property_spec.rb +21 -12
- data/spec/mongomodel/support/scope_spec.rb +134 -134
- data/spec/spec_helper.rb +1 -0
- data/spec/specdoc.opts +0 -3
- metadata +7 -5
@@ -5,11 +5,11 @@ module MongoModel
|
|
5
5
|
describe "optimistic locking" do
|
6
6
|
define_class(:TestDocument, Document)
|
7
7
|
|
8
|
-
it "
|
8
|
+
it "does not lock optimistically by default" do
|
9
9
|
TestDocument.locking_enabled?.should be_false
|
10
10
|
end
|
11
11
|
|
12
|
-
it "
|
12
|
+
it "does not include a lock version property" do
|
13
13
|
TestDocument.properties.should_not include(:_lock_version)
|
14
14
|
end
|
15
15
|
|
@@ -27,28 +27,28 @@ module MongoModel
|
|
27
27
|
@fresh.save!
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
30
|
+
it "is enabled" do
|
31
31
|
TestDocument.locking_enabled?.should be_true
|
32
32
|
end
|
33
33
|
|
34
|
-
it "
|
34
|
+
it "defines a lock version property" do
|
35
35
|
TestDocument.properties.should include(:_lock_version)
|
36
36
|
end
|
37
37
|
|
38
|
-
it "
|
38
|
+
it "saves a fresh document" do
|
39
39
|
@fresh.save.should be_true
|
40
40
|
end
|
41
41
|
|
42
|
-
it "
|
42
|
+
it "saves! a fresh document" do
|
43
43
|
@fresh.save!.should be_true
|
44
44
|
end
|
45
45
|
|
46
|
-
it "
|
46
|
+
it "does not save a stale document" do
|
47
47
|
@stale.save.should be_false
|
48
48
|
@stale._lock_version.should == 1
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "raises an error when trying to save! a stale document" do
|
52
52
|
lambda { @stale.save! }.should raise_error(DocumentNotSaved)
|
53
53
|
end
|
54
54
|
end
|
@@ -14,11 +14,11 @@ module MongoModel
|
|
14
14
|
it { should be_a_new_record }
|
15
15
|
|
16
16
|
describe "#save" do
|
17
|
-
it "
|
17
|
+
it "returns true" do
|
18
18
|
subject.save.should == true
|
19
19
|
end
|
20
20
|
|
21
|
-
it "
|
21
|
+
it "persists the document to the collection" do
|
22
22
|
subject.save
|
23
23
|
|
24
24
|
doc = User.collection.find_one
|
@@ -30,7 +30,7 @@ module MongoModel
|
|
30
30
|
context "with a custom id" do
|
31
31
|
subject { User.new(:id => 'custom-id') }
|
32
32
|
|
33
|
-
it "
|
33
|
+
it "saves the document using the custom id" do
|
34
34
|
subject.save
|
35
35
|
User.collection.find_one['_id'].should == 'custom-id'
|
36
36
|
end
|
@@ -45,17 +45,17 @@ module MongoModel
|
|
45
45
|
it { should_not be_a_new_record }
|
46
46
|
|
47
47
|
describe "#save" do
|
48
|
-
it "
|
48
|
+
it "returns true" do
|
49
49
|
subject.save.should == true
|
50
50
|
end
|
51
51
|
|
52
|
-
it "
|
52
|
+
it "does not create a new document" do
|
53
53
|
lambda {
|
54
54
|
subject.save
|
55
55
|
}.should_not change(User.collection, :count)
|
56
56
|
end
|
57
57
|
|
58
|
-
it "
|
58
|
+
it "updates the document attributes" do
|
59
59
|
subject.attributes[:name] = 'Changed'
|
60
60
|
subject.save
|
61
61
|
|
@@ -66,11 +66,11 @@ module MongoModel
|
|
66
66
|
end
|
67
67
|
|
68
68
|
describe "#collection_name" do
|
69
|
-
it "
|
69
|
+
it "infers the default collection name" do
|
70
70
|
User.collection_name.should == 'users'
|
71
71
|
end
|
72
72
|
|
73
|
-
it "
|
73
|
+
it "infers the default collection name for namespaced models" do
|
74
74
|
module ::Blog
|
75
75
|
class Post < MongoModel::Document; end
|
76
76
|
end
|
@@ -78,7 +78,7 @@ module MongoModel
|
|
78
78
|
::Blog::Post.collection_name.should == 'blog.posts'
|
79
79
|
end
|
80
80
|
|
81
|
-
it "
|
81
|
+
it "allows a custom collection name" do
|
82
82
|
class ::CustomCollectionName < Document
|
83
83
|
self.collection_name = 'foobar'
|
84
84
|
end
|
@@ -86,7 +86,7 @@ module MongoModel
|
|
86
86
|
::CustomCollectionName.collection_name.should == 'foobar'
|
87
87
|
end
|
88
88
|
|
89
|
-
it "
|
89
|
+
it "inherits a custom collection name" do
|
90
90
|
class ::CustomCollectionName < Document
|
91
91
|
self.collection_name = 'foobar'
|
92
92
|
end
|
@@ -97,34 +97,34 @@ module MongoModel
|
|
97
97
|
end
|
98
98
|
|
99
99
|
describe "#collection" do
|
100
|
-
it "
|
100
|
+
it "is an instrumented collection" do
|
101
101
|
User.collection.should be_a(InstrumentedCollection)
|
102
102
|
end
|
103
103
|
|
104
|
-
it "
|
104
|
+
it "uses the correct collection name" do
|
105
105
|
User.collection.name.should == 'users'
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
describe "#database" do
|
110
|
-
it "
|
110
|
+
it "returns the current database" do
|
111
111
|
User.database.should == MongoModel.database
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
115
|
describe "#create" do
|
116
116
|
context "attributes hash" do
|
117
|
-
it "
|
117
|
+
it "passes attributes to instance" do
|
118
118
|
@user = User.create(:name => 'Test', :age => 18)
|
119
119
|
@user.name.should == 'Test'
|
120
120
|
@user.age.should == 18
|
121
121
|
end
|
122
122
|
|
123
|
-
it "
|
123
|
+
it "saves the instance" do
|
124
124
|
User.create.should_not be_a_new_record
|
125
125
|
end
|
126
126
|
|
127
|
-
it "
|
127
|
+
it "yields the instance to a given block before saving" do
|
128
128
|
block_called = false
|
129
129
|
|
130
130
|
User.create do |u|
|
@@ -143,7 +143,7 @@ module MongoModel
|
|
143
143
|
User.create([{ :name => 'Test', :age => 18 }, { :name => 'Second', :age => 21 }], &block)
|
144
144
|
end
|
145
145
|
|
146
|
-
it "
|
146
|
+
it "returns instances in array with associated attributes" do
|
147
147
|
@users = create_users
|
148
148
|
@users[0].name.should == 'Test'
|
149
149
|
@users[0].age.should == 18
|
@@ -151,11 +151,11 @@ module MongoModel
|
|
151
151
|
@users[1].age.should == 21
|
152
152
|
end
|
153
153
|
|
154
|
-
it "
|
154
|
+
it "saves each instance" do
|
155
155
|
create_users.each { |user| user.should_not be_a_new_record }
|
156
156
|
end
|
157
157
|
|
158
|
-
it "
|
158
|
+
it "yields each instance to a given block before saving" do
|
159
159
|
block_called = 0
|
160
160
|
|
161
161
|
create_users do |u|
|
@@ -177,14 +177,14 @@ module MongoModel
|
|
177
177
|
User.create(:id => 'user-3')
|
178
178
|
end
|
179
179
|
|
180
|
-
it "
|
180
|
+
it "deletes by id" do
|
181
181
|
User.delete('user-1')
|
182
182
|
|
183
183
|
User.exists?('user-1').should be_false
|
184
184
|
User.exists?('user-2').should be_true
|
185
185
|
end
|
186
186
|
|
187
|
-
it "
|
187
|
+
it "deletes by multiple ids in array" do
|
188
188
|
User.delete(['user-1', 'user-2'])
|
189
189
|
|
190
190
|
User.exists?('user-1').should be_false
|
@@ -199,23 +199,23 @@ module MongoModel
|
|
199
199
|
User.create(:id => 'user-2', :name => 'Another')
|
200
200
|
end
|
201
201
|
|
202
|
-
it "
|
202
|
+
it "deletes the instance from the database" do
|
203
203
|
@user.delete
|
204
204
|
|
205
205
|
User.exists?('user-1').should be_false
|
206
206
|
User.exists?('user-2').should be_true
|
207
207
|
end
|
208
208
|
|
209
|
-
it "
|
209
|
+
it "returns the instance" do
|
210
210
|
@user.delete.should == @user
|
211
211
|
end
|
212
212
|
|
213
|
-
it "
|
213
|
+
it "freezes the instance" do
|
214
214
|
@user.delete
|
215
215
|
@user.should be_frozen
|
216
216
|
end
|
217
217
|
|
218
|
-
it "
|
218
|
+
it "marks the instance as destroyed" do
|
219
219
|
@user.delete
|
220
220
|
@user.should be_destroyed
|
221
221
|
end
|
@@ -227,23 +227,23 @@ module MongoModel
|
|
227
227
|
User.create(:id => 'user-2', :name => 'Another')
|
228
228
|
end
|
229
229
|
|
230
|
-
it "
|
230
|
+
it "deletes the instance from the database" do
|
231
231
|
@user.destroy
|
232
232
|
|
233
233
|
User.exists?('user-1').should be_false
|
234
234
|
User.exists?('user-2').should be_true
|
235
235
|
end
|
236
236
|
|
237
|
-
it "
|
237
|
+
it "returns the instance" do
|
238
238
|
@user.destroy.should == @user
|
239
239
|
end
|
240
240
|
|
241
|
-
it "
|
241
|
+
it "freezes the instance" do
|
242
242
|
@user.destroy
|
243
243
|
@user.should be_frozen
|
244
244
|
end
|
245
245
|
|
246
|
-
it "
|
246
|
+
it "marks the instance as destroyed" do
|
247
247
|
@user.destroy
|
248
248
|
@user.should be_destroyed
|
249
249
|
end
|
@@ -256,14 +256,14 @@ module MongoModel
|
|
256
256
|
User.create(:id => 'user-3')
|
257
257
|
end
|
258
258
|
|
259
|
-
it "
|
259
|
+
it "destroys by id" do
|
260
260
|
User.destroy('user-1')
|
261
261
|
|
262
262
|
User.exists?('user-1').should be_false
|
263
263
|
User.exists?('user-2').should be_true
|
264
264
|
end
|
265
265
|
|
266
|
-
it "
|
266
|
+
it "destroys by multiple ids in array" do
|
267
267
|
User.destroy(['user-1', 'user-2'])
|
268
268
|
|
269
269
|
User.exists?('user-1').should be_false
|
@@ -277,12 +277,12 @@ module MongoModel
|
|
277
277
|
|
278
278
|
before(:each) { user.update_attributes(:name => 'Changed', :age => 20) }
|
279
279
|
|
280
|
-
it "
|
280
|
+
it "updates the attributes" do
|
281
281
|
user.name.should == 'Changed'
|
282
282
|
user.age.should == 20
|
283
283
|
end
|
284
284
|
|
285
|
-
it "
|
285
|
+
it "saves the document" do
|
286
286
|
user.should_not be_a_new_record
|
287
287
|
end
|
288
288
|
end
|
@@ -292,11 +292,11 @@ module MongoModel
|
|
292
292
|
|
293
293
|
before(:each) { user.update_attribute(:name, 'Changed') }
|
294
294
|
|
295
|
-
it "
|
295
|
+
it "updates the given attribute" do
|
296
296
|
user.name.should == 'Changed'
|
297
297
|
end
|
298
298
|
|
299
|
-
it "
|
299
|
+
it "saves the document" do
|
300
300
|
user.should_not be_a_new_record
|
301
301
|
end
|
302
302
|
end
|
@@ -313,11 +313,11 @@ module MongoModel
|
|
313
313
|
|
314
314
|
subject { UserComment.create!(:title => "Test", :user => user) }
|
315
315
|
|
316
|
-
it "
|
316
|
+
it "returns itself" do
|
317
317
|
subject.reload.should == subject
|
318
318
|
end
|
319
319
|
|
320
|
-
it "
|
320
|
+
it "resets the attributes" do
|
321
321
|
subject.title = "New Value"
|
322
322
|
subject.body = "Blah blah blah"
|
323
323
|
subject.reload
|
@@ -325,7 +325,7 @@ module MongoModel
|
|
325
325
|
subject.body.should == nil
|
326
326
|
end
|
327
327
|
|
328
|
-
it "
|
328
|
+
it "resets the associations" do
|
329
329
|
subject.user.should == user
|
330
330
|
subject.user = User.new(:name => "Bill")
|
331
331
|
subject.reload
|
@@ -9,7 +9,7 @@ module MongoModel
|
|
9
9
|
|
10
10
|
it { should be_an_instance_of(MongoModel::Scope) }
|
11
11
|
|
12
|
-
it "
|
12
|
+
it "sets the target class" do
|
13
13
|
subject.klass.should == Post
|
14
14
|
end
|
15
15
|
|
@@ -19,7 +19,7 @@ module MongoModel
|
|
19
19
|
default_scope limit(5)
|
20
20
|
end
|
21
21
|
|
22
|
-
it "
|
22
|
+
it "returns the default scope" do
|
23
23
|
scope = Post.scoped
|
24
24
|
scope.order_values.should == [:title.asc]
|
25
25
|
scope.limit_value.should == 5
|
@@ -27,7 +27,7 @@ module MongoModel
|
|
27
27
|
end
|
28
28
|
|
29
29
|
context "within a with_scope block" do
|
30
|
-
it "
|
30
|
+
it "returns the current scope" do
|
31
31
|
Post.class_eval do
|
32
32
|
with_scope(where(:published => true)) do
|
33
33
|
scoped.where_values.should == [{:published => true}]
|
@@ -37,7 +37,7 @@ module MongoModel
|
|
37
37
|
end
|
38
38
|
|
39
39
|
context "within nested with_scope blocks" do
|
40
|
-
it "
|
40
|
+
it "returns the merged scope" do
|
41
41
|
Post.class_eval do
|
42
42
|
with_scope(where(:published => true)) do
|
43
43
|
with_scope(limit(5)) do
|
@@ -51,7 +51,7 @@ module MongoModel
|
|
51
51
|
end
|
52
52
|
|
53
53
|
describe "#scope" do
|
54
|
-
it "
|
54
|
+
it "creates a method returning the scope" do
|
55
55
|
Post.class_eval do
|
56
56
|
scope :published, where(:published => true)
|
57
57
|
end
|
@@ -61,7 +61,7 @@ module MongoModel
|
|
61
61
|
scope.where_values.should == [{:published => true}]
|
62
62
|
end
|
63
63
|
|
64
|
-
it "
|
64
|
+
it "creates parameterized method returning the scope when given a lambda" do
|
65
65
|
Post.class_eval do
|
66
66
|
scope :latest, lambda { |num| order(:created_at.desc).limit(num) }
|
67
67
|
end
|
@@ -71,7 +71,7 @@ module MongoModel
|
|
71
71
|
scope.limit_value.should == 4
|
72
72
|
end
|
73
73
|
|
74
|
-
it "
|
74
|
+
it "allows existing scopes to be built upon" do
|
75
75
|
Post.class_eval do
|
76
76
|
scope :recent, order(:created_at.desc).limit(5)
|
77
77
|
scope :recently_published, recent.where(:published => true)
|
@@ -90,7 +90,7 @@ module MongoModel
|
|
90
90
|
scope :recent, order(:created_at.desc).limit(5)
|
91
91
|
end
|
92
92
|
|
93
|
-
it "
|
93
|
+
it "is chainable" do
|
94
94
|
scope = Post.published.recent
|
95
95
|
scope.where_values.should == [{:published => true}]
|
96
96
|
scope.order_values.should == [:created_at.desc]
|
@@ -9,22 +9,22 @@ module MongoModel
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "save" do
|
12
|
-
it "
|
12
|
+
it "returns false" do
|
13
13
|
subject.save.should be_false
|
14
14
|
end
|
15
15
|
|
16
|
-
it "
|
16
|
+
it "adds errors to the instance" do
|
17
17
|
subject.save
|
18
18
|
subject.errors[:title].should_not be_nil
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe "save!" do
|
23
|
-
it "
|
23
|
+
it "raises a DocumentInvalid exception" do
|
24
24
|
lambda { subject.save! }.should raise_error(DocumentInvalid)
|
25
25
|
end
|
26
26
|
|
27
|
-
it "
|
27
|
+
it "adds errors to the instance" do
|
28
28
|
subject.save! rescue nil
|
29
29
|
subject.errors[:title].should_not be_nil
|
30
30
|
end
|
@@ -39,26 +39,26 @@ module MongoModel
|
|
39
39
|
|
40
40
|
subject { Article.new(:title => 'Test') }
|
41
41
|
|
42
|
-
it "
|
42
|
+
it "is valid if no document with same title exists" do
|
43
43
|
subject.should be_valid
|
44
44
|
end
|
45
45
|
|
46
|
-
it "
|
46
|
+
it "is not valid if document with same title exists" do
|
47
47
|
Article.create!(:title => 'Test')
|
48
48
|
subject.should_not be_valid
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "is valid if document with different-cased title exists" do
|
52
52
|
Article.create!(:title => 'TEST')
|
53
53
|
subject.should be_valid
|
54
54
|
end
|
55
55
|
|
56
|
-
it "
|
56
|
+
it "is valid if document already saved and no other document with same title exists" do
|
57
57
|
subject.save!
|
58
58
|
subject.should be_valid
|
59
59
|
end
|
60
60
|
|
61
|
-
it "
|
61
|
+
it "generates correct error message" do
|
62
62
|
Article.create!(:title => 'Test')
|
63
63
|
subject.valid?
|
64
64
|
subject.errors[:title].should include('has already been taken')
|