Commit a27e4404 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #5435 from karlhungus/feature_search_namespace_name_and_public

Allow searching by namespace name, include public projects
parents 9ab4539a 0f87ae3b
......@@ -10,11 +10,14 @@ class SearchContext
query = Shellwords.shellescape(query) if query.present?
return result unless query.present?
projects = Project.where(id: project_ids)
result[:projects] = projects.search(query).limit(20)
result[:projects] = Project.where("projects.id in (?) OR projects.public = true", project_ids).search(query).limit(20)
# Search inside single project
single_project_search(Project.where(id: project_ids), query)
result
end
def single_project_search(projects, query)
project = projects.first if projects.length == 1
if params[:search_code].present?
......@@ -24,7 +27,6 @@ class SearchContext
result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20)
result[:wiki_pages] = []
end
result
end
def result
......
......@@ -122,7 +122,7 @@ class Project < ActiveRecord::Base
end
def search query
where("projects.name LIKE :query OR projects.path LIKE :query", query: "%#{query}%")
joins(:namespace).where("projects.name LIKE :query OR projects.path LIKE :query OR namespaces.name LIKE :query", query: "%#{query}%")
end
def find_with_namespace(id)
......
require 'spec_helper'
describe SearchContext do
let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') }
let(:user) { create(:user, namespace: found_namespace) }
let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, public: false) }
let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') }
let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, public: false) }
let(:public_namespace) { create(:namespace, path: 'something_else',name: 'searchable public namespace') }
let(:other_user) { create(:user, namespace: public_namespace) }
let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: other_user.id, namespace: public_namespace, public: true) }
describe '#execute' do
it 'public projects should be searchable' do
context = SearchContext.new([found_project.id], {search_code: false, search: "searchable"})
results = context.execute
results[:projects].should == [found_project, public_project]
end
it 'namespace name should be searchable' do
context = SearchContext.new([found_project.id], {search_code: false, search: "searchable namespace"})
results = context.execute
results[:projects].should == [found_project]
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