Commit 508b6b46 authored by Yorick Peterse's avatar Yorick Peterse Committed by Robert Speicher

Use ILIKE/LIKE for searching notes

parent 1f5284e5
...@@ -105,8 +105,18 @@ class Note < ActiveRecord::Base ...@@ -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 ...@@ -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
......
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