diff --git a/app/models/concerns/notes/active_record.rb b/app/models/concerns/notes/active_record.rb index ae89beb3271a73902ff0cb0517c7037b8cf79d38..42ebce4d553588124f5e3eea292b9cbd9f4a9111 100644 --- a/app/models/concerns/notes/active_record.rb +++ b/app/models/concerns/notes/active_record.rb @@ -5,13 +5,8 @@ module ActiveRecord extend ActiveSupport::Concern included do - # Aliases to make application_helper#edited_time_ago_with_tooltip helper work properly with notes. - # See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/10392/diffs#note_28719102 - alias_attribute :last_edited_by, :updated_by - belongs_to :author, class_name: "User" belongs_to :updated_by, class_name: "User" - belongs_to :last_edited_by, class_name: 'User' has_many :todos @@ -22,13 +17,21 @@ module ActiveRecord validates :author, presence: true validates :discussion_id, presence: true, format: { with: /\A\h{40}\z/ } validate :validate_created_after + end + + # Alias to make application_helper#edited_time_ago_with_tooltip helper work properly with notes. + # See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/10392/diffs#note_28719102 + def last_edited_by + updated_by + end + + private - def validate_created_after - return unless created_at - return if created_at >= '1970-01-01' + def validate_created_after + return unless created_at + return if created_at >= '1970-01-01' - errors.add(:created_at, s_('Note|The created date provided is too far in the past.')) - end + errors.add(:created_at, s_('Note|The created date provided is too far in the past.')) end end end diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index e624d1477cc905ef527ca24c7c12bac5e0493ad3..f8746efa6bd2bcf0939067d3e62c0161229e4404 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -726,6 +726,36 @@ def retrieve_participants end end + describe '#last_edited_by' do + let(:user) { build(:user) } + + context 'when last_edited_at is nil' do + let(:note) { build(:note, last_edited_at: nil, updated_by: user) } + + it 'returns nil' do + expect(note.last_edited_by).to be_nil + end + end + + context 'when last_edited_at is set' do + context 'when updated_by is set' do + let(:note) { build(:note, last_edited_at: Time.current, updated_by: user) } + + it 'returns the updated_by user' do + expect(note.last_edited_by).to eq(user) + end + end + + context 'when updated_by is not set' do + let(:note) { build(:note, last_edited_at: Time.current, updated_by: nil) } + + it 'returns the ghost user' do + expect(note.last_edited_by).to eq(Users::Internal.ghost) + end + end + end + end + describe '#confidential?' do context 'when note is not confidential' do context 'when include_noteable is set to true' do