Use ILIKE/LIKE for searching notes

This commit is contained in:
Yorick Peterse 2016-03-01 15:43:19 +01:00 committed by Robert Speicher
parent 1f5284e5dd
commit 508b6b46fe
2 changed files with 20 additions and 4 deletions

View File

@ -105,8 +105,18 @@ class Note < ActiveRecord::Base
[:discussion, type.try(:underscore), id, line_code].join("-").to_sym
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)
where("LOWER(note) like :query", query: "%#{query.downcase}%")
table = Note.arel_table
pattern = "%#{query}%"
where(table[:note].matches(pattern))
end
def grouped_awards

View File

@ -140,10 +140,16 @@ describe Note, models: true do
end
end
describe :search do
let!(:note) { create(:note, note: "WoW") }
describe '.search' do
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
describe :grouped_awards do