mongoid 4.0.0 → 4.0.2

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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -1
  3. data/README.md +6 -2
  4. data/lib/config/locales/en.yml +2 -2
  5. data/lib/mongoid/atomic.rb +2 -2
  6. data/lib/mongoid/attributes.rb +2 -0
  7. data/lib/mongoid/contextual/aggregable/memory.rb +2 -2
  8. data/lib/mongoid/contextual/memory.rb +5 -5
  9. data/lib/mongoid/contextual/mongo.rb +13 -3
  10. data/lib/mongoid/criteria/#findable.rb# +141 -0
  11. data/lib/mongoid/document.rb +7 -7
  12. data/lib/mongoid/extensions.rb +13 -0
  13. data/lib/mongoid/findable.rb +27 -5
  14. data/lib/mongoid/persistable/creatable.rb +2 -1
  15. data/lib/mongoid/persistable/settable.rb +1 -1
  16. data/lib/mongoid/persistable/updatable.rb +2 -1
  17. data/lib/mongoid/query_cache.rb +10 -2
  18. data/lib/mongoid/railtie.rb +2 -15
  19. data/lib/mongoid/railties/database.rake +1 -1
  20. data/lib/mongoid/relations/accessors.rb +2 -2
  21. data/lib/mongoid/relations/binding.rb +1 -1
  22. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +1 -1
  23. data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
  24. data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
  25. data/lib/mongoid/relations/counter_cache.rb +2 -2
  26. data/lib/mongoid/relations/many.rb +21 -0
  27. data/lib/mongoid/relations/one.rb +1 -1
  28. data/lib/mongoid/relations/referenced/many.rb +4 -4
  29. data/lib/mongoid/relations/referenced/many_to_many.rb +5 -5
  30. data/lib/mongoid/relations/synchronization.rb +4 -4
  31. data/lib/mongoid/relations/targets/enumerable.rb +10 -10
  32. data/lib/mongoid/reloadable.rb +3 -3
  33. data/lib/mongoid/sessions/options.rb +7 -2
  34. data/lib/mongoid/threaded.rb +26 -15
  35. data/lib/mongoid/traversable.rb +6 -2
  36. data/lib/mongoid/validatable/uniqueness.rb +3 -3
  37. data/lib/mongoid/version.rb +1 -1
  38. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +33 -1
  39. data/spec/app/models/contextable_item.rb +5 -0
  40. data/spec/app/models/id_key.rb +6 -0
  41. data/spec/mongoid/#atomic_spec.rb# +365 -0
  42. data/spec/mongoid/attributes/nested_spec.rb +139 -144
  43. data/spec/mongoid/attributes_spec.rb +36 -0
  44. data/spec/mongoid/contextual/atomic_spec.rb +7 -13
  45. data/spec/mongoid/contextual/memory_spec.rb +7 -2
  46. data/spec/mongoid/criteria/modifiable_spec.rb +5 -8
  47. data/spec/mongoid/criteria_spec.rb +86 -75
  48. data/spec/mongoid/document_spec.rb +9 -5
  49. data/spec/mongoid/extensions_spec.rb +14 -0
  50. data/spec/mongoid/findable_spec.rb +99 -11
  51. data/spec/mongoid/persistable/creatable_spec.rb +41 -0
  52. data/spec/mongoid/persistable/savable_spec.rb +37 -0
  53. data/spec/mongoid/persistable/settable_spec.rb +23 -0
  54. data/spec/mongoid/positional_spec.rb +5 -10
  55. data/spec/mongoid/query_cache_spec.rb +32 -0
  56. data/spec/mongoid/relations/embedded/many_spec.rb +74 -0
  57. data/spec/mongoid/relations/referenced/many_spec.rb +153 -0
  58. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +49 -0
  59. data/spec/mongoid/reloadable_spec.rb +23 -0
  60. data/spec/mongoid/sessions/options_spec.rb +2 -1
  61. data/spec/mongoid/sessions_spec.rb +30 -0
  62. data/spec/spec_helper.rb +2 -0
  63. metadata +10 -3
@@ -248,6 +248,17 @@ describe Mongoid::Attributes do
248
248
  it "delegates to #id" do
249
249
  expect(person._id).to eq(person.id)
250
250
  end
