mongomodel 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
@@ -7,10 +7,10 @@ module MongoModel
|
|
7
7
|
before(:each) do
|
8
8
|
subject.stub(:valid?).and_return(true, false)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
describe "save" do
|
12
12
|
it "returns false" do
|
13
|
-
subject.save.should
|
13
|
+
subject.save.should be false
|
14
14
|
end
|
15
15
|
|
16
16
|
it "adds errors to the instance" do
|
@@ -30,105 +30,105 @@ module MongoModel
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
describe "indexes" do
|
35
35
|
define_class(:Article, Document) do
|
36
36
|
property :title, String
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "creates an index on the attribute" do
|
40
40
|
Article.should_receive(:index).with(:title, :unique => true)
|
41
41
|
Article.validates_uniqueness_of :title
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
it "creates an index on the lowercase attribute if :case_sensitive => false" do
|
45
45
|
Article.should_receive(:index).with("_lowercase_title", :unique => true)
|
46
46
|
Article.validates_uniqueness_of :title, :case_sensitive => false
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
it "creates a compound index when a scope is passed" do
|
50
50
|
Article.should_receive(:index).with(:title, :author_id, :unique => true)
|
51
51
|
Article.validates_uniqueness_of :title, :scope => :author_id
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it "does not create an index if :index => false" do
|
55
55
|
Article.should_not_receive(:index)
|
56
56
|
Article.validates_uniqueness_of :title, :index => false
|
57
57
|
end
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
describe "basic case" do
|
61
61
|
define_class(:Article, Document) do
|
62
62
|
property :title, String
|
63
63
|
validates_uniqueness_of :title
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
subject { Article.new(:title => 'Test') }
|
67
|
-
|
67
|
+
|
68
68
|
it "is valid if no document with same title exists" do
|
69
69
|
subject.should be_valid
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
it "is not valid if document with same title exists" do
|
73
73
|
Article.create!(:title => 'Test')
|
74
74
|
subject.should_not be_valid
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "is valid if document with different-cased title exists" do
|
78
78
|
Article.create!(:title => 'TEST')
|
79
79
|
subject.should be_valid
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
it "is valid if document already saved and no other document with same title exists" do
|
83
83
|
subject.save!
|
84
84
|
subject.should be_valid
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
it "generates correct error message" do
|
88
88
|
Article.create!(:title => 'Test')
|
89
89
|
subject.valid?
|
90
90
|
subject.errors[:title].should include('has already been taken')
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
describe "beating the race condition" do
|
94
94
|
before(:each) { Article.create!(:title => 'Test') }
|
95
95
|
it_should_behave_like "beating the race condition"
|
96
96
|
end
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
describe "with single scope" do
|
100
100
|
define_class(:Article, Document) do
|
101
101
|
property :title, String
|
102
102
|
property :category, String
|
103
103
|
validates_uniqueness_of :title, :scope => :category
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
before(:each) do
|
107
107
|
Article.create!(:title => 'Test', :category => 'Development')
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
subject { Article.new(:title => 'Test', :category => 'Testing') }
|
111
|
-
|
111
|
+
|
112
112
|
describe "no document with same title and category exists" do
|
113
113
|
it { should be_valid }
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
describe "document with same title and category exists" do
|
117
117
|
before(:each) { Article.create!(:title => 'Test', :category => 'Testing') }
|
118
118
|
it { should_not be_valid }
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
describe "document already saved" do
|
122
122
|
before(:each) { subject.save! }
|
123
123
|
it { should be_valid }
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
describe "beating the race condition" do
|
127
127
|
before(:each) { Article.create!(:title => 'Test', :category => 'Testing') }
|
128
128
|
it_should_behave_like "beating the race condition"
|
129
129
|
end
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
describe "with array scope" do
|
133
133
|
define_class(:Article, Document) do
|
134
134
|
property :title, String
|
@@ -136,90 +136,90 @@ module MongoModel
|
|
136
136
|
property :year, Integer
|
137
137
|
validates_uniqueness_of :title, :scope => [:category, :year]
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
before(:each) do
|
141
141
|
Article.create!(:title => 'Test', :category => 'Development', :year => 2008)
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
subject { Article.new(:title => 'Test', :category => 'Testing', :year => 2009) }
|
145
|
-
|
145
|
+
|
146
146
|
describe "no document with same title and category exists" do
|
147
147
|
it { should be_valid }
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
describe "document with same title and category exists" do
|
151
151
|
before(:each) { Article.create!(:title => 'Test', :category => 'Testing', :year => 2009) }
|
152
152
|
it { should_not be_valid }
|
153
153
|
end
|
154
|
-
|
154
|
+
|
155
155
|
describe "document already saved" do
|
156
156
|
before(:each) { subject.save! }
|
157
157
|
it { should be_valid }
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
describe "beating the race condition" do
|
161
161
|
before(:each) { Article.create!(:title => 'Test', :category => 'Testing', :year => 2009) }
|
162
162
|
it_should_behave_like "beating the race condition"
|
163
163
|
end
|
164
164
|
end
|
165
|
-
|
165
|
+
|
166
166
|
describe "case insensitive" do
|
167
167
|
define_class(:Article, Document) do
|
168
168
|
property :title, String
|
169
169
|
validates_uniqueness_of :title, :case_sensitive => false
|
170
170
|
end
|
171
|
-
|
171
|
+
|
172
172
|
subject { Article.new(:title => 'Test') }
|
173
|
-
|
173
|
+
|
174
174
|
describe "no document with same title exists" do
|
175
175
|
it { should be_valid }
|
176
176
|
end
|
177
|
-
|
177
|
+
|
178
178
|
describe "document with same title exists" do
|
179
179
|
before(:each) { Article.create!(:title => 'Test') }
|
180
180
|
it { should_not be_valid }
|
181
181
|
end
|
182
|
-
|
182
|
+
|
183
183
|
describe "document with different-cased title exists" do
|
184
184
|
before(:each) { Article.create!(:title => 'TEST') }
|
185
185
|
it { should_not be_valid }
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
describe "document already saved" do
|
189
189
|
before(:each) { subject.save! }
|
190
190
|
it { should be_valid }
|
191
191
|
end
|
192
|
-
|
192
|
+
|
193
193
|
describe "beating the race condition" do
|
194
194
|
before(:each) { Article.create!(:title => 'TEST') }
|
195
195
|
it_should_behave_like "beating the race condition"
|
196
196
|
end
|
197
197
|
end
|
198
|
-
|
198
|
+
|
199
199
|
describe "validation on parent class" do
|
200
200
|
define_class(:Article, Document) do
|
201
201
|
property :title, String
|
202
202
|
validates_uniqueness_of :title
|
203
203
|
end
|
204
204
|
define_class(:SpecialArticle, :Article)
|
205
|
-
|
205
|
+
|
206
206
|
subject { SpecialArticle.new(:title => 'Test') }
|
207
|
-
|
207
|
+
|
208
208
|
describe "no document with same title exists" do
|
209
209
|
it { should be_valid }
|
210
210
|
end
|
211
|
-
|
211
|
+
|
212
212
|
describe "parent document with same title exists" do
|
213
213
|
before(:each) { Article.create!(:title => 'Test') }
|
214
214
|
it { should_not be_valid }
|
215
215
|
end
|
216
|
-
|
216
|
+
|
217
217
|
describe "child document with same title exists" do
|
218
218
|
before(:each) { SpecialArticle.create!(:title => 'Test') }
|
219
219
|
it { should_not be_valid }
|
220
220
|
end
|
221
221
|
end
|
222
|
-
|
222
|
+
|
223
223
|
describe "validation on child class" do
|
224
224
|
define_class(:Article, Document) do
|
225
225
|
property :title, String
|
@@ -227,18 +227,18 @@ module MongoModel
|
|
227
227
|
define_class(:SpecialArticle, :Article) do
|
228
228
|
validates_uniqueness_of :title
|
229
229
|
end
|
230
|
-
|
230
|
+
|
231
231
|
subject { SpecialArticle.new(:title => 'Test') }
|
232
|
-
|
232
|
+
|
233
233
|
describe "no document with same title exists" do
|
234
234
|
it { should be_valid }
|
235
235
|
end
|
236
|
-
|
236
|
+
|
237
237
|
describe "parent document with same title exists" do
|
238
238
|
before(:each) { Article.create!(:title => 'Test') }
|
239
239
|
it { should be_valid }
|
240
240
|
end
|
241
|
-
|
241
|
+
|
242
242
|
describe "child document with same title exists" do
|
243
243
|
before(:each) { SpecialArticle.create!(:title => 'Test') }
|
244
244
|
it { should_not be_valid }
|
@@ -6,29 +6,29 @@ module MongoModel
|
|
6
6
|
define_class(:TestDocument, Document) do
|
7
7
|
property :title, String
|
8
8
|
validates_presence_of :title
|
9
|
-
|
9
|
+
|
10
10
|
extend ValidationHelpers
|
11
11
|
end
|
12
12
|
|
13
13
|
context "when validations are not met" do
|
14
14
|
subject { TestDocument.new }
|
15
|
-
|
15
|
+
|
16
16
|
describe "#save" do
|
17
17
|
it "returns false" do
|
18
|
-
subject.save.should
|
18
|
+
subject.save.should be false
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
describe "#save!" do
|
23
23
|
before(:each) do
|
24
24
|
subject.errors.stub(:full_messages).and_return(["first error", "second error"])
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
it "raises a MongoModel::DocumentInvalid exception" do
|
28
28
|
lambda { subject.save! }.should raise_error(MongoModel::DocumentInvalid, "Validation failed: first error, second error")
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
shared_examples_for "saving without validation" do
|
33
33
|
it "does not validate the document" do
|
34
34
|
subject.should_not_receive(:valid?)
|
@@ -41,38 +41,38 @@ module MongoModel
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it "returns true" do
|
44
|
-
save.should
|
44
|
+
save.should be true
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
describe "#save(false) [deprecated save without validations]" do
|
49
49
|
def save
|
50
50
|
subject.save(false)
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
it_should_behave_like "saving without validation"
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
describe "#save(:validate => false)" do
|
57
57
|
def save
|
58
58
|
subject.save(:validate => false)
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
it_should_behave_like "saving without validation"
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
describe "#save(:context => :custom)" do
|
65
65
|
before(:each) do
|
66
66
|
TestDocument.clear_validations!
|
67
67
|
TestDocument.validates_presence_of :title, :on => :custom
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
it "saves in default context" do
|
71
|
-
subject.save.should
|
71
|
+
subject.save.should be true
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
it "does not save in custom context" do
|
75
|
-
subject.save(:context => :custom).should
|
75
|
+
subject.save(:context => :custom).should be false
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
@@ -81,44 +81,44 @@ module MongoModel
|
|
81
81
|
define_class(:User, Document) do
|
82
82
|
property :name, String
|
83
83
|
property :age, Integer
|
84
|
-
|
84
|
+
|
85
85
|
validates_presence_of :name
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
context "attributes hash" do
|
89
89
|
it "passes attributes to instance" do
|
90
90
|
@user = User.create!(:name => 'Test', :age => 18)
|
91
91
|
@user.name.should == 'Test'
|
92
92
|
@user.age.should == 18
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it "saves! the instance" do
|
96
96
|
User.create!(:name => 'Test').should_not be_a_new_record
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
it "yields the instance to a given block before saving" do
|
100
100
|
block_called = false
|
101
|
-
|
101
|
+
|
102
102
|
User.create!(:name => 'Test') do |u|
|
103
103
|
block_called = true
|
104
|
-
|
104
|
+
|
105
105
|
u.should be_an_instance_of(User)
|
106
106
|
u.should be_a_new_record
|
107
107
|
end
|
108
|
-
|
109
|
-
block_called.should
|
108
|
+
|
109
|
+
block_called.should be true
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
it "raises an exception if the document is invalid" do
|
113
113
|
lambda { User.create! }.should raise_error(DocumentInvalid)
|
114
114
|
end
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
context "array of attribute hashes" do
|
118
118
|
def create_users(&block)
|
119
119
|
User.create!([{ :name => 'Test', :age => 18 }, { :name => 'Second', :age => 21 }], &block)
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
it "returns instances in array with associated attributes" do
|
123
123
|
@users = create_users
|
124
124
|
@users[0].name.should == 'Test'
|
@@ -126,34 +126,34 @@ module MongoModel
|
|
126
126
|
@users[1].name.should == 'Second'
|
127
127
|
@users[1].age.should == 21
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
it "saves! each instance" do
|
131
131
|
create_users.each { |user| user.should_not be_a_new_record }
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
it "yields each instance to a given block before saving" do
|
135
135
|
block_called = 0
|
136
|
-
|
136
|
+
|
137
137
|
create_users do |u|
|
138
138
|
block_called += 1
|
139
|
-
|
139
|
+
|
140
140
|
u.should be_an_instance_of(User)
|
141
141
|
u.should be_a_new_record
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
block_called.should == 2
|
145
145
|
end
|
146
|
-
|
146
|
+
|
147
147
|
it "raises an exception if a document is invalid" do
|
148
148
|
lambda { User.create!([ {}, {} ]) }.should raise_error(DocumentInvalid)
|
149
149
|
end
|
150
150
|
end
|
151
151
|
end
|
152
152
|
end
|
153
|
-
|
153
|
+
|
154
154
|
describe "validation shortcuts" do
|
155
155
|
define_class(:TestDocument, Document)
|
156
|
-
|
156
|
+
|
157
157
|
describe ":unique => true" do
|
158
158
|
it "adds a validates_uniqueness_of validation" do
|
159
159
|
TestDocument.should_receive(:validates_uniqueness_of).with(:title)
|