From 264fbc86f3e9a6151113a8c048582a50b48c7d76 Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 27 May 2024 11:05:07 -0400 Subject: [PATCH 1/6] [MONGOID-5508] Added a class for Label based on ticket example --- spec/mongoid/touchable_spec_models.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/mongoid/touchable_spec_models.rb b/spec/mongoid/touchable_spec_models.rb index 4dae9c4464..6d92592737 100644 --- a/spec/mongoid/touchable_spec_models.rb +++ b/spec/mongoid/touchable_spec_models.rb @@ -150,5 +150,12 @@ class Sofa embedded_in :floor, touch: true, class_name: "TouchableSpec::Referenced::Floor" end + + class Label + include Mongoid::Document + field :band_updated_at, type: DateTime + field :name, type: String + has_many :bands + end end end From 26eb336804137814da88aebb418f04a2c99da35b Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 27 May 2024 11:07:31 -0400 Subject: [PATCH 2/6] [MONGOID-5508] Added a Band class based on issue example --- spec/mongoid/touchable_spec_models.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/mongoid/touchable_spec_models.rb b/spec/mongoid/touchable_spec_models.rb index 6d92592737..f85c1c09f9 100644 --- a/spec/mongoid/touchable_spec_models.rb +++ b/spec/mongoid/touchable_spec_models.rb @@ -153,9 +153,16 @@ class Sofa class Label include Mongoid::Document - field :band_updated_at, type: DateTime + field :bands_updated_at, type: DateTime field :name, type: String has_many :bands - end + end + + class Band + include Mongoid::Document + field :name, type: String + belongs_to :label, touch :bands_updated_at + end + end end From 3a2a1a54404c8348d29caad4fb966f365d437fba Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 27 May 2024 13:21:14 -0400 Subject: [PATCH 3/6] [MONGOID-5508] Added class_name to Band,Label and included timestamps for managing updated_at --- spec/mongoid/touchable_spec_models.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/spec/mongoid/touchable_spec_models.rb b/spec/mongoid/touchable_spec_models.rb index f85c1c09f9..31fabf1e09 100644 --- a/spec/mongoid/touchable_spec_models.rb +++ b/spec/mongoid/touchable_spec_models.rb @@ -152,16 +152,18 @@ class Sofa end class Label - include Mongoid::Document - field :bands_updated_at, type: DateTime - field :name, type: String - has_many :bands + include Mongoid::Document + include Mongoid::Timestamps + + field :bands_updated_at, type: DateTime + has_many :bands, class_name: "TouchableSpec::Referenced::Band" end class Band - include Mongoid::Document - field :name, type: String - belongs_to :label, touch :bands_updated_at + include Mongoid::Document + include Mongoid::Timestamps + + belongs_to :label, touch: :bands_updated_at, class_name: "TouchableSpec::Referenced::Label" end end From e02d93968a7a97ee9940dcec784b7ef7d6f4b834 Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 27 May 2024 13:23:54 -0400 Subject: [PATCH 4/6] [MONGOID-5508] Added spec to test field is updated along with timestamps for parent and child --- spec/mongoid/touchable_spec.rb | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spec/mongoid/touchable_spec.rb b/spec/mongoid/touchable_spec.rb index 578f22ed6c..42c72c4b1f 100644 --- a/spec/mongoid/touchable_spec.rb +++ b/spec/mongoid/touchable_spec.rb @@ -733,6 +733,44 @@ end end + context 'when a custom field is specified' do + let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } + let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } + + after do + Timecop.return + end + + let!(:label) do + TouchableSpec::Referenced::Label.create! + end + + let!(:band) do + TouchableSpec::Referenced::Band.create!(label: label) + end + + before do + update_time + band.touch + end + + it "updates the specified field in the parent document" do + expect(label.bands_updated_at).to eq(update_time) + expect(label.reload.bands_updated_at).to eq(update_time) + end + + it "updates the parent's timestamp" do + expect(label.updated_at).to eq(update_time) + expect(label.reload.updated_at).to eq(update_time) + end + + it "updates the child's timestamp" do + expect(band.updated_at).to eq(update_time) + expect(band.reload.updated_at).to eq(update_time) + end + + end + context 'multi-level' do let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } From e7faad5582ba04872740da9934e2489ed5686ff6 Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 10 Jun 2024 15:44:50 -0400 Subject: [PATCH 5/6] [MONGOID-5508] Added test for codepaths of save and destroy and fixed utc conversion. Off by 2 miliseconds --- spec/mongoid/touchable_spec.rb | 94 ++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/spec/mongoid/touchable_spec.rb b/spec/mongoid/touchable_spec.rb index 42c72c4b1f..bae0ad7898 100644 --- a/spec/mongoid/touchable_spec.rb +++ b/spec/mongoid/touchable_spec.rb @@ -733,42 +733,80 @@ end end - context 'when a custom field is specified' do - let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } - let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } + context "when a custom field is specified" do - after do - Timecop.return - end + shared_examples "updates the child's updated_at" do - let!(:label) do - TouchableSpec::Referenced::Label.create! - end - - let!(:band) do - TouchableSpec::Referenced::Band.create!(label: label) - end + let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } - before do - update_time - band.touch - end + let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } + + after do + Timecop.return + end + + let!(:label) do + TouchableSpec::Referenced::Label.create! + end - it "updates the specified field in the parent document" do - expect(label.bands_updated_at).to eq(update_time) - expect(label.reload.bands_updated_at).to eq(update_time) + let(:band) do + TouchableSpec::Referenced::Band.create!(label: label) + end + + before do + band + update_time + band.send(meth) + end + + it "updates the child's timestamp" do + # expect(band.updated_at).to eq(update_time.utc) + expect(band.reload.updated_at).to eq(update_time.utc) + end end - it "updates the parent's timestamp" do - expect(label.updated_at).to eq(update_time) - expect(label.reload.updated_at).to eq(update_time) - end + shared_examples "updates the parent's custom field and updated_at" do - it "updates the child's timestamp" do - expect(band.updated_at).to eq(update_time) - expect(band.reload.updated_at).to eq(update_time) - end + let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } + let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } + + after do + Timecop.return + end + + let!(:label) do + TouchableSpec::Referenced::Label.create! + end + + let!(:band) do + TouchableSpec::Referenced::Band.create!(label: label) + end + + before do + update_time + band.send(meth) + end + + it "updates the parent's custom field" do + expect(label.bands_updated_at).to eq(update_time) + expect(label.reload.bands_updated_at).to eq(update_time) + end + + it "updates the parent's timestamp" do + expect(label.updated_at).to eq(update_time) + expect(label.reload.updated_at).to eq(update_time) + end + + end + + [:save, :destroy, :touch].each do |meth| + context "with #{meth} on referenced associations" do + let(:meth) { meth } + include_examples "updates the child's updated_at" unless meth == :destroy + include_examples "updates the parent's custom field and updated_at" + end + end end context 'multi-level' do From e3c6844794fab4b6f5d333e9edc90e20394ff00b Mon Sep 17 00:00:00 2001 From: Adviti Mishra Date: Mon, 10 Jun 2024 16:09:57 -0400 Subject: [PATCH 6/6] [MONGOID-5508] It passes tests with save, destroy, and touch codepaths too --- spec/mongoid/touchable_spec.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/mongoid/touchable_spec.rb b/spec/mongoid/touchable_spec.rb index bae0ad7898..287e363839 100644 --- a/spec/mongoid/touchable_spec.rb +++ b/spec/mongoid/touchable_spec.rb @@ -754,14 +754,13 @@ end before do - band update_time band.send(meth) end it "updates the child's timestamp" do - # expect(band.updated_at).to eq(update_time.utc) - expect(band.reload.updated_at).to eq(update_time.utc) + expect(band.updated_at).to eq(update_time) + expect(band.reload.updated_at).to eq(update_time) end end