251
+
252
+ context "when #id alias is overridden" do
253
+
254
+ let(:object) do
255
+ IdKey.new(key: 'foo')
256
+ end
257
+
258
+ it "delegates to another method" do
259
+ expect(object.id).to eq(object.key)
260
+ end
261
+ end
251
262
  end
252
263
 
253
264
  describe "#_id=" do
@@ -314,6 +325,19 @@ describe Mongoid::Attributes do
314
325
  expect(person.id).to eq(2)
315
326
  end
316
327
  end
328
+
329
+ context "when #id= alias is overridden" do
330
+
331
+ let(:object) do
332
+ IdKey.new(key: 'foo')
333
+ end
334
+
335
+ it "delegates to another method" do
336
+ object.id = 'bar'
337
+ expect(object.id).to eq('bar')
338
+ end
339
+ end
340
+
317
341
  end
318
342
 
319
343
  context "when using string ids" do
@@ -853,6 +877,18 @@ describe Mongoid::Attributes do
853
877
  expect(person.attribute_present?(:title)).to be false
854
878
  end
855
879
  end
880
+
881
+ context "when the attribute is not on only list" do
882
+
883
+ before { Person.create }
884
+ let(:person) do
885
+ Person.only(:id).first
886
+ end
887
+
888
+ it "return false" do
889
+ expect(person.attribute_present?(:foobar)).to be false
890
+ end
891
+ end
856
892
  end
857
893
 
858
894
  describe "#has_attribute?" do
@@ -103,10 +103,8 @@ describe Mongoid::Contextual::Atomic do
103
103
  expect(depeche_mode.reload.likes).to eq(12)
104
104
  end
105
105
 
106
- if mongodb_version > "2.5"
107
- it "does not error on non initialized fields" do
108
- expect(smiths.reload.likes).to eq(0)
109
- end
106
+ it "does not error on non initialized fields" do
107
+ expect(smiths.reload.likes).to eq(0)
110
108
  end
111
109
  end
112
110
 
@@ -120,10 +118,8 @@ describe Mongoid::Contextual::Atomic do
120
118
  expect(depeche_mode.reload.likes).to eq(61)
121
119
  end
122
120
 
123
- if mongodb_version > "2.5"
124
- it "does not error on non initialized fields" do
125
- expect(smiths.reload.likes).to eq(13)
126
- end
121
+ it "does not error on non initialized fields" do
122
+ expect(smiths.reload.likes).to eq(13)
127
123
  end
128
124
  end
129
125
 
@@ -137,13 +133,11 @@ describe Mongoid::Contextual::Atomic do
137
133
  expect(depeche_mode.reload.likes).to eq(14)
138
134
  end
139
135
 
140
- if mongodb_version > "2.5"
141
- it "does not error on non initialized fields" do
142
- expect(smiths.reload.likes).to eq(10)
143
- end
136
+ it "does not error on non initialized fields" do
137
+ expect(smiths.reload.likes).to eq(10)
144
138
  end
145
139
  end
146
- end
140
+ end if mongodb_version > "2.5"
147
141
 
148
142
  describe "#inc" do
149
143
 
@@ -926,8 +926,13 @@ describe Mongoid::Contextual::Memory do
926
926
  Address.new(street: "lenau", number: 5, name: "lenau")
927
927
  end
928
928
 
929
+ let(:kampuchea_krom) do
930
+ Address.new(street: "kampuchea krom", number: 5, name: "kampuchea krom")
931
+ end
932
+
929
933
  before do
930
934
  criteria.documents.unshift(lenau)
935
+ criteria.documents.unshift(kampuchea_krom)
931
936
  end
932
937
 
933
938
  context "when the sort is ascending" do
@@ -937,7 +942,7 @@ describe Mongoid::Contextual::Memory do
937
942
  end
938
943
 
939
944
  it "sorts the documents" do
940
- expect(context.entries).to eq([ friedel, lenau, pfluger, hobrecht ])
945
+ expect(context.entries).to eq([ friedel, kampuchea_krom, lenau, pfluger, hobrecht ])
941
946
  end
942
947
 
943
948
  it "returns the context" do
@@ -952,7 +957,7 @@ describe Mongoid::Contextual::Memory do
952
957
  end
953
958
 
954
959
  it "sorts the documents" do
955
- expect(context.entries).to eq([ hobrecht, pfluger, lenau, friedel ])
960
+ expect(context.entries).to eq([ hobrecht, pfluger, lenau, kampuchea_krom, friedel ])
956
961
  end
957
962
 
