Commit b49ebf57 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Use new context for search

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 4791084d
......@@ -7,12 +7,9 @@
background: $primary_color;
}
> li > a {
@include border-radius(0);
}
&.nav-stacked {
> li > a {
@include border-radius(0);
border-left: 4px solid #EEE;
padding: 12px;
color: #777;
......
class SearchContext
attr_accessor :project_ids, :current_user, :params
def initialize(project_ids, user, params)
@project_ids, @current_user, @params = project_ids, user, params.dup
end
def execute
query = params[:search]
query = Shellwords.shellescape(query) if query.present?
return result unless query.present?
visibility_levels = @current_user ? [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] : [ Gitlab::VisibilityLevel::PUBLIC ]
result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).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?
result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
else
result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20)
result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20)
result[:wiki_pages] = []
end
end
def result
@result ||= {
projects: [],
merge_requests: [],
issues: [],
wiki_pages: [],
blobs: []
}
end
end
class SearchController < ApplicationController
def show
project_id = params[:project_id]
group_id = params[:group_id]
project_ids = find_project_ids(group_id, project_id)
result = SearchContext.new(project_ids, current_user, params).execute
@projects = result[:projects]
@merge_requests = result[:merge_requests]
@issues = result[:issues]
@wiki_pages = result[:wiki_pages]
@blobs = Kaminari.paginate_array(result[:blobs]).page(params[:page]).per(20)
@total_results = @projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.total_count
end
private
def find_project_ids(group_id, project_id)
project_ids = current_user.authorized_projects.map(&:id)
if group_id.present?
@group = Group.find(group_id)
group_project_ids = @group.projects.map(&:id)
project_ids.select! { |id| group_project_ids.include?(id) }
elsif project_id.present?
@project = Project.find(project_id)
project_ids = @project.public? ? [@project.id] : project_ids.select { |id| id == project_id.to_i }
@project = Project.find_by_id(params[:project_id]) if params[:project_id].present?
@group = Group.find_by_id(params[:group_id]) if params[:group_id].present?
if @project
return access_denied! unless can?(current_user, :download_code, @project)
@search_results = Search::ProjectContext.new(@project, current_user, params).execute
else
@search_results = Search::GlobalContext.new(current_user, params).execute
end
project_ids
end
end
......@@ -9,7 +9,7 @@
%b.caret
%ul.dropdown-menu
%li
= link_to search_path(group_id: nil) do
= link_to search_path(group_id: nil, search: params[:search]) do
Any
- current_user.authorized_groups.sort_by(&:name).each do |group|
%li
......@@ -27,7 +27,7 @@
%b.caret
%ul.dropdown-menu
%li
= link_to search_path(project_id: nil) do
= link_to search_path(project_id: nil, search: params[:search]) do
Any
- current_user.authorized_projects.sort_by(&:name_with_namespace).each do |project|
%li
......
.search_results
%ul.bordered-list
= render partial: "search/results/project", collection: @projects
= render partial: "search/results/merge_request", collection: @merge_requests
= render partial: "search/results/issue", collection: @issues
= render partial: "search/results/project", collection: @search_results[:projects]
= render partial: "search/results/merge_request", collection: @search_results[:merge_requests]
= render partial: "search/results/issue", collection: @search_results[:issues]
%ul.nav.nav-pills
%ul.nav.nav-pills.append-bottom-20
%li{class: ("active" if params[:search_code].present?)}
= link_to search_path(params.merge(search_code: true)) do
Repository Code
......@@ -9,9 +9,9 @@
.search_results
- if params[:search_code].present?
.blob-results
= render partial: "search/results/blob", collection: @blobs
= paginate @blobs, theme: 'gitlab'
= render partial: "search/results/blob", collection: @search_results[:blobs]
= paginate @search_results[:blobs], theme: 'gitlab'
- else
%ul.bordered-list
= render partial: "search/results/merge_request", collection: @merge_requests
= render partial: "search/results/issue", collection: @issues
= render partial: "search/results/merge_request", collection: @search_results[:merge_requests]
= render partial: "search/results/issue", collection: @search_results[:issues]
%fieldset
%legend
Search results
%span.cgray (#{@total_results})
%span.cgray (#{@search_results[:total_results]})
- if @project
= render "project_results"
......
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