Class: Gitlab::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/search_results.rb

Constant Summary collapse

COUNT_LIMIT =
100
COUNT_LIMIT_MESSAGE =
"#{COUNT_LIMIT - 1}+"
DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user, query, limit_projects = nil, default_project_filter: false, filters: {}) ⇒ SearchResults

Returns a new instance of SearchResults.


22
23
24
25
26
27
28
# File 'lib/gitlab/search_results.rb', line 22

def initialize(current_user, query, limit_projects = nil, default_project_filter: false, filters: {})
  @current_user = current_user
  @query = query
  @limit_projects = limit_projects || Project.all
  @default_project_filter = default_project_filter
  @filters = filters
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user


10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def current_user
  @current_user
end

#default_project_filterObject (readonly)

Whether a custom filter is used to restrict scope of projects. If the default filter (which lists all projects user has access to) is used, we can skip it when filtering merge requests and optimize the query


20
21
22
# File 'lib/gitlab/search_results.rb', line 20

def default_project_filter
  @default_project_filter
end

#filtersObject (readonly)

Returns the value of attribute filters


10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def filters
  @filters
end

#limit_projectsObject (readonly)

Limit search results by passed projects It allows us to search only for projects user has access to


14
15
16
# File 'lib/gitlab/search_results.rb', line 14

def limit_projects
  @limit_projects
end

#queryObject (readonly)

Returns the value of attribute query


10
11
12
# File 'lib/gitlab/search_results.rb', line 10

def query
  @query
end

Instance Method Details

#count_limitObject


109
110
111
# File 'lib/gitlab/search_results.rb', line 109

def count_limit
  COUNT_LIMIT
end

#formatted_count(scope) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitlab/search_results.rb', line 54

def formatted_count(scope)
  case scope
  when 'projects'
    formatted_limited_count(limited_projects_count)
  when 'issues'
    formatted_limited_count(limited_issues_count)
  when 'merge_requests'
    formatted_limited_count(limited_merge_requests_count)
  when 'milestones'
    formatted_limited_count(limited_milestones_count)
  when 'users'
    formatted_limited_count(limited_users_count)
  end
end

#formatted_limited_count(count) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/gitlab/search_results.rb', line 69

def formatted_limited_count(count)
  if count >= COUNT_LIMIT
    COUNT_LIMIT_MESSAGE
  else
    count.to_s
  end
end

#limited_issues_countObject


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gitlab/search_results.rb', line 81

def limited_issues_count
  return @limited_issues_count if @limited_issues_count

  # By default getting limited count (e.g. 1000+) is fast on issuable
  # collections except for issues, where filtering both not confidential
  # and confidential issues user has access to, is too complex.
  # It's faster to try to fetch all public issues first, then only
  # if necessary try to fetch all issues.
  sum = limited_count(issues(public_only: true))
  @limited_issues_count = sum < count_limit ? limited_count(issues) : sum
end

#limited_merge_requests_countObject


93
94
95
# File 'lib/gitlab/search_results.rb', line 93

def limited_merge_requests_count
  @limited_merge_requests_count ||= limited_count(merge_requests)
end

#limited_milestones_countObject


97
98
99
# File 'lib/gitlab/search_results.rb', line 97

def limited_milestones_count
  @limited_milestones_count ||= limited_count(milestones)
end

#limited_projects_countObject


77
78
79
# File 'lib/gitlab/search_results.rb', line 77

def limited_projects_count
  @limited_projects_count ||= limited_count(projects)
end

#limited_users_countObject


101
102
103
# File 'lib/gitlab/search_results.rb', line 101

def limited_users_count
  @limited_users_count ||= limited_count(users)
end

#objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/search_results.rb', line 30

def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil)
  should_preload = preload_method.present?
  collection = case scope
               when 'projects'
                 projects
               when 'issues'
                 issues
               when 'merge_requests'
                 merge_requests
               when 'milestones'
                 milestones
               when 'users'
                 users
               else
                 should_preload = false
                 Kaminari.paginate_array([])
               end

  collection = collection.public_send(preload_method) if should_preload # rubocop:disable GitlabSecurity/PublicSend
  collection = collection.page(page).per(per_page)

  without_count ? collection.without_count : collection
end

#single_commit_result?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'lib/gitlab/search_results.rb', line 105

def single_commit_result?
  false
end

#usersObject


113
114
115
116
117
# File 'lib/gitlab/search_results.rb', line 113

def users
  return User.none unless Ability.allowed?(current_user, :read_users_list)

  UsersFinder.new(current_user, search: query).execute
end