Skip to content

Commit

Permalink
MONGOID-5825 Do not update timestamp on destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Jan 21, 2025
1 parent 55beb7d commit 090ce25
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/mongoid/timestamps/created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,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?
now = Time.current
self.updated_at = now if is_a?(Updated) && !updated_at_changed?
self.created_at = now
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 @@ -44,4 +44,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

0 comments on commit 090ce25

Please sign in to comment.