958
963
  it "returns the context" do
@@ -934,12 +934,9 @@ describe Mongoid::Criteria::Modifiable do
934
934
 
935
935
  context "when the relation is a references many" do
936
936
 
937
- let!(:post_one) do
938
- person.posts.create(title: "First")
939
- end
940
-
941
- let!(:post_two) do
942
- person.posts.create(title: "Second")
937
+ before do
938
+ person.posts.create!(title: "First")
939
+ person.posts.create!(title: "Second")
943
940
  end
944
941
 
945
942
  context "when updating the relation directly" do
@@ -948,12 +945,12 @@ describe Mongoid::Criteria::Modifiable do
948
945
  person.posts.update(title: "London")
949
946
  end
950
947
 
951
- let!(:from_db) do
948
+ let(:from_db) do
952
949
  Person.first
953
950
  end
954
951
 
955
952
  it "updates the first document" do
956
- expect(from_db.posts.first.title).to eq("London")
953
+ expect(from_db.posts.map(&:title)).to eq(["London", "Second"])
957
954
  end
958
955
 
959
956
  it "does not update the last document" do
@@ -1371,6 +1371,77 @@ describe Mongoid::Criteria do
1371
1371
 
1372
1372
  context "when including a belongs to relation" do
1373
1373
 
1374
+ context "when the criteria is from the root" do
1375
+
1376
+ let!(:person_two) do
1377
+ Person.create(age: 2)
1378
+ end
1379
+
1380
+ let!(:post_one) do
1381
+ person.posts.create(title: "one")
1382
+ end
1383
+
1384
+ let!(:post_two) do
1385
+ person_two.posts.create(title: "two")
1386
+ end
1387
+
1388
+ context "when calling first" do
1389
+
1390
+ let(:criteria) do
1391
+ Post.includes(:person)
1392
+ end
1393
+
1394
+ let!(:document) do
1395
+ criteria.first
1396
+ end
1397
+
1398
+ it "eager loads the first document" do
1399
+ expect_query(0) do
1400
+ expect(document.person).to eq(person)
1401
+ end
1402
+ end
1403
+
1404
+ it "does not eager load the last document" do
1405
+ doc = criteria.last
1406
+ expect_query(1) do
1407
+ expect(doc.person).to eq(person_two)
1408
+ end
1409
+ end
1410
+
1411
+ it "returns the first document" do
1412
+ expect(document).to eq(post_one)
1413
+ end
1414
+ end
1415
+
1416
+ context "when calling last" do
1417
+
1418
+ let!(:criteria) do
1419
+ Post.includes(:person)
1420
+ end
1421
+
1422
+ let!(:document) do
1423
+ criteria.last
1424
+ end
1425
+
1426
+ it "eager loads the last document" do
1427
+ expect_query(0) do
1428
+ expect(document.person).to eq(person_two)
1429
+ end
1430
+ end
1431
+
1432
+ it "does not eager load the first document" do
1433
+ doc = criteria.first
1434
+ expect_query(1) do
1435
+ expect(doc.person).to eq(person)
1436
+ end
1437
+ end
1438
+
1439
+ it "returns the last document" do
1440
+ expect(document).to eq(post_two)
1441
+ end
1442
+ end
1443
+ end
1444
+
1374
1445
  context "when the criteria is from an embedded relation" do
1375
1446
 
1376
1447
  let(:peep) do
@@ -1495,77 +1566,6 @@ describe Mongoid::Criteria do
1495
1566
  end
1496
1567
  end
1497
1568
  end
