mongo_mapper 0.13.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +61 -0
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +7 -0
- data/lib/mongo_mapper/connection.rb +16 -37
- data/lib/mongo_mapper/document.rb +4 -0
- data/lib/mongo_mapper/extensions/array.rb +14 -6
- data/lib/mongo_mapper/extensions/hash.rb +15 -3
- data/lib/mongo_mapper/extensions/object.rb +4 -0
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/extensions/string.rb +13 -5
- data/lib/mongo_mapper/extensions/symbol.rb +18 -0
- data/lib/mongo_mapper/plugins/accessible.rb +15 -5
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +27 -14
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +60 -29
- data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +36 -29
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
- data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
- data/lib/mongo_mapper/plugins/indexes.rb +14 -7
- data/lib/mongo_mapper/plugins/keys.rb +170 -151
- data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
- data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
- data/lib/mongo_mapper/plugins/persistence.rb +13 -8
- data/lib/mongo_mapper/plugins/protected.rb +6 -5
- data/lib/mongo_mapper/plugins/querying.rb +85 -42
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +20 -15
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/sci.rb +0 -0
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/railtie.rb +4 -3
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
- data/spec/examples.txt +1717 -0
- data/spec/functional/accessible_spec.rb +19 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +36 -20
- data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
- data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
- data/spec/functional/associations/one_proxy_spec.rb +10 -10
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +8 -15
- data/spec/functional/callbacks_spec.rb +89 -2
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +63 -46
- data/spec/functional/document_spec.rb +30 -5
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +17 -17
- data/spec/functional/identity_map_spec.rb +29 -16
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +86 -28
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +77 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +9 -9
- data/spec/functional/scopes_spec.rb +235 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +86 -0
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/touch_spec.rb +1 -1
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +37 -9
- data/spec/support/matchers.rb +5 -14
- data/spec/unit/associations/base_spec.rb +12 -12
- data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
- data/spec/unit/associations/many_association_spec.rb +2 -2
- data/spec/unit/associations/one_association_spec.rb +2 -2
- data/spec/unit/associations/proxy_spec.rb +19 -20
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +8 -8
- data/spec/unit/dynamic_finder_spec.rb +8 -8
- data/spec/unit/embedded_document_spec.rb +18 -19
- data/spec/unit/extensions_spec.rb +41 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +20 -11
- data/spec/unit/model_generator_spec.rb +0 -0
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/rails_spec.rb +5 -0
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +46 -33
- metadata +66 -37
- data/README.rdoc +0 -59
- data/lib/mongo_mapper/connections/10gen.rb +0 -0
- data/lib/mongo_mapper/connections/moped.rb +0 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
data/spec/functional/sci_spec.rb
CHANGED
@@ -3,12 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe "Single collection inheritance (document)" do
|
4
4
|
context "without a connection", :without_connection => true do
|
5
5
|
it "should attempt to create a connection during inheritance" do
|
6
|
-
Mongo::
|
6
|
+
Mongo::Client.should_not_receive(:new)
|
7
7
|
doc = Class.new
|
8
8
|
doc.send(:include, MongoMapper::Document)
|
9
|
-
|
9
|
+
lambda {
|
10
10
|
Class.new(doc)
|
11
|
-
}.
|
11
|
+
}.should_not raise_error
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should pick up a connection if one wasn't set" do
|
@@ -17,7 +17,7 @@ describe "Single collection inheritance (document)" do
|
|
17
17
|
klass = Class.new(doc)
|
18
18
|
klass.connection.should be_nil
|
19
19
|
MongoMapper.connection
|
20
|
-
klass.connection.should be_a Mongo::
|
20
|
+
klass.connection.should be_a Mongo::Client
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -27,7 +27,7 @@ describe "Single collection inheritance (document)" do
|
|
27
27
|
include MongoMapper::Document
|
28
28
|
key :name, String
|
29
29
|
end
|
30
|
-
DocParent.collection.
|
30
|
+
DocParent.collection.drop
|
31
31
|
|
32
32
|
class ::DocDaughter < ::DocParent; end
|
33
33
|
class ::DocSon < ::DocParent; end
|
@@ -49,7 +49,7 @@ describe "Single collection inheritance (document)" do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should automatically add _type key to store class" do
|
52
|
-
DocParent.key?(:_type).should
|
52
|
+
DocParent.key?(:_type).should be_truthy
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should use modifiers properly" do
|
@@ -61,7 +61,7 @@ describe "Single collection inheritance (document)" do
|
|
61
61
|
it "should use the same connection in the subclass" do
|
62
62
|
parent_class = Class.new do
|
63
63
|
include MongoMapper::Document
|
64
|
-
connection Mongo::
|
64
|
+
connection Mongo::Client.new(['127.0.0.1:27017'])
|
65
65
|
end
|
66
66
|
|
67
67
|
child_class = Class.new(parent_class) do
|
@@ -323,7 +323,7 @@ describe "Single collection inheritance (document)" do
|
|
323
323
|
end
|
324
324
|
|
325
325
|
it "should automatically add _type key" do
|
326
|
-
Grandparent.key?(:_type).should
|
326
|
+
Grandparent.key?(:_type).should be_truthy
|
327
327
|
end
|
328
328
|
|
329
329
|
context ".single_collection_inherited?" do
|
@@ -377,4 +377,4 @@ describe "Single collection inheritance (document)" do
|
|
377
377
|
p.reload.article_parent.sci_polymorphic_posts.all.should include(p)
|
378
378
|
end
|
379
379
|
end
|
380
|
-
end
|
380
|
+
end
|
@@ -66,7 +66,7 @@ describe "Scopes" do
|
|
66
66
|
@document.class_eval do
|
67
67
|
scope :age, lambda { |age| {:age => age} }
|
68
68
|
scope :ages, lambda { |low, high| {:age.gte => low, :age.lte => high} }
|
69
|
-
scope :ordered, lambda { |
|
69
|
+
scope :ordered, lambda { |s| sort(s) }
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
@@ -170,7 +170,7 @@ describe "Scopes" do
|
|
170
170
|
key :title, String
|
171
171
|
key :published_at, Time
|
172
172
|
end
|
173
|
-
Item.collection.
|
173
|
+
Item.collection.drop
|
174
174
|
|
175
175
|
class ::Page < ::Item; end
|
176
176
|
class ::Blog < ::Item
|
@@ -201,4 +201,237 @@ describe "Scopes" do
|
|
201
201
|
end
|
202
202
|
end
|
203
203
|
end
|
204
|
+
|
205
|
+
describe "with_scope" do
|
206
|
+
describe "with a base query" do
|
207
|
+
before do
|
208
|
+
@klass = Doc do
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should scope" do
|
213
|
+
u1 = @klass.create!(:first_name => "Scott")
|
214
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
215
|
+
|
216
|
+
run = false
|
217
|
+
|
218
|
+
@klass.with_scope(:first_name => "Scott") do
|
219
|
+
run = true
|
220
|
+
@klass.all.should == [u1]
|
221
|
+
end
|
222
|
+
|
223
|
+
run.should == true
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should return the scope at the end of the block" do
|
227
|
+
u1 = @klass.create!(:first_name => "Scott")
|
228
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
229
|
+
|
230
|
+
run = false
|
231
|
+
|
232
|
+
@klass.with_scope(:first_name => "Scott") do
|
233
|
+
run = true
|
234
|
+
@klass.all.should == [u1]
|
235
|
+
end
|
236
|
+
|
237
|
+
run.should == true
|
238
|
+
|
239
|
+
@klass.all.should include(u1)
|
240
|
+
@klass.all.should include(u2)
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should be able to use an unscoped query" do
|
244
|
+
u1 = @klass.create!(:first_name => "Scott")
|
245
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
246
|
+
|
247
|
+
run = false
|
248
|
+
|
249
|
+
@klass.with_scope(:first_name => "Scott") do
|
250
|
+
@klass.unscoped do
|
251
|
+
run = true
|
252
|
+
@klass.all.should include(u1)
|
253
|
+
@klass.all.should include(u2)
|
254
|
+
end
|
255
|
+
|
256
|
+
@klass.all.should == [u1]
|
257
|
+
end
|
258
|
+
|
259
|
+
run.should == true
|
260
|
+
|
261
|
+
@klass.all.should include(u1)
|
262
|
+
@klass.all.should include(u2)
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should return the value of the block" do
|
266
|
+
u1 = @klass.create!(:first_name => "Scott")
|
267
|
+
u2 = @klass.create!(:first_name => "Andrew")
|
268
|
+
|
269
|
+
result = @klass.with_scope(:first_name => "Scott") do
|
270
|
+
@klass.all
|
271
|
+
end
|
272
|
+
|
273
|
+
result.should == [u1]
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should have an empty list of default scope" do
|
277
|
+
@klass.default_scopes.should == []
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should apply the default scope to all queries" do
|
281
|
+
@klass.key :active, Boolean, :default => true
|
282
|
+
|
283
|
+
normal = @klass.create!
|
284
|
+
inactive = @klass.create!(:active => false)
|
285
|
+
|
286
|
+
@klass.default_scope do
|
287
|
+
{
|
288
|
+
:active => true
|
289
|
+
}
|
290
|
+
end
|
291
|
+
|
292
|
+
@klass.all.should == [normal]
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should allow multiple default scopes" do
|
296
|
+
@klass.key :active, Boolean, :default => true
|
297
|
+
@klass.key :soft_deleted, Boolean, :default => false
|
298
|
+
|
299
|
+
normal = @klass.create!
|
300
|
+
inactive = @klass.create!(:active => false)
|
301
|
+
deleted = @klass.create!(:soft_deleted => true)
|
302
|
+
|
303
|
+
@klass.default_scope do
|
304
|
+
{
|
305
|
+
:active => true,
|
306
|
+
}
|
307
|
+
end
|
308
|
+
|
309
|
+
@klass.default_scope do
|
310
|
+
{
|
311
|
+
:soft_deleted => false
|
312
|
+
}
|
313
|
+
end
|
314
|
+
|
315
|
+
@klass.all.should == [normal]
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should allow a where inside a default_scope" do
|
319
|
+
@klass.key :active, Boolean, :default => true
|
320
|
+
|
321
|
+
normal = @klass.create!
|
322
|
+
inactive = @klass.create!(:active => false)
|
323
|
+
|
324
|
+
@klass.default_scope do
|
325
|
+
where(:active => true)
|
326
|
+
end
|
327
|
+
|
328
|
+
@klass.all.should == [normal]
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should allow a hash directly" do
|
332
|
+
@klass.key :active, Boolean, :default => true
|
333
|
+
|
334
|
+
normal = @klass.create!
|
335
|
+
inactive = @klass.create!(:active => false)
|
336
|
+
|
337
|
+
@klass.default_scope :active => true
|
338
|
+
|
339
|
+
@klass.all.should == [normal]
|
340
|
+
end
|
341
|
+
|
342
|
+
it "should inherit default scopes, but only downwards" do
|
343
|
+
@klass.default_scope do
|
344
|
+
where(:active => true)
|
345
|
+
end
|
346
|
+
@subclass = Class.new(@klass)
|
347
|
+
@subclass.default_scopes.length.should == 1
|
348
|
+
|
349
|
+
@subclass.default_scope do
|
350
|
+
{
|
351
|
+
:foo => :bar
|
352
|
+
}
|
353
|
+
end
|
354
|
+
|
355
|
+
@subclass.default_scopes.length.should == 2
|
356
|
+
@klass.default_scopes.length.should == 1
|
357
|
+
end
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
describe "regression - multiple named scopes" do
|
362
|
+
it "should merge the hashes from multiple scopes" do
|
363
|
+
klass = Doc do
|
364
|
+
key :a, Boolean
|
365
|
+
key :b, Boolean
|
366
|
+
key :c, Boolean
|
367
|
+
|
368
|
+
scope :one, where(:a => true, :b => true)
|
369
|
+
scope :two, where(:c => true)
|
370
|
+
end
|
371
|
+
|
372
|
+
obj = klass.create!(:a => false, :b => true, :c => true)
|
373
|
+
klass.one.two.all.should == []
|
374
|
+
|
375
|
+
obj2 = klass.create(:a => true, :b => true, :c => true)
|
376
|
+
klass.one.two.all.should == [obj2]
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
describe "regression - with multiple scopes that set a key to nil" do
|
381
|
+
it "should still have the query as nil" do
|
382
|
+
klass = Doc do
|
383
|
+
key :a, Boolean
|
384
|
+
|
385
|
+
scope :one, lambda {
|
386
|
+
{
|
387
|
+
:a => nil
|
388
|
+
}
|
389
|
+
}
|
390
|
+
scope :two, lambda {
|
391
|
+
{
|
392
|
+
:a => nil
|
393
|
+
}
|
394
|
+
}
|
395
|
+
end
|
396
|
+
|
397
|
+
obj = klass.create!(:a => nil)
|
398
|
+
klass.one.two.all.should == [obj]
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe "regression - multiple scopes leading to empty query (array bug?)" do
|
403
|
+
before do
|
404
|
+
@klass = Doc do
|
405
|
+
key :send_at, Time
|
406
|
+
|
407
|
+
scope :unsent, lambda {
|
408
|
+
now = Time.now
|
409
|
+
|
410
|
+
{
|
411
|
+
send_at: {
|
412
|
+
'$lte' => now,
|
413
|
+
'$gte' => now - 30.days,
|
414
|
+
},
|
415
|
+
sent_at: nil,
|
416
|
+
}
|
417
|
+
}
|
418
|
+
|
419
|
+
scope :sorted, lambda {
|
420
|
+
sort(send_at: 1)
|
421
|
+
}
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should filter properly by dates" do
|
426
|
+
old_obj = @klass.create(send_at: 3.months.ago)
|
427
|
+
one_day_ago = @klass.create(send_at: 1.day.ago)
|
428
|
+
two_days_ago = @klass.create(send_at: 2.days.ago)
|
429
|
+
|
430
|
+
all_unsorted = @klass.unsent.all
|
431
|
+
|
432
|
+
all_unsorted.should include(one_day_ago)
|
433
|
+
all_unsorted.should include(two_days_ago)
|
434
|
+
@klass.unsent.sorted.all.should == [two_days_ago, one_day_ago]
|
435
|
+
end
|
436
|
+
end
|
204
437
|
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoMapper::Plugins::Keys::Static do
|
4
|
+
before do
|
5
|
+
@klass = Class.new do
|
6
|
+
include MongoMapper::Document
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is off by default" do
|
11
|
+
@klass.static_keys.should == false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can be turned on" do
|
15
|
+
@klass.static_keys = true
|
16
|
+
@klass.static_keys.should == true
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "a class with static keys" do
|
20
|
+
before do
|
21
|
+
@klass = Class.new do
|
22
|
+
include MongoMapper::Document
|
23
|
+
|
24
|
+
self.static_keys = true
|
25
|
+
|
26
|
+
attr_accessor :an_accessor
|
27
|
+
|
28
|
+
key "valid_key", String
|
29
|
+
end
|
30
|
+
|
31
|
+
@obj = @klass.new
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be able to assign to a valid key" do
|
35
|
+
@obj.valid_key = "foo"
|
36
|
+
@obj.valid_key.should == "foo"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise a MissingKeyError if trying to assign to a field that does not exist" do
|
40
|
+
lambda {
|
41
|
+
@obj['foo'] = "foo"
|
42
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should raise a MissingKeyError if trying to read from a field that does not exist" do
|
46
|
+
lambda {
|
47
|
+
@obj['foo']
|
48
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should raise a MissingKeyError if trying to set attributes= to a field that does not exist" do
|
52
|
+
lambda {
|
53
|
+
@obj.attributes = {:foo => "foo"}
|
54
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should be able to call a method" do
|
58
|
+
@obj.an_accessor = :bar
|
59
|
+
@obj.an_accessor.should == :bar
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should raise a NoMethodError if trying to call the non-existant key method" do
|
63
|
+
lambda {
|
64
|
+
@obj.foo
|
65
|
+
}.should raise_error(NoMethodError)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should raise for read_key" do
|
69
|
+
lambda {
|
70
|
+
@obj.read_key('foo')
|
71
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should raise for attribute" do
|
75
|
+
lambda {
|
76
|
+
@obj.attribute('foo')
|
77
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not raise if trying to assign to a field that exists (but with a symbol instead of a string)" do
|
81
|
+
lambda {
|
82
|
+
@obj.write_key(:valid_key, "foo")
|
83
|
+
}.should_not raise_error
|
84
|
+
|
85
|
+
@obj.valid_key.should == "foo"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not raise a MissingKeyError if trying to read from a field that exists (but with a symbol instead of a string)" do
|
89
|
+
lambda {
|
90
|
+
@obj[:valid_key]
|
91
|
+
}.should_not raise_error
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should not blow up when loading if there is a key defined in the db that has not been defined (but it should not load it)" do
|
95
|
+
@klass.collection.insert_one({ :foo => "bar", :valid_key => "something" })
|
96
|
+
@obj = @klass.first
|
97
|
+
@obj.valid_key.should == "something"
|
98
|
+
|
99
|
+
lambda {
|
100
|
+
@obj.foo
|
101
|
+
}.should raise_error(NoMethodError)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "a class without static keys" do
|
106
|
+
before do
|
107
|
+
@klass = Class.new do
|
108
|
+
include MongoMapper::Document
|
109
|
+
key :valid_key, String
|
110
|
+
end
|
111
|
+
|
112
|
+
@obj = @klass.new
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should be able to assign to a valid key" do
|
116
|
+
@obj.valid_key = "foo"
|
117
|
+
@obj.valid_key.should == "foo"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should be able to read and assign a dynamic key" do
|
121
|
+
@obj['foo'].should be_nil
|
122
|
+
@obj['foo'] = "bar"
|
123
|
+
@obj['foo'].should == "bar"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "with mixed static + non static models" do
|
128
|
+
before do
|
129
|
+
@static_model = Class.new do
|
130
|
+
include MongoMapper::Document
|
131
|
+
self.static_keys = true
|
132
|
+
end
|
133
|
+
|
134
|
+
@non_static_model = Class.new do
|
135
|
+
include MongoMapper::Document
|
136
|
+
self.static_keys = false
|
137
|
+
end
|
138
|
+
|
139
|
+
@static_key_object = @static_model.new
|
140
|
+
@non_static_key_object = @non_static_model.new
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should allow non-static key assignment to one, only static assignment to the other" do
|
144
|
+
@non_static_key_object['foo'] = 'bar'
|
145
|
+
@non_static_key_object.foo.should == 'bar'
|
146
|
+
@non_static_key_object['foo'].should == 'bar'
|
147
|
+
|
148
|
+
lambda {
|
149
|
+
@static_key_object['foo'] = 'bar'
|
150
|
+
}.should raise_error(MongoMapper::Plugins::Keys::Static::MissingKeyError)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|