diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-03-01 17:05:26 +0100 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-03-11 15:25:22 -0500 |
commit | 2cf7f3f410832560bf049b24fbcaa27c1e3f30c7 (patch) | |
tree | 39e97a7c0d99ac97b98ed28d11a12dae11c109d5 | |
parent | 87e7c3e1321ac5ae26d6a23d7b16e8dadaff98d2 (diff) | |
download | gitlab-ce-2cf7f3f410832560bf049b24fbcaa27c1e3f30c7.tar.gz |
Use ILIKE/LIKE for searching milestones
-rw-r--r-- | app/models/milestone.rb | 13 | ||||
-rw-r--r-- | spec/models/milestone_spec.rb | 30 |
2 files changed, 41 insertions, 2 deletions
diff --git a/app/models/milestone.rb b/app/models/milestone.rb index e3969f32dd6..e3b6c552f92 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -58,9 +58,18 @@ class Milestone < ActiveRecord::Base alias_attribute :name, :title class << self + # Searches for milestones 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) - query = "%#{query}%" - where("title like ? or description like ?", query, query) + t = arel_table + pattern = "%#{query}%" + + where(t[:title].matches(pattern).or(t[:description].matches(pattern))) end end diff --git a/spec/models/milestone_spec.rb b/spec/models/milestone_spec.rb index 28f13100d15..de1757bf67a 100644 --- a/spec/models/milestone_spec.rb +++ b/spec/models/milestone_spec.rb @@ -181,4 +181,34 @@ describe Milestone, models: true do expect(issue4.position).to eq(42) end end + + describe '.search' do + let(:milestone) { create(:milestone, title: 'foo', description: 'bar') } + + it 'returns milestones with a matching title' do + expect(described_class.search(milestone.title)).to eq([milestone]) + end + + it 'returns milestones with a partially matching title' do + expect(described_class.search(milestone.title[0..2])).to eq([milestone]) + end + + it 'returns milestones with a matching title regardless of the casing' do + expect(described_class.search(milestone.title.upcase)).to eq([milestone]) + end + + it 'returns milestones with a matching description' do + expect(described_class.search(milestone.description)).to eq([milestone]) + end + + it 'returns milestones with a partially matching description' do + expect(described_class.search(milestone.description[0..2])). + to eq([milestone]) + end + + it 'returns milestones with a matching description regardless of the casing' do + expect(described_class.search(milestone.description.upcase)). + to eq([milestone]) + end + end end |