mongoid 8.0.8 → 8.1.0

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 (181) 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 +7 -16
  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/associated.rb +18 -96
  90. data/lib/mongoid/validatable/macros.rb +5 -5
  91. data/lib/mongoid/validatable.rb +4 -9
  92. data/lib/mongoid/version.rb +1 -1
  93. data/lib/mongoid/warnings.rb +17 -1
  94. data/lib/mongoid.rb +16 -3
  95. data/spec/integration/app_spec.rb +2 -2
  96. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -40
  97. data/spec/integration/callbacks_models.rb +37 -0
  98. data/spec/integration/callbacks_spec.rb +126 -12
  99. data/spec/integration/discriminator_key_spec.rb +4 -5
  100. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  101. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  102. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  103. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  104. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  105. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  106. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  107. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  108. data/spec/mongoid/association/syncable_spec.rb +1 -1
  109. data/spec/mongoid/attributes_spec.rb +3 -33
  110. data/spec/mongoid/changeable_spec.rb +299 -24
  111. data/spec/mongoid/clients_spec.rb +122 -13
  112. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  113. data/spec/mongoid/config/defaults_spec.rb +160 -0
  114. data/spec/mongoid/config_spec.rb +154 -27
  115. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  116. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  117. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  118. data/spec/mongoid/contextual/none_spec.rb +49 -2
  119. data/spec/mongoid/copyable_spec.rb +2 -10
  120. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  121. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  122. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  123. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  124. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  125. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  126. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  127. data/spec/mongoid/criteria_spec.rb +5 -9
  128. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  129. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  130. data/spec/mongoid/extensions/time_spec.rb +8 -43
  131. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  132. data/spec/mongoid/fields/localized_spec.rb +46 -28
  133. data/spec/mongoid/fields_spec.rb +136 -77
  134. data/spec/mongoid/findable_spec.rb +391 -34
  135. data/spec/mongoid/indexable_spec.rb +16 -10
  136. data/spec/mongoid/interceptable_spec.rb +173 -362
  137. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  138. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  139. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  140. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  141. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  142. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  143. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  144. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  145. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  146. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  147. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  148. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  149. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  150. data/spec/mongoid/persistence_context_spec.rb +7 -57
  151. data/spec/mongoid/query_cache_spec.rb +56 -61
  152. data/spec/mongoid/reloadable_spec.rb +24 -28
  153. data/spec/mongoid/scopable_spec.rb +70 -0
  154. data/spec/mongoid/serializable_spec.rb +9 -30
  155. data/spec/mongoid/stateful_spec.rb +122 -8
  156. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  157. data/spec/mongoid/tasks/database_spec.rb +127 -0
  158. data/spec/mongoid/timestamps_spec.rb +9 -11
  159. data/spec/mongoid/touchable_spec.rb +277 -5
  160. data/spec/mongoid/touchable_spec_models.rb +3 -1
  161. data/spec/mongoid/traversable_spec.rb +9 -24
  162. data/spec/mongoid/validatable/associated_spec.rb +30 -13
  163. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  164. data/spec/mongoid_spec.rb +36 -10
  165. data/spec/spec_helper.rb +5 -0
  166. data/spec/support/immutable_ids.rb +118 -0
  167. data/spec/support/macros.rb +47 -15
  168. data/spec/support/models/artist.rb +0 -1
  169. data/spec/support/models/band.rb +1 -0
  170. data/spec/support/models/book.rb +1 -0
  171. data/spec/support/models/building.rb +2 -0
  172. data/spec/support/models/cover.rb +10 -0
  173. data/spec/support/models/name.rb +0 -10
  174. data/spec/support/models/person.rb +0 -1
  175. data/spec/support/models/product.rb +1 -0
  176. data.tar.gz.sig +0 -0
  177. metadata +698 -664
  178. metadata.gz.sig +0 -0
  179. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  180. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  181. data/spec/support/models/purse.rb +0 -9
@@ -23,10 +23,9 @@ module RefHasManySpec
23
23
  end
24
24
 
25
25
  describe Mongoid::Association::Referenced::HasMany::Proxy do
26
+ config_override :raise_not_found_error, true
26
27
 
27
28
  before :all do
28
- Mongoid.raise_not_found_error = true
29
-
30
29
  Drug.belongs_to :person, primary_key: :username
31
30
  Person.has_many :drugs, validate: false, primary_key: :username
32
31
  end
@@ -2535,10 +2534,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2535
2534
  context "when the id does not match" do
2536
2535
 
