Commit 0f87ae3be5d7d1bc0a3728f43ae0465005b68b92
1 parent
cddec75d
Exists in
master
and in
4 other branches
Allow searching by namespace name, include public projects
GITLAB-1457 (GITLAB-836) Change-Id: If8e2474bc6908accca51f395f58937687f101b58
Showing
3 changed files
with
34 additions
and
5 deletions
Show diff stats
app/contexts/search_context.rb
| ... | ... | @@ -9,11 +9,14 @@ class SearchContext |
| 9 | 9 | query = params[:search] |
| 10 | 10 | |
| 11 | 11 | return result unless query.present? |
| 12 | - | |
| 13 | - projects = Project.where(id: project_ids) | |
| 14 | - result[:projects] = projects.search(query).limit(20) | |
| 12 | + result[:projects] = Project.where("projects.id in (?) OR projects.public = true", project_ids).search(query).limit(20) | |
| 15 | 13 | |
| 16 | 14 | # Search inside single project |
| 15 | + single_project_search(Project.where(id: project_ids), query) | |
| 16 | + result | |
| 17 | + end | |
| 18 | + | |
| 19 | + def single_project_search(projects, query) | |
| 17 | 20 | project = projects.first if projects.length == 1 |
| 18 | 21 | |
| 19 | 22 | if params[:search_code].present? |
| ... | ... | @@ -23,7 +26,6 @@ class SearchContext |
| 23 | 26 | result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) |
| 24 | 27 | result[:wiki_pages] = [] |
| 25 | 28 | end |
| 26 | - result | |
| 27 | 29 | end |
| 28 | 30 | |
| 29 | 31 | def result | ... | ... |
app/models/project.rb
| ... | ... | @@ -122,7 +122,7 @@ class Project < ActiveRecord::Base |
| 122 | 122 | end |
| 123 | 123 | |
| 124 | 124 | def search query |
| 125 | - where("projects.name LIKE :query OR projects.path LIKE :query", query: "%#{query}%") | |
| 125 | + joins(:namespace).where("projects.name LIKE :query OR projects.path LIKE :query OR namespaces.name LIKE :query", query: "%#{query}%") | |
| 126 | 126 | end |
| 127 | 127 | |
| 128 | 128 | def find_with_namespace(id) | ... | ... |
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe SearchContext do | |
| 4 | + let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } | |
| 5 | + let(:user) { create(:user, namespace: found_namespace) } | |
| 6 | + let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, public: false) } | |
| 7 | + | |
| 8 | + let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') } | |
| 9 | + let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, public: false) } | |
| 10 | + let(:public_namespace) { create(:namespace, path: 'something_else',name: 'searchable public namespace') } | |
| 11 | + let(:other_user) { create(:user, namespace: public_namespace) } | |
| 12 | + let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: other_user.id, namespace: public_namespace, public: true) } | |
| 13 | + | |
| 14 | + describe '#execute' do | |
| 15 | + it 'public projects should be searchable' do | |
| 16 | + context = SearchContext.new([found_project.id], {search_code: false, search: "searchable"}) | |
| 17 | + results = context.execute | |
| 18 | + results[:projects].should == [found_project, public_project] | |
| 19 | + end | |
| 20 | + | |
| 21 | + it 'namespace name should be searchable' do | |
| 22 | + context = SearchContext.new([found_project.id], {search_code: false, search: "searchable namespace"}) | |
| 23 | + results = context.execute | |
| 24 | + results[:projects].should == [found_project] | |
| 25 | + end | |
| 26 | + end | |
| 27 | +end | ... | ... |