1498
-
1499
- context "when the criteria is from the root" do
1500
-
1501
- let!(:person_two) do
1502
- Person.create(age: 2)
1503
- end
1504
-
1505
- let!(:post_one) do
1506
- person.posts.create(title: "one")
1507
- end
1508
-
1509
- let!(:post_two) do
1510
- person_two.posts.create(title: "two")
1511
- end
1512
-
1513
- context "when calling first" do
1514
-
1515
- let(:criteria) do
1516
- Post.includes(:person)
1517
- end
1518
-
1519
- let!(:document) do
1520
- criteria.first
1521
- end
1522
-
1523
- it "eager loads the first document" do
1524
- expect_query(0) do
1525
- expect(document.person).to eq(person)
1526
- end
1527
- end
1528
-
1529
- it "does not eager load the last document" do
1530
- doc = criteria.last
1531
- expect_query(1) do
1532
- expect(doc.person).to eq(person_two)
1533
- end
1534
- end
1535
-
1536
- it "returns the first document" do
1537
- expect(document).to eq(post_one)
1538
- end
1539
- end
1540
-
1541
- context "when calling last" do
1542
-
1543
- let!(:criteria) do
1544
- Post.includes(:person)
1545
- end
1546
-
1547
- let!(:document) do
1548
- criteria.last
1549
- end
1550
-
1551
- it "eager loads the last document" do
1552
- expect_query(0) do
1553
- expect(document.person).to eq(person_two)
1554
- end
1555
- end
1556
-
1557
- it "does not eager load the first document" do
1558
- doc = criteria.first
1559
- expect_query(1) do
1560
- expect(doc.person).to eq(person)
1561
- end
1562
- end
1563
-
1564
- it "returns the last document" do
1565
- expect(document).to eq(post_two)
1566
- end
1567
- end
1568
- end
1569
1569
  end
1570
1570
 
1571
1571
  context "when providing inclusions to the default scope" do
@@ -2851,6 +2851,17 @@ describe Mongoid::Criteria do
2851
2851
  end
2852
2852
  end
2853
2853
 
2854
+ context "when plucking existent and non-existent fields" do
2855
+
2856
+ let(:plucked) do
2857
+ Band.all.pluck(:id, :fooz)
2858
+ end
2859
+
2860
+ it "returns nil for the field that doesnt exist" do
2861
+ expect(plucked).to eq([[depeche.id, nil], [tool.id, nil], [photek.id, nil] ])
2862
+ end
2863
+ end
2864
+
2854
2865
  context "when plucking a field that doesnt exist" do
2855
2866
 
2856
2867
  context "when pluck one field" do
@@ -2859,8 +2870,8 @@ describe Mongoid::Criteria do
2859
2870
  Band.all.pluck(:foo)
2860
2871
  end
2861
2872
 
2862
- it "returns a empty array" do
2863
- expect(plucked).to eq([])
2873
+ it "returns a array with nil values" do
2874
+ expect(plucked).to eq([nil, nil, nil])
2864
2875
  end
2865
2876
  end
2866
2877
 
@@ -2870,8 +2881,8 @@ describe Mongoid::Criteria do
2870
2881
  Band.all.pluck(:foo, :bar)
2871
2882
  end
2872
2883
 
2873
- it "returns a empty array" do
2874
- expect(plucked).to eq([[], [], []])
2884
+ it "returns a nil arrays" do
2885
+ expect(plucked).to eq([[nil, nil], [nil, nil], [nil, nil]])
2875
2886
  end
2876
2887
  end
2877
2888
  end
@@ -144,7 +144,7 @@ describe Mongoid::Document do
144
144
  context "with updated_at" do
145
145
 
146
146
  let!(:updated_at) do
147
- document.updated_at.utc.to_s(:number)
147
+ document.updated_at.utc.to_s(:nsec)
148
148
  end
149
149
 
150
150
  it "has the id and updated_at key name" do
@@ -182,7 +182,7 @@ describe Mongoid::Document do
182
182
  end
183
183
 
184
184
  let!(:updated_at) do
185
- agent.updated_at.utc.to_s(:number)
185
+ agent.updated_at.utc.to_s(:nsec)
186
186
  end
187
187
 
188
188
  it "has the id and updated_at key name" do
@@ -529,11 +529,15 @@ describe Mongoid::Document do
529
529
  context "when the document is not new" do
530
530
 
531
531
  let(:person) do
532
- Person.instantiate("_id" => BSON::ObjectId.new)
532
+ Person.create!
533
533
  end
534
534
 
535
535
  it "returns the id in an array" do
536
- expect(person.to_key).to eq([ person.id ])
536
+ expect(person.to_key).to eq([ person.id.to_s ])
537
+ end
538
+
539
+ it "can query using the key" do
540
+ expect(person.id).to eq Person.find(person.to_key).first.id
537
541
  end
538
542
  end
539
543
 
@@ -546,7 +550,7 @@ describe Mongoid::Document do
546
550
  end
547
551
 
548
552
  it "returns the id in an array" do
549
- expect(person.to_key).to eq([ person.id ])
553
+ expect(person.to_key).to eq([ person.id.to_s ])
550
554
  end
551
555
  end
552
556
  end
@@ -13,3 +13,17 @@ describe BSON::ObjectId do
13
13
  end
