Use ILIKE/LIKE for searching notes
This commit is contained in:
parent
1f5284e5dd
commit
508b6b46fe
|
@ -105,8 +105,18 @@ class Note < ActiveRecord::Base
|
||||||
[:discussion, type.try(:underscore), id, line_code].join("-").to_sym
|
[:discussion, type.try(:underscore), id, line_code].join("-").to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Searches for notes matching the given query.
|
||||||
|
#
|
||||||
|
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
|
||||||
|
#
|
||||||
|
# query - The search query as a String.
|
||||||
|
#
|
||||||
|
# Returns an ActiveRecord::Relation.
|
||||||
def search(query)
|
def search(query)
|
||||||
where("LOWER(note) like :query", query: "%#{query.downcase}%")
|
table = Note.arel_table
|
||||||
|
pattern = "%#{query}%"
|
||||||
|
|
||||||
|
where(table[:note].matches(pattern))
|
||||||
end
|
end
|
||||||
|
|
||||||
def grouped_awards
|
def grouped_awards
|
||||||
|
|
|
@ -140,10 +140,16 @@ describe Note, models: true do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe :search do
|
describe '.search' do
|
||||||
let!(:note) { create(:note, note: "WoW") }
|
let(:note) { create(:note, note: 'WoW') }
|
||||||
|
|
||||||
it { expect(Note.search('wow')).to include(note) }
|
it 'returns notes with matching content' do
|
||||||
|
expect(described_class.search(note.note)).to eq([note])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns notes with matching content regardless of the casing' do
|
||||||
|
expect(described_class.search('WOW')).to eq([note])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe :grouped_awards do
|
describe :grouped_awards do
|
||||||
|
|
Loading…
Reference in New Issue