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
13961396
13971397Touching
13981398--------
1399-
14001399Any ``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 :
14031402
14041403.. code-block:: ruby
14051404
14061405 class Band
14071406 include Mongoid::Document
1407+ field :name
14081408 belongs_to :label, touch: true
14091409 end
14101410
14111411 band = Band.first
1412+ band.name = "The Rolling Stones"
1413+ band.save! # Calls touch on the parent label.
14121414 band.touch # Calls touch on the parent label.
14131415
14141416``:touch`` can also take a string or symbol argument specifying a field to
Original file line number Diff line number Diff line change 587587
588588 context "when the touch option is true" do
589589
590- shared_examples "updates the updated_at" do
590+ shared_examples "updates the parent's updated_at" do
591591
592592 let! ( :start_time ) { Timecop . freeze ( Time . at ( Time . now . to_i ) ) }
593593
624624 end
625625 end
626626
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+
627664 [ :save! , :destroy , :touch ] . each do |meth |
628665 context "with #{ meth } on referenced associations" do
629666 let ( :parent_cls ) { TouchableSpec ::Referenced ::Building }
630667 let ( :meth ) { meth }
631668
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"
633671 end
634672
635673 context "with #{ meth } on embedded associations" do
636674 let ( :parent_cls ) { TouchableSpec ::Embedded ::Building }
637675 let ( :meth ) { meth }
638676
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"
640679 end
641680 end
642681 end
You can’t perform that action at this time.
0 commit comments