Commit 4791084d7c9356c16ead5bf3162fd132fa10c979
1 parent
cb589d23
Exists in
spb-stable
and in
3 other branches
Search::GlobalContext and Search::ProjectContext for separation between 2 types of search
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
82 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
1 | +module Search | ||
2 | + class GlobalContext | ||
3 | + attr_accessor :current_user, :params | ||
4 | + | ||
5 | + def initialize(user, params) | ||
6 | + @current_user, @params = user, params.dup | ||
7 | + end | ||
8 | + | ||
9 | + def execute | ||
10 | + query = params[:search] | ||
11 | + query = Shellwords.shellescape(query) if query.present? | ||
12 | + return result unless query.present? | ||
13 | + | ||
14 | + | ||
15 | + projects = current_user.authorized_projects | ||
16 | + | ||
17 | + if params[:group_id].present? | ||
18 | + group = Group.find_by_id(params[:group_id]) | ||
19 | + projects = projects.where(namespace_id: group.id) if group | ||
20 | + end | ||
21 | + | ||
22 | + project_ids = projects.pluck(:id) | ||
23 | + | ||
24 | + visibility_levels = if current_user | ||
25 | + [Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC] | ||
26 | + else | ||
27 | + [Gitlab::VisibilityLevel::PUBLIC] | ||
28 | + end | ||
29 | + result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20) | ||
30 | + result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20) | ||
31 | + result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) | ||
32 | + result[:total_results] = %w(projects issues merge_requests).sum { |items| result[items.to_sym].size } | ||
33 | + result | ||
34 | + end | ||
35 | + | ||
36 | + def result | ||
37 | + @result ||= { | ||
38 | + projects: [], | ||
39 | + merge_requests: [], | ||
40 | + issues: [], | ||
41 | + total_results: 0, | ||
42 | + } | ||
43 | + end | ||
44 | + end | ||
45 | +end |
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +module Search | ||
2 | + class ProjectContext | ||
3 | + attr_accessor :project, :current_user, :params | ||
4 | + | ||
5 | + def initialize(project, user, params) | ||
6 | + @project, @current_user, @params = project, user, params.dup | ||
7 | + end | ||
8 | + | ||
9 | + def execute | ||
10 | + query = params[:search] | ||
11 | + query = Shellwords.shellescape(query) if query.present? | ||
12 | + return result unless query.present? | ||
13 | + | ||
14 | + if params[:search_code].present? | ||
15 | + blobs = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo? | ||
16 | + blobs = Kaminari.paginate_array(blobs).page(params[:page]).per(20) | ||
17 | + result[:blobs] = blobs | ||
18 | + result[:total_results] = blobs.total_count | ||
19 | + else | ||
20 | + result[:merge_requests] = project.merge_requests.search(query).order('updated_at DESC').limit(20) | ||
21 | + result[:issues] = project.issues.search(query).order('updated_at DESC').limit(20) | ||
22 | + result[:total_results] = %w(issues merge_requests).sum { |items| result[items.to_sym].size } | ||
23 | + end | ||
24 | + | ||
25 | + result | ||
26 | + end | ||
27 | + | ||
28 | + def result | ||
29 | + @result ||= { | ||
30 | + merge_requests: [], | ||
31 | + issues: [], | ||
32 | + blobs: [], | ||
33 | + total_results: 0, | ||
34 | + } | ||
35 | + end | ||
36 | + end | ||
37 | +end |