Skip to content

Commit 0616bb4

Browse files
authored
MONGOID-5274 Add pending test for Issues with #touch method (#5204)
* MONGOID-5274 add tests for the first 2/3 problems * MONGOID-5274 rework tests * MONGOID-5274 works on referenced * MONGOID-5274 add tests to illustrate errors * MONGOID-5274 clean up tests * MONGOID-5274 remove test and comment * MONGOID-5274 wip * MONGOID-5274 update tests and add pending to failing one * MONGOID-5274 fix tests * MONGOID-5274 add return, and fix symbol
1 parent 8b9a081 commit 0616bb4

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

lib/mongoid/touchable.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def touch(field = nil)
6060

6161
# Callbacks are invoked on the composition root first and on the
6262
# leaf-most embedded document last.
63-
# TODO add tests, see MONGOID-5015.
6463
run_callbacks(:touch)
6564
true
6665
end

spec/mongoid/touchable_spec.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,5 +584,121 @@
584584
end
585585
end
586586
end
587+
588+
context "when the touch option is true" do
589+
590+
shared_examples "updates the updated_at" do
591+
592+
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
593+
594+
let(:update_time) do
595+
Timecop.freeze(Time.at(Time.now.to_i) + 2)
596+
end
597+
598+
after do
599+
Timecop.return
600+
end
601+
602+
let(:building) do
603+
parent_cls.create!
604+
end
605+
606+
let(:floor) do
607+
building.floors.create!
608+
end
609+
610+
before do
611+
floor
612+
update_time
613+
floor.level = 9
614+
floor.send(meth)
615+
end
616+
617+
it "the parent is not nil" do
618+
expect(floor.building).to_not be nil
619+
end
620+
621+
it "updates the parent's timestamp" do
622+
building.updated_at.should == update_time
623+
building.reload.updated_at.should == update_time
624+
end
625+
end
626+
627+
[ :save!, :destroy, :touch].each do |meth|
628+
context "with #{meth} on referenced associations" do
629+
let(:parent_cls) { TouchableSpec::Referenced::Building }
630+
let(:meth) { meth }
631+
632+
include_examples "updates the updated_at"
633+
end
634+
635+
context "with #{meth} on embedded associations" do
636+
let(:parent_cls) { TouchableSpec::Embedded::Building }
637+
let(:meth) { meth }
638+
639+
include_examples "updates the updated_at"
640+
end
641+
end
642+
end
643+
644+
context "when the touch option is false" do
645+
646+
shared_examples "does not update the parent" do
647+
648+
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
649+
650+
let(:update_time) do
651+
Timecop.freeze(Time.at(Time.now.to_i) + 2)
652+
end
653+
654+
after do
655+
Timecop.return
656+
end
657+
658+
let(:building) do
659+
parent_cls.create!
660+
end
661+
662+
let(:entrance) do
663+
building.entrances.create!
664+
end
665+
666+
before do
667+
entrance
668+
update_time
669+
entrance.touch
670+
end
671+
672+
it "updates the child's timestamp" do
673+
entrance.updated_at.should == update_time
674+
entrance.reload.updated_at.should == update_time
675+
end
676+
677+
it "does not update the parent's timestamp" do
678+
building.updated_at.should == start_time
679+
building.reload.updated_at.should == start_time
680+
end
681+
end
682+
683+
[ :save!, :destroy, :touch].each do |meth|
684+
context "with #{meth} on belongs_to" do
685+
let(:meth) { meth }
686+
let(:parent_cls) { TouchableSpec::Referenced::Building }
687+
688+
include_examples "does not update the parent"
689+
end
690+
691+
context "with #{meth} on embedded_in" do
692+
let(:meth) { meth }
693+
let(:parent_cls) { TouchableSpec::Embedded::Building }
694+
695+
before do
696+
skip "MONGOID-5274"
697+
end
698+
699+
include_examples "does not update the parent"
700+
end
701+
end
702+
end
587703
end
588704
end

spec/mongoid/touchable_spec_models.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ class Building
66
include Mongoid::Document
77
include Mongoid::Timestamps
88

9-
embeds_many :entrances
10-
embeds_many :floors
9+
embeds_many :entrances, class_name: "TouchableSpec::Embedded::Entrance"
10+
embeds_many :floors, class_name: "TouchableSpec::Embedded::Floor"
1111
end
1212

1313
class Entrance
1414
include Mongoid::Document
1515
include Mongoid::Timestamps
1616

17-
embedded_in :building
17+
embedded_in :building, class_name: "TouchableSpec::Embedded::Building"
1818

1919
field :last_used_at, type: Time
2020
end
@@ -23,7 +23,9 @@ class Floor
2323
include Mongoid::Document
2424
include Mongoid::Timestamps
2525

26-
embedded_in :building, touch: true
26+
field :level, type: Integer
27+
28+
embedded_in :building, touch: true, class_name: "TouchableSpec::Embedded::Building"
2729
end
2830
end
2931

@@ -32,22 +34,24 @@ class Building
3234
include Mongoid::Document
3335
include Mongoid::Timestamps
3436

35-
has_many :entrances, inverse_of: :building
36-
has_many :floors, inverse_of: :building
37+
has_many :entrances, inverse_of: :building, class_name: "TouchableSpec::Referenced::Entrance"
38+
has_many :floors, inverse_of: :building, class_name: "TouchableSpec::Referenced::Floor"
3739
end
3840

3941
class Entrance
4042
include Mongoid::Document
4143
include Mongoid::Timestamps
4244

43-
belongs_to :building
45+
belongs_to :building, touch: false, class_name: "TouchableSpec::Referenced::Building"
4446
end
4547

4648
class Floor
4749
include Mongoid::Document
4850
include Mongoid::Timestamps
4951

50-
belongs_to :building, touch: true
52+
field :level, type: Integer
53+
54+
belongs_to :building, touch: true, class_name: "TouchableSpec::Referenced::Building"
5155
end
5256
end
5357
end

0 commit comments

Comments
 (0)