Skip to content

Commit d88728b

Browse files
MONGOID-5825 Do not update timestamp on destroyed (#5939) (#5945) (#5946)
1 parent 1b51522 commit d88728b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/mongoid/timestamps/created.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ module Created
2222
# @example Set the created at time.
2323
# person.set_created_at
2424
def set_created_at
25-
if !timeless? && !created_at
25+
if able_to_set_created_at?
2626
time = Time.configured.now
2727
self.updated_at = time if is_a?(Updated) && !updated_at_changed?
2828
self.created_at = time
2929
end
3030
clear_timeless_option
3131
end
32+
33+
# Is the created timestamp able to be set?
34+
#
35+
# @return [ true, false ] If the timestamp can be set.
36+
def able_to_set_created_at?
37+
!frozen? && !timeless? && !created_at
38+
end
3239
end
3340
end
3441
end

spec/mongoid/timestamps/created_spec.rb

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

0 commit comments

Comments
 (0)