File tree Expand file tree Collapse file tree 2 files changed +47
-6
lines changed Expand file tree Collapse file tree 2 files changed +47
-6
lines changed Original file line number Diff line number Diff line change @@ -1396,19 +1396,21 @@ Mongoid to instantiate a new document when the association is accessed and it is
1396
1396
1397
1397
Touching
1398
1398
--------
1399
-
1400
1399
Any ``belongs_to`` association can take an optional ``:touch`` option which
1401
- will cause the parent document be touched whenever the child document is
1402
- touched :
1400
+ will cause the parent document to be touched whenever the child document is
1401
+ updated :
1403
1402
1404
1403
.. code-block:: ruby
1405
1404
1406
1405
class Band
1407
1406
include Mongoid::Document
1407
+ field :name
1408
1408
belongs_to :label, touch: true
1409
1409
end
1410
1410
1411
1411
band = Band.first
1412
+ band.name = "The Rolling Stones"
1413
+ band.save! # Calls touch on the parent label.
1412
1414
band.touch # Calls touch on the parent label.
1413
1415
1414
1416
``:touch`` can also take a string or symbol argument specifying a field to
Original file line number Diff line number Diff line change 587
587
588
588
context "when the touch option is true" do
589
589
590
- shared_examples "updates the updated_at" do
590
+ shared_examples "updates the parent's updated_at" do
591
591
592
592
let! ( :start_time ) { Timecop . freeze ( Time . at ( Time . now . to_i ) ) }
593
593
624
624
end
625
625
end
626
626
627
+ shared_examples "updates the child's updated_at" do
628
+
629
+ let! ( :start_time ) { Timecop . freeze ( Time . at ( Time . now . to_i ) ) }
630
+
631
+ let ( :update_time ) do
632
+ Timecop . freeze ( Time . at ( Time . now . to_i ) + 2 )
633
+ end
634
+
635
+ after do
636
+ Timecop . return
637
+ end
638
+
639
+ let ( :building ) do
640
+ parent_cls . create!
641
+ end
642
+
643
+ let ( :floor ) do
644
+ building . floors . create!
645
+ end
646
+
647
+ before do
648
+ floor
649
+ update_time
650
+ floor . level = 9
651
+ floor . send ( meth )
652
+ end
653
+
654
+ it "the parent is not nil" do
655
+ expect ( floor . building ) . to_not be nil
656
+ end
657
+
658
+ it "updates the child's timestamp" do
659
+ floor . updated_at . should == update_time
660
+ floor . reload . updated_at . should == update_time
661
+ end
662
+ end
663
+
627
664
[ :save! , :destroy , :touch ] . each do |meth |
628
665
context "with #{ meth } on referenced associations" do
629
666
let ( :parent_cls ) { TouchableSpec ::Referenced ::Building }
630
667
let ( :meth ) { meth }
631
668
632
- include_examples "updates the updated_at"
669
+ include_examples "updates the child's updated_at" unless meth == :destroy
670
+ include_examples "updates the parent's updated_at"
633
671
end
634
672
635
673
context "with #{ meth } on embedded associations" do
636
674
let ( :parent_cls ) { TouchableSpec ::Embedded ::Building }
637
675
let ( :meth ) { meth }
638
676
639
- include_examples "updates the updated_at"
677
+ include_examples "updates the child's updated_at" unless meth == :destroy
678
+ include_examples "updates the parent's updated_at"
640
679
end
641
680
end
642
681
end
You can’t perform that action at this time.
0 commit comments