From 953501c1486284e6aa1bdae8489d41012e37683a Mon Sep 17 00:00:00 2001 From: Darshana Venkatesh Date: Fri, 6 Dec 2024 15:50:05 -0800 Subject: [PATCH 1/4] fixed timeless error by updating touch --- lib/mongoid/persistable/updatable.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mongoid/persistable/updatable.rb b/lib/mongoid/persistable/updatable.rb index 0ba8dc08a6..ea9aae3e8a 100644 --- a/lib/mongoid/persistable/updatable.rb +++ b/lib/mongoid/persistable/updatable.rb @@ -104,6 +104,7 @@ def prepare_update(options = {}) invalid?(options[:context] || :update) process_flagged_destroys update_children = cascadable_children(:update) + options[:touch] = false if timeless? process_touch_option(options, update_children) do run_all_callbacks_for_update(update_children) do result = yield(self) From fa97a819a8000aeba8453d7716f8aedc5987efe3 Mon Sep 17 00:00:00 2001 From: Darshana Venkatesh Date: Mon, 9 Dec 2024 12:03:19 -0800 Subject: [PATCH 2/4] added test --- spec/mongoid/timestamps/timeless_spec.rb | 41 ++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/spec/mongoid/timestamps/timeless_spec.rb b/spec/mongoid/timestamps/timeless_spec.rb index 9ea2c8a7e3..4dace4ce2c 100644 --- a/spec/mongoid/timestamps/timeless_spec.rb +++ b/spec/mongoid/timestamps/timeless_spec.rb @@ -12,6 +12,10 @@ class Chicken include Mongoid::Document include Mongoid::Timestamps + field :color, type: String + + embeds_many :chicks, class_name: 'Chick', cascade_callbacks: true + before_save :lay_timeless_egg def lay_timeless_egg @@ -23,6 +27,12 @@ class Egg include Mongoid::Document include Mongoid::Timestamps end + + class Chick + include Mongoid::Document + include Mongoid::Timestamps + embedded_in :chicken, class_name: 'Chicken' + end end after(:all) do @@ -70,7 +80,7 @@ class Egg Chicken.timeless.create! end - it "creates the parent with a timestamp" do + it "creates the parent with no timestamp" do expect(chicken.created_at).to be_nil end @@ -78,6 +88,33 @@ class Egg expect(Egg.last.created_at).to be_nil end end + + context "when root contains embedded doc and executes timeless" do + + let!(:chicken) do + Chicken.create!(color: "red", chicks: [Chick.new]) + end + + before do + @before_update_chicken_timestamp = chicken.updated_at + @before_update_chick_timestamp = chicken.chicks.first.updated_at + + chicken.color = "white" + sleep 2 + chicken.timeless.save() + + @after_update_chicken_timestamp = chicken.updated_at + @after_update_chick_timestamp = chicken.chicks.first.updated_at + end + + it "does not change the updated_at timestamp on the parent" do + expect(@after_update_chicken_timestamp).to eq(@before_update_chicken_timestamp) + end + + it "does not change the updated_at timestamp on the embedded document" do + expect(@after_update_chick_timestamp).to eq(@before_update_chick_timestamp) + end + end end context "when used as a proxy method" do @@ -147,4 +184,4 @@ class Egg end end end -end +end \ No newline at end of file From 9c4409830b6fd8c96e975984cccfbcfa2a016495 Mon Sep 17 00:00:00 2001 From: Darshana Venkatesh Date: Mon, 9 Dec 2024 13:50:33 -0800 Subject: [PATCH 3/4] included chick change for testing --- spec/mongoid/timestamps/timeless_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/mongoid/timestamps/timeless_spec.rb b/spec/mongoid/timestamps/timeless_spec.rb index 4dace4ce2c..fe0e194f20 100644 --- a/spec/mongoid/timestamps/timeless_spec.rb +++ b/spec/mongoid/timestamps/timeless_spec.rb @@ -31,6 +31,7 @@ class Egg class Chick include Mongoid::Document include Mongoid::Timestamps + field :color, type: String embedded_in :chicken, class_name: 'Chicken' end end @@ -38,6 +39,7 @@ class Chick after(:all) do Object.send(:remove_const, :Chicken) Object.send(:remove_const, :Egg) + Object.send(:remove_const, :Chick) end context "when timeless is used on one instance and then not used on another instance" do @@ -92,14 +94,15 @@ class Chick context "when root contains embedded doc and executes timeless" do let!(:chicken) do - Chicken.create!(color: "red", chicks: [Chick.new]) + Chicken.create!(color: "red", chicks: [Chick.new(color: "red")]) end before do @before_update_chicken_timestamp = chicken.updated_at @before_update_chick_timestamp = chicken.chicks.first.updated_at - chicken.color = "white" + chicken.chicks.first.color = "white" + sleep 2 chicken.timeless.save() @@ -114,6 +117,7 @@ class Chick it "does not change the updated_at timestamp on the embedded document" do expect(@after_update_chick_timestamp).to eq(@before_update_chick_timestamp) end + end end From 1c099a9794fe308dcedb89ebe126c31b68a4f3cb Mon Sep 17 00:00:00 2001 From: Darshana Venkatesh Date: Mon, 9 Dec 2024 14:14:23 -0800 Subject: [PATCH 4/4] only set to false if timeless defined --- lib/mongoid/persistable/updatable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid/persistable/updatable.rb b/lib/mongoid/persistable/updatable.rb index ea9aae3e8a..cf9a5a56d3 100644 --- a/lib/mongoid/persistable/updatable.rb +++ b/lib/mongoid/persistable/updatable.rb @@ -104,7 +104,7 @@ def prepare_update(options = {}) invalid?(options[:context] || :update) process_flagged_destroys update_children = cascadable_children(:update) - options[:touch] = false if timeless? + options[:touch] = false if respond_to?(:timeless?) && timeless? process_touch_option(options, update_children) do run_all_callbacks_for_update(update_children) do result = yield(self)