mongoid 8.0.7 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +0 -25
  6. data/lib/config/locales/en.yml +46 -14
  7. data/lib/mongoid/association/accessors.rb +2 -2
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  10. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  11. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  13. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  14. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  15. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  16. data/lib/mongoid/association/macros.rb +0 -6
  17. data/lib/mongoid/association/nested/one.rb +40 -2
  18. data/lib/mongoid/association/proxy.rb +1 -1
  19. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  21. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  22. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  23. data/lib/mongoid/association/reflections.rb +2 -2
  24. data/lib/mongoid/atomic.rb +0 -7
  25. data/lib/mongoid/attributes/dynamic.rb +1 -1
  26. data/lib/mongoid/attributes/nested.rb +2 -2
  27. data/lib/mongoid/attributes/processing.rb +5 -29
  28. data/lib/mongoid/attributes/projector.rb +1 -1
  29. data/lib/mongoid/attributes/readonly.rb +1 -1
  30. data/lib/mongoid/attributes.rb +8 -2
  31. data/lib/mongoid/changeable.rb +107 -5
  32. data/lib/mongoid/clients/storage_options.rb +2 -5
  33. data/lib/mongoid/clients/validators/storage.rb +1 -13
  34. data/lib/mongoid/collection_configurable.rb +58 -0
  35. data/lib/mongoid/composable.rb +2 -0
  36. data/lib/mongoid/config/defaults.rb +60 -0
  37. data/lib/mongoid/config/options.rb +0 -3
  38. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  39. data/lib/mongoid/config/validators.rb +1 -0
  40. data/lib/mongoid/config.rb +99 -28
  41. data/lib/mongoid/contextual/atomic.rb +1 -1
  42. data/lib/mongoid/contextual/memory.rb +233 -33
  43. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  44. data/lib/mongoid/contextual/mongo.rb +370 -133
  45. data/lib/mongoid/contextual/none.rb +162 -7
  46. data/lib/mongoid/contextual.rb +12 -0
  47. data/lib/mongoid/criteria/findable.rb +2 -2
  48. data/lib/mongoid/criteria/includable.rb +4 -3
  49. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  50. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  51. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  52. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  53. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  54. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  55. data/lib/mongoid/criteria.rb +6 -5
  56. data/lib/mongoid/deprecable.rb +1 -2
  57. data/lib/mongoid/deprecation.rb +3 -3
  58. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  59. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  60. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  61. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  62. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  63. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  64. data/lib/mongoid/errors.rb +4 -1
  65. data/lib/mongoid/extensions/hash.rb +2 -24
  66. data/lib/mongoid/extensions/object.rb +2 -2
  67. data/lib/mongoid/extensions/time.rb +2 -0
  68. data/lib/mongoid/fields/localized.rb +10 -0
  69. data/lib/mongoid/fields/standard.rb +10 -0
  70. data/lib/mongoid/fields.rb +53 -24
  71. data/lib/mongoid/findable.rb +27 -3
  72. data/lib/mongoid/interceptable.rb +10 -118
  73. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  74. data/lib/mongoid/matcher/type.rb +1 -1
  75. data/lib/mongoid/persistable/creatable.rb +1 -0
  76. data/lib/mongoid/persistable/deletable.rb +1 -1
  77. data/lib/mongoid/persistable/savable.rb +13 -1
  78. data/lib/mongoid/persistable/unsettable.rb +2 -2
  79. data/lib/mongoid/persistable/updatable.rb +51 -1
  80. data/lib/mongoid/persistable/upsertable.rb +20 -1
  81. data/lib/mongoid/persistable.rb +3 -0
  82. data/lib/mongoid/query_cache.rb +5 -1
  83. data/lib/mongoid/railties/database.rake +7 -2
  84. data/lib/mongoid/reloadable.rb +5 -3
  85. data/lib/mongoid/stateful.rb +22 -1
  86. data/lib/mongoid/tasks/database.rake +12 -0
  87. data/lib/mongoid/tasks/database.rb +20 -0
  88. data/lib/mongoid/utils.rb +22 -0
  89. data/lib/mongoid/validatable/macros.rb +5 -5
  90. data/lib/mongoid/validatable.rb +4 -1
  91. data/lib/mongoid/version.rb +1 -1
  92. data/lib/mongoid/warnings.rb +17 -1
  93. data/lib/mongoid.rb +16 -3
  94. data/spec/integration/app_spec.rb +2 -2
  95. data/spec/integration/callbacks_models.rb +37 -0
  96. data/spec/integration/callbacks_spec.rb +126 -12
  97. data/spec/integration/discriminator_key_spec.rb +4 -5
  98. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  99. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  100. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  101. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  102. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  103. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  104. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  105. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  106. data/spec/mongoid/association/syncable_spec.rb +1 -1
  107. data/spec/mongoid/attributes_spec.rb +3 -33
  108. data/spec/mongoid/changeable_spec.rb +299 -24
  109. data/spec/mongoid/clients_spec.rb +122 -13
  110. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  111. data/spec/mongoid/config/defaults_spec.rb +160 -0
  112. data/spec/mongoid/config_spec.rb +154 -27
  113. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  114. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  115. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  116. data/spec/mongoid/contextual/none_spec.rb +49 -2
  117. data/spec/mongoid/copyable_spec.rb +2 -10
  118. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  119. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  120. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  121. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  122. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  123. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  124. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  125. data/spec/mongoid/criteria_spec.rb +5 -9
  126. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  127. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  128. data/spec/mongoid/extensions/time_spec.rb +8 -43
  129. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  130. data/spec/mongoid/fields/localized_spec.rb +46 -28
  131. data/spec/mongoid/fields_spec.rb +136 -77
  132. data/spec/mongoid/findable_spec.rb +391 -34
  133. data/spec/mongoid/indexable_spec.rb +16 -10
  134. data/spec/mongoid/interceptable_spec.rb +173 -362
  135. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  136. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  137. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  138. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  139. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  140. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  141. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  142. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  143. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  144. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  145. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  146. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  147. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  148. data/spec/mongoid/persistence_context_spec.rb +7 -57
  149. data/spec/mongoid/query_cache_spec.rb +56 -61
  150. data/spec/mongoid/reloadable_spec.rb +24 -28
  151. data/spec/mongoid/scopable_spec.rb +70 -0
  152. data/spec/mongoid/serializable_spec.rb +9 -30
  153. data/spec/mongoid/stateful_spec.rb +122 -8
  154. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  155. data/spec/mongoid/tasks/database_spec.rb +127 -0
  156. data/spec/mongoid/timestamps_spec.rb +9 -11
  157. data/spec/mongoid/touchable_spec.rb +277 -5
  158. data/spec/mongoid/touchable_spec_models.rb +3 -1
  159. data/spec/mongoid/traversable_spec.rb +9 -24
  160. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  161. data/spec/mongoid_spec.rb +36 -10
  162. data/spec/spec_helper.rb +5 -0
  163. data/spec/support/immutable_ids.rb +118 -0
  164. data/spec/support/macros.rb +47 -15
  165. data/spec/support/models/artist.rb +0 -1
  166. data/spec/support/models/band.rb +1 -0
  167. data/spec/support/models/book.rb +1 -0
  168. data/spec/support/models/building.rb +2 -0
  169. data/spec/support/models/cover.rb +10 -0
  170. data/spec/support/models/person.rb +0 -1
  171. data/spec/support/models/product.rb +1 -0
  172. data.tar.gz.sig +0 -0
  173. metadata +685 -651
  174. metadata.gz.sig +0 -0
  175. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  176. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  177. data/spec/support/models/purse.rb +0 -9
