Skip to content

Commit dbbaee7

Browse files
authored
MONGOID-5291 Clarify whether :touch option is applicable for all updates (#5481)
1 parent dc0d505 commit dbbaee7

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

docs/reference/associations.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,19 +1396,21 @@ Mongoid to instantiate a new document when the association is accessed and it is
13961396

13971397
Touching
13981398
--------
1399-
14001399
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:
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

spec/mongoid/touchable_spec.rb

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@
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

@@ -624,19 +624,58 @@
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

0 commit comments

Comments
 (0)