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

Make Namespace.search case-insensitive

This ensures searching namespaces works exactly the same as searching
for any other resource.
parent d7d59375
...@@ -52,8 +52,18 @@ class Namespace < ActiveRecord::Base ...@@ -52,8 +52,18 @@ class Namespace < ActiveRecord::Base
find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
end end
# Searches for namespaces 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("name LIKE :query OR path LIKE :query", query: "%#{query}%") t = arel_table
pattern = "%#{query}%"
where(t[:name].matches(pattern).or(t[:path].matches(pattern)))
end end
def clean_path(path) def clean_path(path)
......
...@@ -41,13 +41,32 @@ describe Namespace, models: true do ...@@ -41,13 +41,32 @@ describe Namespace, models: true do
it { expect(namespace.human_name).to eq(namespace.owner_name) } it { expect(namespace.human_name).to eq(namespace.owner_name) }
end end
describe :search do describe '#search' do
before do let(:namespace) { create(:namespace) }
@namespace = create :namespace
it 'returns namespaces with a matching name' do
expect(described_class.search(namespace.name)).to eq([namespace])
end
it 'returns namespaces with a partially matching name' do
expect(described_class.search(namespace.name[0..2])).to eq([namespace])
end
it 'returns namespaces with a matching name regardless of the casing' do
expect(described_class.search(namespace.name.upcase)).to eq([namespace])
end
it 'returns namespaces with a matching path' do
expect(described_class.search(namespace.path)).to eq([namespace])
end end
it { expect(Namespace.search(@namespace.path)).to eq([@namespace]) } it 'returns namespaces with a partially matching path' do
it { expect(Namespace.search('unknown')).to eq([]) } expect(described_class.search(namespace.path[0..2])).to eq([namespace])
end
it 'returns namespaces with a matching path regardless of the casing' do
expect(described_class.search(namespace.path.upcase)).to eq([namespace])
end
end end
describe :move_dir do describe :move_dir 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