Commit db615d0a authored by Yorick Peterse's avatar Yorick Peterse Committed by Robert Speicher

Use ILIKE in Project.search_by_title

Similar to the changes made to Project.search the method
Project.search_by_title now also uses Arel so it can automatically use
ILIKE/LIKE instead of the lower() function.
parent 135659a7
...@@ -298,7 +298,10 @@ class Project < ActiveRecord::Base ...@@ -298,7 +298,10 @@ class Project < ActiveRecord::Base
end end
def search_by_title(query) def search_by_title(query)
non_archived.where('LOWER(projects.name) LIKE :query', query: "%#{query.downcase}%") pattern = "%#{query}%"
table = Project.arel_table
non_archived.where(table[:name].matches(pattern))
end end
def find_with_namespace(id) def find_with_namespace(id)
......
...@@ -698,4 +698,20 @@ describe Project, models: true do ...@@ -698,4 +698,20 @@ describe Project, models: true do
project.expire_caches_before_rename('foo') project.expire_caches_before_rename('foo')
end end
end end
describe '.search_by_title' do
let(:project) { create(:project, name: 'kittens') }
it 'returns projects with a matching name' do
expect(described_class.search_by_title(project.name)).to eq([project])
end
it 'returns projects with a partially matching name' do
expect(described_class.search_by_title('kitten')).to eq([project])
end
it 'returns projects with a matching name regardless of the casing' do
expect(described_class.search_by_title('KITTENS')).to eq([project])
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