Skip to content

Commit 902fbc4

Browse files
MONGOID-5825 Do not update timestamp on destroyed (#5939)
1 parent 837f255 commit 902fbc4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/mongoid/timestamps/created.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ module Created
2323
# @example Set the created at time.
2424
# person.set_created_at
2525
def set_created_at
26-
if !timeless? && !created_at
26+
if able_to_set_created_at?
2727
now = Time.current
2828
self.updated_at = now if is_a?(Updated) && !updated_at_changed?
2929
self.created_at = now
3030
end
3131
clear_timeless_option
3232
end
33+
34+
# Is the created timestamp able to be set?
35+
#
36+
# @return [ true, false ] If the timestamp can be set.
37+
def able_to_set_created_at?
38+
!frozen? && !timeless? && !created_at
39+
end
3340
end
3441
end
3542
end

spec/mongoid/timestamps/created_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,27 @@
4444
expect(quiz.created_at).to be_within(10).of(Time.now.utc)
4545
end
4646
end
47+
48+
context "when the document is destroyed" do
49+
let(:book) do
50+
Book.create!
51+
end
52+
53+
before do
54+
Cover.before_save do
55+
destroy if title == "delete me"
56+
end
57+
end
58+
59+
after do
60+
Cover.reset_callbacks(:save)
61+
end
62+
63+
it "does not set the created_at timestamp" do
64+
book.covers << Cover.new(title: "delete me")
65+
expect {
66+
book.save
67+
}.not_to raise_error
68+
end
69+
end
4770
end

0 commit comments

Comments
 (0)