Commit b49ebf5737ce95bcab792c18bddc1a4675f148ed
1 parent
4791084d
Exists in
spb-stable
and in
3 other branches
Use new context for search
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
7 changed files
with
20 additions
and
85 deletions
Show diff stats
app/assets/stylesheets/generic/nav.scss
app/contexts/search_context.rb
... | ... | @@ -1,42 +0,0 @@ |
1 | -class SearchContext | |
2 | - attr_accessor :project_ids, :current_user, :params | |
3 | - | |
4 | - def initialize(project_ids, user, params) | |
5 | - @project_ids, @current_user, @params = project_ids, user, params.dup | |
6 | - end | |
7 | - | |
8 | - def execute | |
9 | - query = params[:search] | |
10 | - query = Shellwords.shellescape(query) if query.present? | |
11 | - | |
12 | - return result unless query.present? | |
13 | - visibility_levels = @current_user ? [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] : [ Gitlab::VisibilityLevel::PUBLIC ] | |
14 | - result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20) | |
15 | - | |
16 | - # Search inside single project | |
17 | - single_project_search(Project.where(id: project_ids), query) | |
18 | - result | |
19 | - end | |
20 | - | |
21 | - def single_project_search(projects, query) | |
22 | - project = projects.first if projects.length == 1 | |
23 | - | |
24 | - if params[:search_code].present? | |
25 | - result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo? | |
26 | - else | |
27 | - result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20) | |
28 | - result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) | |
29 | - result[:wiki_pages] = [] | |
30 | - end | |
31 | - end | |
32 | - | |
33 | - def result | |
34 | - @result ||= { | |
35 | - projects: [], | |
36 | - merge_requests: [], | |
37 | - issues: [], | |
38 | - wiki_pages: [], | |
39 | - blobs: [] | |
40 | - } | |
41 | - end | |
42 | -end |
app/controllers/search_controller.rb
1 | 1 | class SearchController < ApplicationController |
2 | 2 | def show |
3 | - project_id = params[:project_id] | |
4 | - group_id = params[:group_id] | |
5 | - | |
6 | - project_ids = find_project_ids(group_id, project_id) | |
7 | - | |
8 | - result = SearchContext.new(project_ids, current_user, params).execute | |
9 | - | |
10 | - @projects = result[:projects] | |
11 | - @merge_requests = result[:merge_requests] | |
12 | - @issues = result[:issues] | |
13 | - @wiki_pages = result[:wiki_pages] | |
14 | - @blobs = Kaminari.paginate_array(result[:blobs]).page(params[:page]).per(20) | |
15 | - @total_results = @projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.total_count | |
16 | - end | |
17 | - | |
18 | - private | |
19 | - | |
20 | - def find_project_ids(group_id, project_id) | |
21 | - project_ids = current_user.authorized_projects.map(&:id) | |
22 | - | |
23 | - if group_id.present? | |
24 | - @group = Group.find(group_id) | |
25 | - group_project_ids = @group.projects.map(&:id) | |
26 | - project_ids.select! { |id| group_project_ids.include?(id) } | |
27 | - elsif project_id.present? | |
28 | - @project = Project.find(project_id) | |
29 | - project_ids = @project.public? ? [@project.id] : project_ids.select { |id| id == project_id.to_i } | |
3 | + @project = Project.find_by_id(params[:project_id]) if params[:project_id].present? | |
4 | + @group = Group.find_by_id(params[:group_id]) if params[:group_id].present? | |
5 | + | |
6 | + if @project | |
7 | + return access_denied! unless can?(current_user, :download_code, @project) | |
8 | + @search_results = Search::ProjectContext.new(@project, current_user, params).execute | |
9 | + else | |
10 | + @search_results = Search::GlobalContext.new(current_user, params).execute | |
30 | 11 | end |
31 | - project_ids | |
32 | 12 | end |
33 | 13 | end | ... | ... |
app/views/search/_filter.html.haml
... | ... | @@ -9,7 +9,7 @@ |
9 | 9 | %b.caret |
10 | 10 | %ul.dropdown-menu |
11 | 11 | %li |
12 | - = link_to search_path(group_id: nil) do | |
12 | + = link_to search_path(group_id: nil, search: params[:search]) do | |
13 | 13 | Any |
14 | 14 | - current_user.authorized_groups.sort_by(&:name).each do |group| |
15 | 15 | %li |
... | ... | @@ -27,7 +27,7 @@ |
27 | 27 | %b.caret |
28 | 28 | %ul.dropdown-menu |
29 | 29 | %li |
30 | - = link_to search_path(project_id: nil) do | |
30 | + = link_to search_path(project_id: nil, search: params[:search]) do | |
31 | 31 | Any |
32 | 32 | - current_user.authorized_projects.sort_by(&:name_with_namespace).each do |project| |
33 | 33 | %li | ... | ... |
app/views/search/_global_results.html.haml
1 | 1 | .search_results |
2 | 2 | %ul.bordered-list |
3 | - = render partial: "search/results/project", collection: @projects | |
4 | - = render partial: "search/results/merge_request", collection: @merge_requests | |
5 | - = render partial: "search/results/issue", collection: @issues | |
3 | + = render partial: "search/results/project", collection: @search_results[:projects] | |
4 | + = render partial: "search/results/merge_request", collection: @search_results[:merge_requests] | |
5 | + = render partial: "search/results/issue", collection: @search_results[:issues] | ... | ... |
app/views/search/_project_results.html.haml
1 | -%ul.nav.nav-pills | |
1 | +%ul.nav.nav-pills.append-bottom-20 | |
2 | 2 | %li{class: ("active" if params[:search_code].present?)} |
3 | 3 | = link_to search_path(params.merge(search_code: true)) do |
4 | 4 | Repository Code |
... | ... | @@ -9,9 +9,9 @@ |
9 | 9 | .search_results |
10 | 10 | - if params[:search_code].present? |
11 | 11 | .blob-results |
12 | - = render partial: "search/results/blob", collection: @blobs | |
13 | - = paginate @blobs, theme: 'gitlab' | |
12 | + = render partial: "search/results/blob", collection: @search_results[:blobs] | |
13 | + = paginate @search_results[:blobs], theme: 'gitlab' | |
14 | 14 | - else |
15 | 15 | %ul.bordered-list |
16 | - = render partial: "search/results/merge_request", collection: @merge_requests | |
17 | - = render partial: "search/results/issue", collection: @issues | |
16 | + = render partial: "search/results/merge_request", collection: @search_results[:merge_requests] | |
17 | + = render partial: "search/results/issue", collection: @search_results[:issues] | ... | ... |