@@ -393,11 +393,10 @@ describe Mongoid::Attributes do
393
393
  end
394
394
 
395
395
  context "when reloaded" do
396
+ config_override :raise_not_found_error, false
396
397
 
397
398
  before do
398
- Mongoid.raise_not_found_error = false
399
399
  person.reload
400
- Mongoid.raise_not_found_error = true
401
400
  end
402
401
 
403
402
  it "returns the default value" do
@@ -1030,14 +1029,13 @@ describe Mongoid::Attributes do
1030
1029
  end
1031
1030
 
1032
1031
  context "when the attribute does not exist" do
1032
+ config_override :raise_not_found_error, false
1033
1033
 
1034
1034
  before do
1035
1035
  person.collection
1036
1036
  .find({ _id: person.id })
1037
1037
  .update_one({ "$unset" => { age: 1 }})
1038
- Mongoid.raise_not_found_error = false
1039
1038
  person.reload
1040
- Mongoid.raise_not_found_error = true
1041
1039
  end
1042
1040
 
1043
1041
  it "returns the default value" do
@@ -1161,14 +1159,13 @@ describe Mongoid::Attributes do
1161
1159
  end
1162
1160
 
1163
1161
  context "when the attribute does not exist" do
1162
+ config_override :raise_not_found_error, false
1164
1163
 
1165
1164
  before do
1166
1165
  person.collection
1167
1166
  .find({ _id: person.id })
1168
1167
  .update_one({ "$unset" => { age: 1 }})
1169
- Mongoid.raise_not_found_error = false
1170
1168
  person.reload
1171
- Mongoid.raise_not_found_error = true
1172
1169
  end
1173
1170
 
1174
1171
  it "returns true" do
@@ -2711,31 +2708,4 @@ describe Mongoid::Attributes do
2711
2708
  catalog.set_field.should == Set.new([ 1, 2 ])
2712
2709
  end
2713
2710
  end
2714
-
2715
- context 'when an embedded field has a capitalized store_as name' do
2716
- let(:person) { Person.new(Purse: { brand: 'Gucci' }) }
2717
-
2718
- it 'sets the value' do
2719
- expect(person.purse.brand).to eq('Gucci')
2720
- end
2721
-
2722
- it 'saves successfully' do
2723
- expect(person.save!).to eq(true)
2724
- end
2725
-
2726
- context 'when persisted' do
2727
- before do
2728
- person.save!
2729
- person.reload
2730
- end
2731
-
2732
- it 'persists the value' do
2733
- expect(person.reload.purse.brand).to eq('Gucci')
2734
- end
2735
-
2736
- it 'uses the correct key in the database' do
2737
- expect(person.collection.find(_id: person.id).first['Purse']['_id']).to eq(person.purse.id)
2738
- end
2739
- end
2740
- end
2741
2711
  end
@@ -476,6 +476,188 @@ describe Mongoid::Changeable do
476
476
  end
477
477
  end
478
478
  end
479
+
480
+ context "when including key word args" do
481
+
482
+ let(:person) { Person.new }
483
+
484
+ context "when only including from" do
485
+
486
+ context "when the object has not changed" do
487
+
488
+ it "returns false" do
489
+ expect(person.send(:attribute_changed?, :score, from: nil)).to be false
490
+ end
491
+
492
+ it "returns false using (attribute)_changed?" do
493
+ expect(person.score_changed?(from: nil)).to be false
494
+ end
495
+ end
496
+
497
+ context "when the object has changed from the wrong item" do
498
+
499
+ before do
500
+ person.score = 2
501
+ end
502
+
503
+ it "returns false" do
504
+ expect(person.send(:attribute_changed?, :score, from: 1)).to be false
505
+ end
506
+
507
+ it "returns false using (attribute)_changed?" do
508
+ expect(person.score_changed?(from: 1)).to be false
509
+ end
510
+ end
511
+
512
+ context "when the object has changed from the correct item" do
513
+
514
+ before do
515
+ person.score = 2
516
+ end
517
+
518
+ it "returns true" do
519
+ expect(person.send(:attribute_changed?, :score, from: nil)).to be true
520
+ end
521
+
522
+ it "returns true using (attribute)_changed?" do
523
+ expect(person.score_changed?(from: nil)).to be true
524
+ end
525
+ end
526
+ end
527
+
528
+ context "when only including to" do
529
+
530
+ context "when the object has not changed" do
531
+
532
+ it "returns false" do
533
+ expect(person.send(:attribute_changed?, :score, to: nil)).to be false
534
+ end
535
+
536
+ it "returns false using (attribute)_changed?" do
537
+ expect(person.score_changed?(to: nil)).to be false
538
+ end
539
+ end
540
+
541
+ context "when the object has changed to the wrong item" do
542
+
543
+ before do
544
+ person.score = 2
545
+ end
546
+
547
+ it "returns false" do
548
+ expect(person.send(:attribute_changed?, :score, to: 1)).to be false
549
+ end
550
+
551
+ it "returns false using (attribute)_changed?" do
552
+ expect(person.score_changed?(to: 1)).to be false
553
+ end
554
+ end
555
+
556
+ context "when the object has changed to the correct item" do
557
+
558
+ before do
559
+ person.score = 2
560
+ end
561
+
562
+ it "returns true" do
563
+ expect(person.send(:attribute_changed?, :score, to: 2)).to be true
564
+ end
565
+
566
+ it "returns true using (attribute)_changed?" do
567
+ expect(person.score_changed?(to: 2)).to be true
568
+ end
569
+ end
570
+ end
571
+
572
+ context "when including from and to" do
573
+
574
+ context "when the object has not changed" do
575
+
576
+ it "returns false" do
577
+ expect(person.send(:attribute_changed?, :score, from: nil, to: nil)).to be false
578
+ end
579
+
580
+ it "returns false using (attribute)_changed?" do
581
+ expect(person.score_changed?(from: nil, to: nil)).to be false
582
+ end
583
+ end
584
+
585
+ context "when only the from is correct" do
586
+
587
+ before do
588
+ person.score = 2
589
+ end
590
+
591
+ it "returns false" do
592
+ expect(person.send(:attribute_changed?, :score, from: nil, to: 3)).to be false
593
+ end
594
+
595
+ it "returns false using (attribute)_changed?" do
596
+ expect(person.score_changed?(from: nil, to: 3)).to be false
597
+ end
598
+ end
599
+
600
+ context "when only the to is correct" do
601
+
602
+ before do
603
+ person.score = 2
604
+ end
605
+
606
+ it "returns false" do
607
+ expect(person.send(:attribute_changed?, :score, from: 1, to: 2)).to be false
608
+ end
609
+
610
+ it "returns false using (attribute)_changed?" do
611
+ expect(person.score_changed?(from: 1, to: 2)).to be false
612
+ end
613
+ end
614
+
615
+ context "when the from and to are correct" do
616
+
617
+ before do
618
+ person.score = 2
619
+ end
620
+
621
+ it "returns true" do
622
+ expect(person.send(:attribute_changed?, :score, from: nil, to: 2)).to be true
623
+ end
624
+
625
+ it "returns true using (attribute)_changed?" do
626
+ expect(person.score_changed?(from: nil, to: 2)).to be true
627
+ end
628
+ end
629
+
630
+ context "when value is mongoized" do
631
+
632
+ before do
633
+ person.score = "2"
634
+ end
635
+
636
+ it "returns true with mongoized value" do
637
+ expect(person.send(:attribute_changed?, :score, from: nil, to: 2)).to be true
638
+ end
639
+
640
+ it "returns true with mongoized value using (attribute)_changed?" do
641
+ expect(person.score_changed?(from: nil, to: 2)).to be true
642
+ end
643
+ end
644
+
645
+ context "when value is mongoized" do
646
+
647
+ before do
648
+ person.score = "2"
649
+ end
650
+
651
+ it "returns false with unmongoized value" do
652
+ expect(person.send(:attribute_changed?, :score, from: nil, to: "2")).to be false
653
+ end
654
+
655
+ it "returns false with unmongoized value using (attribute)_changed?" do
656
+ expect(person.score_changed?(from: nil, to: "2")).to be false
657
+ end
658
+ end
659
+ end
660
+ end
479
661
  end
480
662
 
481
663
  describe "#attribute_changed_from_default?" do
@@ -1167,20 +1349,10 @@ describe Mongoid::Changeable do
1167
1349
 
1168
1350
  context "when the document is embedded" do
1169
1351
 
1170
- let(:person) do
1171
- Person.instantiate(title: "Grand Poobah")
1172
- end
1352
+ let(:person) { Person.create(title: "Grand Poobah") }
1353
+ let(:address) { person.addresses.create(street: "Oxford St") }
1173
1354
 
1174
- let(:address) do
1175
- Address.instantiate(street: "Oxford St")
1176
- end
1177
-
1178
- before do
1179
- person.addresses << address
1180
- person.instance_variable_set(:@new_record, false)
1181
- address.instance_variable_set(:@new_record, false)
1182
- address.street = "Bond St"
1183
- end
1355
+ before { address.street = "Bond St" }
1184
1356
 
1185
1357
  it "returns a hash of field names and new values" do
1186
1358
  expect(address.setters).to eq(
@@ -1189,17 +1361,9 @@ describe Mongoid::Changeable do
1189
1361
  end
1190
1362
 
1191
1363
  context "when the document is embedded multiple levels" do
1192
-
1193
- let(:location) do
1194
- Location.new(name: "Home")
1195
- end
1196
-
1197
- before do
1198
- location.instance_variable_set(:@new_record, false)
1199
- address.locations << location
1200
- location.name = "Work"
1201
- end
1202
-
1364
+ let(:location) { address.locations.create(name: "Home") }
1365
+ before { location.name = "Work" }
1366
+
1203
1367
  it "returns the proper hash with locations" do
1204
1368
  expect(location.setters).to eq(
1205
1369
  { "addresses.0.locations.0.name" => "Work" }
@@ -1479,6 +1643,117 @@ describe Mongoid::Changeable do
1479
1643
  end
1480
1644
  end
1481
1645
 
1646
+ describe '#attribute_before_last_save' do
1647
+ let(:person) do
1648
+ Person.create!(title: "Grand Poobah")
1649
+ end
1650
+
1651
+ before do
1652
+ person.title = "Captain Obvious"
1653
+ end
1654
+
1655
+ context "when the document has been saved" do
1656
+ before do
1657
+ person.save!
1658
+ end
1659
+
1660
+ it "returns the changes" do
1661
+ expect(person.attribute_before_last_save(:title)).to eq("Grand Poobah")
1662
+ expect(person.title_before_last_save).to eq("Grand Poobah")
1663
+ end
1664
+ end
1665
+
1666
+ context "when the document has not been saved" do
1667
+ it "returns no changes" do
1668
+ expect(person.attribute_before_last_save(:title)).to be_nil
1669
+ expect(person.title_before_last_save).to be_nil
1670
+ end
1671
+ end
1672
+ end
1673
+
1674
+ describe '#saved_change_to_attribute' do
1675
+ let(:person) do
1676
+ Person.create!(title: "Grand Poobah")
1677
+ end
1678
+
1679
+ before do
1680
+ person.title = "Captain Obvious"
1681
+ end
1682
+
1683
+ context "when the document has been saved" do
1684
+ before do
1685
+ person.save!
1686
+ end
1687
+
1688
+ it "returns the changes" do
1689
+ expect(person.saved_change_to_attribute(:title)).to eq(["Grand Poobah", "Captain Obvious"])
1690
+ expect(person.saved_change_to_title).to eq(["Grand Poobah", "Captain Obvious"])
1691
+ end
1692
+ end
1693
+
1694
+ context "when the document has not been saved" do
1695
+ it "returns changes for the previous save" do
1696
+ expect(person.saved_change_to_attribute(:title)).to eq([nil, "Grand Poobah"])
1697
+ expect(person.saved_change_to_title).to eq([nil, "Grand Poobah"])
1698
+ end
1699
+ end
1700
+ end
1701
+
1702
+ describe '#saved_change_to_attribute?' do
1703
+ context "when the document has been saved" do
1704
+ let(:person) do
1705
+ Person.create!(title: "Grand Poobah")
1706
+ end
1707
+
1708
+ before do
1709
+ person.title = "Captain Obvious"
1710
+ end
1711
+
1712
+ before do
1713
+ person.save!
1714
+ end
1715
+
1716
+ it "detects the changes" do
1717
+ expect(person.saved_change_to_attribute?(:title)).to be_truthy
1718
+ expect(person.saved_change_to_attribute?(:title, from: "Grand Poobah")).to be_truthy
1719
+ expect(person.saved_change_to_attribute?(:title, to: "Captain Obvious")).to be_truthy
1720
+ expect(person.saved_change_to_attribute?(:title, from: "Grand Poobah", to: "Captain Obvious")).to be_truthy
1721
+ expect(person.saved_change_to_title?(from: "Grand Poobah", to: "Captain Obvious")).to be_truthy
1722
+ expect(person.saved_change_to_attribute?(:age)).to be_falsey
1723
+ expect(person.saved_change_to_age?).to be_falsey
1724
+ end
1725
+ end
1726
+
1727
+ context "when the document has not been saved" do
1728
+ let(:person) do
1729
+ Person.new(title: "Grand Poobah")
1730
+ end
1731
+
1732
+ it "returns changes for the previous save" do
1733
+ expect(person.saved_change_to_attribute?(:title)).to be_falsey
1734
+ expect(person.saved_change_to_title?).to be_falsey
1735
+ end
1736
+ end
1737
+ end
1738
+
1739
+ describe '#will_save_change_to_attribute?' do
1740
+ let(:person) do
1741
+ Person.create!(title: "Grand Poobah")
1742
+ end
1743
+
1744
+ before do
1745
+ person.title = "Captain Obvious"
1746
+ end
1747
+
1748
+ it 'correctly detects changes' do
1749
+ expect(person.will_save_change_to_attribute?(:title)).to eq(true)
1750
+ expect(person.will_save_change_to_title?).to eq(true)
1751
+ expect(person.will_save_change_to_attribute?(:score)).to eq(false)
1752
+ expect(person.will_save_change_to_score?).to eq(false)
1753
+ end
1754
+
1755
+ end
1756
+
1482
1757
  context "when fields have been defined pre-dirty inclusion" do
1483
1758
 
1484
1759
  let(:document) do
@@ -738,19 +738,6 @@ describe Mongoid::Clients do
738
738
  end
739
739
  end
740
740
 
741
- context "when provided a class that extends another document" do
742
-
743
- let(:klass) do
744
- Class.new(Band)
745
- end
746
-
747
- it "raises an error" do
748
- expect {
749
- klass.store_in(database: :artists)
750
- }.to raise_error(Mongoid::Errors::InvalidStorageParent)
751
- end
752
- end
753
-
754
741
  context "when provided a hash" do
755
742
 
756
743
  context "when the hash is not valid" do
@@ -762,6 +749,128 @@ describe Mongoid::Clients do
762
749
  end
763
750
  end
764
751
  end
752
+
753
+ context "when it is called on a subclass" do
754
+
755
+ let(:client) { StoreParent.collection.client }
756
+ let(:parent) { StoreParent.create! }
757
+ let(:child1) { StoreChild1.create! }
758
+ let(:child2) { StoreChild2.create! }
759
+
760
+ before do
761
+ class StoreParent
762
+ include Mongoid::Document
763
+ end
764
+
765
+ class StoreChild1 < StoreParent
766
+ end
767
+
768
+ class StoreChild2 < StoreParent
769
+ end
770
+ end
771
+
772
+ after do
773
+ Object.send(:remove_const, :StoreParent)
774
+ Object.send(:remove_const, :StoreChild1)
775
+ Object.send(:remove_const, :StoreChild2)
776
+ end
777
+
778
+ context "when it is not called on the parent" do
779
+
780
+ context "when it is called on all subclasses" do
781
+
782
+ before do
783
+ StoreChild1.store_in collection: :store_ones
784
+ StoreChild2.store_in collection: :store_twos
785
+ [ parent, child1, child2 ]
786
+ end
787
+
788
+ let(:db_parent) { client['store_parents'].find.first }
789
+ let(:db_child1) { client['store_ones'].find.first }
790
+ let(:db_child2) { client['store_twos'].find.first }
791
+
792
+ it "stores the documents in the correct collections" do
793
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
794
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
795
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
796
+ end
797
+
798
+ it "only queries from its own collections" do
799
+ expect(StoreParent.count).to eq(1)
800
+ expect(StoreChild1.count).to eq(1)
801
+ expect(StoreChild2.count).to eq(1)
802
+ end
803
+ end
804
+
805
+ context "when it is called on one of the subclasses" do
806
+
807
+ before do
808
+ StoreChild1.store_in collection: :store_ones
809
+ [ parent, child1, child2 ]
810
+ end
811
+
812
+ let(:db_parent) { client['store_parents'].find.first }
813
+ let(:db_child1) { client['store_ones'].find.first }
814
+ let(:db_child2) { client['store_parents'].find.to_a.last }
815
+
816
+ it "stores the documents in the correct collections" do
817
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
818
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
819
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
820
+ end
821
+
822
+ it "queries from its own collections" do
823
+ expect(StoreParent.count).to eq(2)
824
+ expect(StoreChild1.count).to eq(1)
825
+ expect(StoreChild2.count).to eq(1)
826
+ end
827
+ end
828
+ end
829
+
830
+ context "when it is called on the parent" do
831
+
832
+ before do
833
+ StoreParent.store_in collection: :st_parents
834
+ end
835
+
836
+ context "when it is called on all subclasses" do
837
+
838
+ before do
839
+ StoreChild1.store_in collection: :store_ones
840
+ StoreChild2.store_in collection: :store_twos
841
+ [ parent, child1, child2 ]
842
+ end
843
+
844
+ let(:db_parent) { client['st_parents'].find.first }
845
+ let(:db_child1) { client['store_ones'].find.first }
846
+ let(:db_child2) { client['store_twos'].find.first }
847
+
848
+ it "stores the documents in the correct collections" do
849
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
850
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
851
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
852
+ end
853
+ end
854
+
855
+ context "when it is called on one of the subclasses" do
856
+
857
+ before do
858
+ StoreChild1.store_in collection: :store_ones
859
+ [ parent, child1, child2 ]
860
+ end
861
+
862
+ let(:db_parent) { client['st_parents'].find.first }
863
+ let(:db_child1) { client['store_ones'].find.first }
864
+ let(:db_child2) { client['st_parents'].find.to_a.last }
865
+
866
+ it "stores the documents in the correct collections" do
867
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
868
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
869
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
870
+ end
871
+ end
872
+ end
873
+ end
765
874
  end
766
875
 
767
876
  describe ".with" do