14
14
  end
15
15
  end
16
+
17
+ describe BSON::Document do
18
+
19
+ describe "#symbolize_keys" do
20
+
21
+ let(:doc) do
22
+ described_class.new("foo" => "bar")
23
+ end
24
+
25
+ it "returns key as symbol" do
26
+ expect(doc.symbolize_keys.keys).to eq [:foo]
27
+ end
28
+ end
29
+ end
@@ -53,6 +53,55 @@ describe Mongoid::Findable do
53
53
 
54
54
  describe ".find_by" do
55
55
 
56
+ context "when collection is a embeds_many" do
57
+
58
+ let(:person) do
59
+ Person.create(title: "sir")
60
+ end
61
+
62
+ let!(:message) do
63
+ person.messages.create!(body: 'foo')
64
+ end
65
+
66
+ context "when the document is found" do
67
+
68
+ it "returns the document" do
69
+ expect(person.messages.find_by(body: 'foo')).to eq(message)
70
+ end
71
+ end
72
+
73
+ context "when the document is not found" do
74
+
75
+ context "when raising a not found error" do
76
+
77
+ let!(:raise_option) { Mongoid.raise_not_found_error }
78
+
79
+ before { Mongoid.raise_not_found_error = true }
80
+
81
+ after { Mongoid.raise_not_found_error = raise_option }
82
+
83
+ it "raises an error" do
84
+ expect {
85
+ person.messages.find_by(body: 'bar')
86
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
87
+ end
88
+ end
89
+
90
+ context "when raising no error" do
91
+
92
+ let!(:raise_option) { Mongoid.raise_not_found_error }
93
+
94
+ before { Mongoid.raise_not_found_error = false }
95
+
96
+ after { Mongoid.raise_not_found_error = raise_option }
97
+
98
+ it "returns nil" do
99
+ expect(person.messages.find_by(body: 'bar')).to be_nil
100
+ end
101
+ end
102
+ end
103
+ end
104
+
56
105
  context "when the document is found" do
57
106
 
58
107
  let!(:person) do
@@ -84,9 +133,11 @@ describe Mongoid::Findable do
84
133
 
85
134
  context "when raising a not found error" do
86
135
 
87
- before do
88
- Mongoid.raise_not_found_error = true
89
- end
136
+ let!(:raise_option) { Mongoid.raise_not_found_error }
137
+
138
+ before { Mongoid.raise_not_found_error = true }
139
+
140
+ after { Mongoid.raise_not_found_error = raise_option }
90
141
 
91
142
  it "raises an error" do
92
143
  expect {
@@ -97,13 +148,11 @@ describe Mongoid::Findable do
97
148
 
98
149
  context "when raising no error" do
99
150
 
100
- before do
101
- Mongoid.raise_not_found_error = false
102
- end
151
+ let!(:raise_option) { Mongoid.raise_not_found_error }
103
152
 
104
- after do
105
- Mongoid.raise_not_found_error = true
106
- end
153
+ before { Mongoid.raise_not_found_error = false }
154
+
155
+ after { Mongoid.raise_not_found_error = raise_option }
107
156
 
108
157
  context "when no block is provided" do
109
158
 
@@ -128,6 +177,45 @@ describe Mongoid::Findable do
128
177
  end
129
178
  end
130
179
 
180
+ describe "find_by!" do
181
+
182
+ context "when the document is found" do
183
+
184
+ let!(:person) do
185
+ Person.create(title: "sir")
186
+ end
187
+
188
+ context "when no block is provided" do
189
+
190
+ it "returns the document" do
191
+ expect(Person.find_by!(title: "sir")).to eq(person)
192
+ end
193
+ end
194
+
195
+ context "when a block is provided" do
196
+
197
+ let(:result) do
198
+ Person.find_by!(title: "sir") do |peep|
199
+ peep.age = 50
200
+ end
201
+ end
202
+
203
+ it "yields the returned document" do
204
+ expect(result.age).to eq(50)
205
+ end
206
+ end
207
+ end
208
+
209
+ context "when the document is not found" do
210
+
211
+ it "raises an error" do
212
+ expect {
213
+ Person.find_by!(ssn: "333-22-1111")
214
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
215
+ end
216
+ end
217
+ end
218
+
131
219
  [ :first, :one ].each do |method|
132
220
 
133
221
  describe "##{method}" do
@@ -382,8 +470,8 @@ describe Mongoid::Findable do
382
470
  Band.pluck(:follows)
383
471
  end
384
472
 
385
- it "returns an empty array" do
386
- expect(plucked).to be_empty
473
+ it "returns a array with nil values" do
474
+ expect(plucked).to eq([nil, nil, nil])
387
475
  end
388
476
  end
389
477
  end
@@ -394,6 +394,47 @@ describe Mongoid::Persistable::Creatable do
394
394
  end
395
395
  end
396
396
  end
397
+
398
+ context "#find_or_create_by!" do
399
+
400
+ before do
401
+ container.vehicles.find_or_create_by!({ driver_id: driver.id }, Car)
402
+ end
403
+
404
+ it "creates the given type document" do
405
+ expect(container.vehicles.map(&:class)).to eq([ Car ])
406
+ end
407
+
408
+ it "creates with the given attributes" do
409
+ expect(container.vehicles.map(&:driver)).to eq([ driver ])
410
+ end
411
+
412
+ it "creates the correct number of documents" do
413
+ expect(container.vehicles.size).to eq(1)
414
+ end
415
+
416
+ context "when executing with a found document" do
417
+
418
+ before do
419
+ container.vehicles.find_or_create_by!({ driver_id: driver.id }, Car)
420
+ end
421
+
422
+ it "does not create an additional document" do
423
+ expect(container.vehicles.size).to eq(1)
424
+ end
425
+ end
426
+
427
+ context "when executing with an additional new document" do
428
+
429
+ before do
430
+ container.vehicles.find_or_create_by!({ driver_id: driver.id }, Truck)
431
+ end
432
+
433
+ it "creates the new additional document" do
434
+ expect(container.vehicles.size).to eq(2)
435
+ end
436
+ end
437
+ end
397
438
  end
398
439
  end
399
440
 
@@ -8,6 +8,14 @@ describe Mongoid::Persistable::Savable do
8
8
  Person.create
9
9
  end
10
10
 
11
+ let(:contextable_item) do
12
+ ContextableItem.new
13
+ end
14
+
15
+ let(:persisted_contextable_item) do
16
+ ContextableItem.create(title: 'sir')
17
+ end
18
+
11
19
  context "when skipping validation" do
12
20
 
13
21
  context "when no relations are involved" do
@@ -285,6 +293,35 @@ describe Mongoid::Persistable::Savable do
285
293
  }.to raise_error(Mongoid::Errors::ReadonlyDocument)
286
294
  end
287
295
  end
296
+
297
+ context "when validation context isn't assigned" do
298
+ it "returns true" do
299
+ expect(contextable_item.save).to be true
300
+ end
301
+ end
302
+
303
+ context "when validation context exists" do
304
+ context "on new document" do
305
+ it "returns true" do
306
+ contextable_item.title = "sir"
307
+ expect(contextable_item.save(context: :in_context)).to be true
308
+ end
309
+ it "returns false" do
310
+ expect(contextable_item.save(context: :in_context)).to be false
311
+ end
312
+ end
313
+
314
+ context "on persisted document" do
315
+ it "returns true" do
316
+ persisted_contextable_item.title = "lady"
317
+ expect(persisted_contextable_item.save(context: :in_context)).to be true
318
+ end
319
+ it "returns false" do
320
+ persisted_contextable_item.title = nil
321
+ expect(persisted_contextable_item.save(context: :in_context)).to be false
322
+ end
323
+ end
324
+ end
288
325
  end
289
326
 
290
327
  describe "save!" do
@@ -136,4 +136,27 @@ describe Mongoid::Persistable::Settable do
136
136
  end
137
137
  end
138
138
  end
139
+
140
+ context "when dynamic attributes are not enabled" do
141
+ let(:account) do
142
+ Account.create
143
+ end
144
+
145
+ it "raises exception for an unknown attribute " do
146
+ expect {
147
+ account.set(somethingnew: "somethingnew")
148
+ }.to raise_error(Mongoid::Errors::UnknownAttribute)
149
+ end
150
+ end
151
+
152
+ context "when dynamic attributes enabled" do
153
+ let(:person) do
154
+ Person.create
155
+ end
156
+
157
+ it "updates non existing attribute" do
158
+ person.set(somethingnew: "somethingnew")
159
+ expect(person.reload.somethingnew).to eq "somethingnew"
160
+ end
161
+ end
139
162
  end