Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5782 Ensure embedded documents are also marked timeless #5912

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mongoid/persistable/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 43 additions & 2 deletions spec/mongoid/timestamps/timeless_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -70,14 +82,43 @@ 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

it "creates the child with no timestamp" do
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
Expand Down Expand Up @@ -147,4 +188,4 @@ class Egg
end
end
end
end
end
Loading