Commit 0366cda9 authored by Sean McGivern's avatar Sean McGivern

Merge branch '33629-add-scopes-to-note-pd' into 'master'

Add additional scopes to note

See merge request gitlab-org/gitlab!18309
parents c7c4ac07 ad67c419
...@@ -145,6 +145,9 @@ class Note < ApplicationRecord ...@@ -145,6 +145,9 @@ class Note < ApplicationRecord
end end
scope :with_metadata, -> { includes(:system_note_metadata) } scope :with_metadata, -> { includes(:system_note_metadata) }
scope :for_note_or_capitalized_note, ->(text) { where(note: [text, text.capitalize]) }
scope :like_note_or_capitalized_note, ->(text) { where('(note LIKE ? OR note LIKE ?)', text, text.capitalize) }
after_initialize :ensure_discussion_id after_initialize :ensure_discussion_id
before_validation :nullify_blank_type, :nullify_blank_line_code before_validation :nullify_blank_type, :nullify_blank_line_code
before_validation :set_discussion_id, on: :create before_validation :set_discussion_id, on: :create
......
...@@ -288,18 +288,16 @@ module SystemNotes ...@@ -288,18 +288,16 @@ module SystemNotes
"#{self.class.cross_reference_note_prefix}#{gfm_reference}" "#{self.class.cross_reference_note_prefix}#{gfm_reference}"
end end
# rubocop: disable CodeReuse/ActiveRecord
def notes_for_mentioner(mentioner, noteable, notes) def notes_for_mentioner(mentioner, noteable, notes)
if mentioner.is_a?(Commit) if mentioner.is_a?(Commit)
text = "#{self.class.cross_reference_note_prefix}%#{mentioner.to_reference(nil)}" text = "#{self.class.cross_reference_note_prefix}%#{mentioner.to_reference(nil)}"
notes.where('(note LIKE ? OR note LIKE ?)', text, text.capitalize) notes.like_note_or_capitalized_note(text)
else else
gfm_reference = mentioner.gfm_reference(noteable.project || noteable.group) gfm_reference = mentioner.gfm_reference(noteable.project || noteable.group)
text = cross_reference_note_content(gfm_reference) text = cross_reference_note_content(gfm_reference)
notes.where(note: [text, text.capitalize]) notes.for_note_or_capitalized_note(text)
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
def self.cross_reference_note_prefix def self.cross_reference_note_prefix
'mentioned in ' 'mentioned in '
......
...@@ -981,4 +981,55 @@ describe Note do ...@@ -981,4 +981,55 @@ describe Note do
expect(note.parent).to be_nil expect(note.parent).to be_nil
end end
end end
describe 'scopes' do
let_it_be(:note1) { create(:note, note: 'Test 345') }
let_it_be(:note2) { create(:note, note: 'Test 789') }
describe '#for_note_or_capitalized_note' do
it 'returns the expected matching note' do
notes = described_class.for_note_or_capitalized_note('Test 345')
expect(notes.count).to eq(1)
expect(notes.first.id).to eq(note1.id)
end
it 'returns the expected capitalized note' do
notes = described_class.for_note_or_capitalized_note('test 345')
expect(notes.count).to eq(1)
expect(notes.first.id).to eq(note1.id)
end
it 'does not support pattern matching' do
notes = described_class.for_note_or_capitalized_note('test%')
expect(notes.count).to eq(0)
end
end
describe '#like_note_or_capitalized_note' do
it 'returns the expected matching note' do
notes = described_class.like_note_or_capitalized_note('Test 345')
expect(notes.count).to eq(1)
expect(notes.first.id).to eq(note1.id)
end
it 'returns the expected capitalized note' do
notes = described_class.like_note_or_capitalized_note('test 345')
expect(notes.count).to eq(1)
expect(notes.first.id).to eq(note1.id)
end
it 'supports pattern matching' do
notes = described_class.like_note_or_capitalized_note('test%')
expect(notes.count).to eq(2)
expect(notes.first.id).to eq(note1.id)
expect(notes.second.id).to eq(note2.id)
end
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