2537
2536
  context "when config set to raise error" do
2538
-
2539
- before do
2540
- Mongoid.raise_not_found_error = true
2541
- end
2537
+ config_override :raise_not_found_error, true
2542
2538
 
2543
2539
  it "raises an error" do
2544
2540
  expect {
@@ -2548,19 +2544,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2548
2544
  end
2549
2545
 
2550
2546
  context "when config set not to raise error" do
2547
+ config_override :raise_not_found_error, false
2551
2548
 
2552
2549
  let(:post) do
2553
2550
  person.posts.find(BSON::ObjectId.new)
2554
2551
  end
2555
2552
 
2556
- before do
2557
- Mongoid.raise_not_found_error = false
2558
- end
2559
-
2560
- after do
2561
- Mongoid.raise_not_found_error = true
2562
- end
2563
-
2564
2553
  it "returns nil" do
2565
2554
  expect(post).to be_nil
2566
2555
  end
@@ -2584,10 +2573,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2584
2573
  context "when the ids do not match" do
2585
2574
 
2586
2575
  context "when config set to raise error" do
2587
-
2588
- before do
2589
- Mongoid.raise_not_found_error = true
2590
- end
2576
+ config_override :raise_not_found_error, true
2591
2577
 
2592
2578
  it "raises an error" do
2593
2579
  expect {
@@ -2597,19 +2583,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2597
2583
  end
2598
2584
 
2599
2585
  context "when config set not to raise error" do
2586
+ config_override :raise_not_found_error, false
2600
2587
 
2601
2588
  let(:posts) do
2602
2589
  person.posts.find([ BSON::ObjectId.new ])
2603
2590
  end
2604
2591
 
2605
- before do
2606
- Mongoid.raise_not_found_error = false
2607
- end
2608
-
2609
- after do
2610
- Mongoid.raise_not_found_error = true
2611
- end
2612
-
2613
2592
  it "returns an empty array" do
2614
2593
  expect(posts).to be_empty
2615
2594
  end
@@ -2648,10 +2627,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2648
2627
  context "when the id does not match" do
2649
2628
 
2650
2629
  context "when config set to raise error" do
2651
-
2652
- before do
2653
- Mongoid.raise_not_found_error = true
2654
- end
2630
+ config_override :raise_not_found_error, true
2655
2631
 
2656
2632
  it "raises an error" do
2657
2633
  expect {
@@ -2661,19 +2637,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2661
2637
  end
2662
2638
 
2663
2639
  context "when config set not to raise error" do
2640
+ config_override :raise_not_found_error, false
2664
2641
 
2665
2642
  let(:rating) do
2666
2643
  movie.ratings.find(BSON::ObjectId.new)
2667
2644
  end
2668
2645
 
2669
- before do
2670
- Mongoid.raise_not_found_error = false
2671
- end
2672
-
2673
- after do
2674
- Mongoid.raise_not_found_error = true
2675
- end
2676
-
2677
2646
  it "returns nil" do
2678
2647
  expect(rating).to be_nil
2679
2648
  end
@@ -2705,10 +2674,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2705
2674
  context "when the ids do not match" do
2706
2675
 
2707
2676
  context "when config set to raise error" do
2708
-
2709
- before do
2710
- Mongoid.raise_not_found_error = true
2711
- end
2677
+ config_override :raise_not_found_error, true
2712
2678
 
2713
2679
  it "raises an error" do
2714
2680
  expect {
@@ -2718,19 +2684,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2718
2684
  end
2719
2685
 
2720
2686
  context "when config set not to raise error" do
2687
+ config_override :raise_not_found_error, false
2721
2688
 
2722
2689
  let(:ratings) do
2723
2690
  movie.ratings.find([ BSON::ObjectId.new ])
2724
2691
  end
2725
2692
 
2726
- before do
2727
- Mongoid.raise_not_found_error = false
2728
- end
2729
-
2730
- after do
2731
- Mongoid.raise_not_found_error = true
2732
- end
2733
-
2734
2693
  it "returns an empty array" do
2735
2694
  expect(ratings).to be_empty
2736
2695
  end
@@ -3,9 +3,9 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe "Syncable Association" do
6
+ config_override :raise_not_found_error, true
6
7
 
7
8
  before(:all) do
8
- Mongoid.raise_not_found_error = true
9
9
  Person._synced(Person.relations["preferences"])
10
10
  Person.has_and_belongs_to_many :preferences
11
11
  Preference.has_and_belongs_to_many :people
@@ -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