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-5825 Do not update timestamp on destroyed #5946

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
9 changes: 8 additions & 1 deletion lib/mongoid/timestamps/created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ module Created
# @example Set the created at time.
# person.set_created_at
def set_created_at
if !timeless? && !created_at
if able_to_set_created_at?
time = Time.configured.now
self.updated_at = time if is_a?(Updated) && !updated_at_changed?
self.created_at = time
end
clear_timeless_option
end

# Is the created timestamp able to be set?
#
# @return [ true, false ] If the timestamp can be set.
def able_to_set_created_at?
!frozen? && !timeless? && !created_at
end
end
end
end
23 changes: 23 additions & 0 deletions spec/mongoid/timestamps/created_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,27 @@
expect(quiz.created_at).to be_within(10).of(Time.now.utc)
end
end

context "when the document is destroyed" do
let(:book) do
Book.create!
end

before do
Cover.before_save do
destroy if title == "delete me"
end
end

after do
Cover.reset_callbacks(:save)
end

it "does not set the created_at timestamp" do
book.covers << Cover.new(title: "delete me")
expect {
book.save
}.not_to raise_error
end
end
end
Loading