Use ILIKE/LIKE for searching snippets
Previously this used a regular LIKE which is case-sensitive on PostgreSQL. This ensures that for both PostgreSQL and MySQL the searching is case-insensitive similar to searching for projects.
This commit is contained in:
parent
db615d0a79
commit
1f5284e5dd
|
@ -113,12 +113,32 @@ class Snippet < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
# Searches for snippets with a matching title or file name.
|
||||||
|
#
|
||||||
|
# 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('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
|
t = Snippet.arel_table
|
||||||
|
pattern = "%#{query}%"
|
||||||
|
|
||||||
|
where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Searches for snippets with matching content.
|
||||||
|
#
|
||||||
|
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
|
||||||
|
#
|
||||||
|
# query - The search query as a String.
|
||||||
|
#
|
||||||
|
# Returns an ActiveRecord::Relation.
|
||||||
def search_code(query)
|
def search_code(query)
|
||||||
where('(content LIKE :query)', query: "%#{query}%")
|
table = Snippet.arel_table
|
||||||
|
pattern = "%#{query}%"
|
||||||
|
|
||||||
|
where(table[:content].matches(pattern))
|
||||||
end
|
end
|
||||||
|
|
||||||
def accessible_to(user)
|
def accessible_to(user)
|
||||||
|
|
|
@ -59,4 +59,48 @@ describe Snippet, models: true do
|
||||||
expect(snippet.to_reference(cross)).to eq "#{project.to_reference}$#{snippet.id}"
|
expect(snippet.to_reference(cross)).to eq "#{project.to_reference}$#{snippet.id}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.search' do
|
||||||
|
let(:snippet) { create(:snippet) }
|
||||||
|
|
||||||
|
it 'returns snippets with a matching title' do
|
||||||
|
expect(described_class.search(snippet.title)).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with a partially matching title' do
|
||||||
|
expect(described_class.search(snippet.title[0..2])).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with a matching title regardless of the casing' do
|
||||||
|
expect(described_class.search(snippet.title.upcase)).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with a matching file name' do
|
||||||
|
expect(described_class.search(snippet.file_name)).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with a partially matching file name' do
|
||||||
|
expect(described_class.search(snippet.file_name[0..2])).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with a matching file name regardless of the casing' do
|
||||||
|
expect(described_class.search(snippet.file_name.upcase)).to eq([snippet])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#search_code' do
|
||||||
|
let(:snippet) { create(:snippet, content: 'class Foo; end') }
|
||||||
|
|
||||||
|
it 'returns snippets with matching content' do
|
||||||
|
expect(described_class.search_code(snippet.content)).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with partially matching content' do
|
||||||
|
expect(described_class.search_code('class')).to eq([snippet])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns snippets with matching content regardless of the casing' do
|
||||||
|
expect(described_class.search_code('FOO')).to eq([snippet])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue