Skip to content
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 @@ -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
Loading