Commit a27e4404dbb553b3916c9f3c3202f41fb85cdffb
Exists in
master
and in
4 other branches
Merge pull request #5435 from karlhungus/feature_search_namespace_name_and_public
Allow searching by namespace name, include public projects
Showing
3 changed files
with
34 additions
and
5 deletions
Show diff stats
app/contexts/search_context.rb
@@ -10,11 +10,14 @@ class SearchContext | @@ -10,11 +10,14 @@ class SearchContext | ||
10 | query = Shellwords.shellescape(query) if query.present? | 10 | query = Shellwords.shellescape(query) if query.present? |
11 | 11 | ||
12 | return result unless query.present? | 12 | return result unless query.present? |
13 | - | ||
14 | - projects = Project.where(id: project_ids) | ||
15 | - result[:projects] = projects.search(query).limit(20) | 13 | + result[:projects] = Project.where("projects.id in (?) OR projects.public = true", project_ids).search(query).limit(20) |
16 | 14 | ||
17 | # Search inside single project | 15 | # Search inside single project |
16 | + single_project_search(Project.where(id: project_ids), query) | ||
17 | + result | ||
18 | + end | ||
19 | + | ||
20 | + def single_project_search(projects, query) | ||
18 | project = projects.first if projects.length == 1 | 21 | project = projects.first if projects.length == 1 |
19 | 22 | ||
20 | if params[:search_code].present? | 23 | if params[:search_code].present? |
@@ -24,7 +27,6 @@ class SearchContext | @@ -24,7 +27,6 @@ class SearchContext | ||
24 | result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) | 27 | result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) |
25 | result[:wiki_pages] = [] | 28 | result[:wiki_pages] = [] |
26 | end | 29 | end |
27 | - result | ||
28 | end | 30 | end |
29 | 31 | ||
30 | def result | 32 | def result |
app/models/project.rb
@@ -122,7 +122,7 @@ class Project < ActiveRecord::Base | @@ -122,7 +122,7 @@ class Project < ActiveRecord::Base | ||
122 | end | 122 | end |
123 | 123 | ||
124 | def search query | 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 | end | 126 | end |
127 | 127 | ||
128 | def find_with_namespace(id) | 128 | def find_with_namespace(id) |
@@ -0,0 +1,27 @@ | @@ -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 |