Commit a27e4404dbb553b3916c9f3c3202f41fb85cdffb

Authored by Dmitriy Zaporozhets
2 parents 9ab4539a 0f87ae3b

Merge pull request #5435 from karlhungus/feature_search_namespace_name_and_public

Allow searching by namespace name, include public projects
app/contexts/search_context.rb
... ... @@ -10,11 +10,14 @@ class SearchContext
10 10 query = Shellwords.shellescape(query) if query.present?
11 11  
12 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 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 21 project = projects.first if projects.length == 1
19 22  
20 23 if params[:search_code].present?
... ... @@ -24,7 +27,6 @@ class SearchContext
24 27 result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20)
25 28 result[:wiki_pages] = []
26 29 end
27   - result
28 30 end
29 31  
30 32 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)
... ...
spec/contexts/search_context_spec.rb 0 → 100644
... ... @@ -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
... ...