diff --git a/lib/mongoid/persistable/updatable.rb b/lib/mongoid/persistable/updatable.rb index 0ba8dc08a6..cf9a5a56d3 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 respond_to?(:timeless?) && timeless? process_touch_option(options, update_children) do run_all_callbacks_for_update(update_children) do result = yield(self) diff --git a/spec/mongoid/timestamps/timeless_spec.rb b/spec/mongoid/timestamps/timeless_spec.rb index 9ea2c8a7e3..fe0e194f20 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,11 +27,19 @@ class Egg include Mongoid::Document include Mongoid::Timestamps end + + class Chick + include Mongoid::Document + include Mongoid::Timestamps + field :color, type: String + embedded_in :chicken, class_name: 'Chicken' + end end 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 @@ -70,7 +82,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 +90,35 @@ 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(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() + + @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 +188,4 @@ class Egg end end end -end +end \ No newline at end of file