Commit 96aaeb37 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Jan Provaznik

Keep edited timestamp when reacting to old notes

Copies updated_at to last_edited_at before bumping updated_at.
last_edited_at was added later and we decided not to do a backfill
migration so we just did a fallback to updated_at for old notes.

Changelog: fixed
parent 0214d07d
......@@ -504,7 +504,15 @@ class Note < ApplicationRecord
# Instead of calling touch which is throttled via ThrottledTouch concern,
# we bump the updated_at column directly. This also prevents executing
# after_commit callbacks that we don't need.
update_column(:updated_at, Time.current)
attributes_to_update = { updated_at: Time.current }
# Notes that were edited before the `last_edited_at` column was added, fall back to `updated_at` for the edit time.
# We copy this over to the correct column so we don't erroneously change the edit timestamp.
if updated_by_id.present? && read_attribute(:last_edited_at).blank?
attributes_to_update[:last_edited_at] = updated_at
end
update_columns(attributes_to_update)
end
def expire_etag_cache
......
......@@ -1685,4 +1685,27 @@ RSpec.describe Note do
expect(note.commands_changes.keys).to contain_exactly(:emoji_award, :time_estimate, :spend_time)
end
end
describe '#bump_updated_at', :freeze_time do
it 'sets updated_at to the current timestamp' do
note = create(:note, updated_at: 1.day.ago)
note.bump_updated_at
note.reload
expect(note.updated_at).to be_like_time(Time.current)
end
context 'with legacy edited note' do
it 'copies updated_at to last_edited_at before bumping the timestamp' do
note = create(:note, updated_at: 1.day.ago, updated_by: create(:user), last_edited_at: nil)
note.bump_updated_at
note.reload
expect(note.last_edited_at).to be_like_time(1.day.ago)
expect(note.updated_at).to be_like_time(Time.current)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment