Class: Gitlab::ProjectSearchResults

Inherits:
SearchResults show all
Defined in:
lib/gitlab/project_search_results.rb

Constant Summary

Constants inherited from SearchResults

SearchResults::COUNT_LIMIT, SearchResults::COUNT_LIMIT_MESSAGE, SearchResults::DEFAULT_PAGE, SearchResults::DEFAULT_PER_PAGE, SearchResults::SCOPE_ONLY_SORT

Instance Attribute Summary collapse

Attributes inherited from SearchResults

#current_user, #default_project_filter, #filters, #limit_projects, #order_by, #query, #sort

Instance Method Summary collapse

Methods inherited from SearchResults

#aggregations, #count_limit, #error, #failed?, #formatted_limited_count, #highlight_map, #limited_issues_count, #limited_merge_requests_count, #limited_milestones_count, #limited_projects_count, #limited_users_count

Constructor Details

#initialize(current_user, query, project:, repository_ref: nil, order_by: nil, sort: nil, filters: {}) ⇒ ProjectSearchResults

Returns a new instance of ProjectSearchResults.



7
8
9
10
11
12
13
# File 'lib/gitlab/project_search_results.rb', line 7

def initialize(current_user, query, project:, repository_ref: nil, order_by: nil, sort: nil, filters: {})
  @project = project
  @repository_ref = repository_ref.presence

  # use the default filter for project searches since we are already limiting by a single project
  super(current_user, query, [project], order_by: order_by, sort: sort, filters: filters, default_project_filter: true)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/gitlab/project_search_results.rb', line 5

def project
  @project
end

#repository_refObject (readonly)

Returns the value of attribute repository_ref.



5
6
7
# File 'lib/gitlab/project_search_results.rb', line 5

def repository_ref
  @repository_ref
end

Instance Method Details

#commits_countObject



87
88
89
# File 'lib/gitlab/project_search_results.rb', line 87

def commits_count
  @commits_count ||= commits(limit: count_limit).count
end

#formatted_count(scope) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab/project_search_results.rb', line 32

def formatted_count(scope)
  case scope
  when 'blobs'
    formatted_limited_count(limited_blobs_count)
  when 'notes'
    formatted_limited_count(limited_notes_count)
  when 'wiki_blobs'
    wiki_blobs_count.to_s
  when 'commits'
    formatted_limited_count(commits_count)
  else
    super
  end
end

#limited_blobs_countObject

rubocop:enable CodeReuse/ActiveRecord



63
64
65
# File 'lib/gitlab/project_search_results.rb', line 63

def limited_blobs_count
  @limited_blobs_count ||= blobs(limit: count_limit).count
end

#limited_notes_countObject

rubocop: disable CodeReuse/ActiveRecord



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gitlab/project_search_results.rb', line 68

def limited_notes_count
  return @limited_notes_count if defined?(@limited_notes_count)

  types = %w[issue merge_request commit snippet]
  @limited_notes_count = 0

  types.each do |type|
    @limited_notes_count += notes_finder(type).limit(count_limit).count
    break if @limited_notes_count >= count_limit
  end

  @limited_notes_count
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/project_search_results.rb', line 15

def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, preload_method: nil)
  case scope
  when 'notes'
    notes.page(page).per(per_page)
  when 'blobs'
    paginated_blobs(blobs(limit: limit_up_to_page(page, per_page)), page, per_page)
  when 'wiki_blobs'
    paginated_wiki_blobs(wiki_blobs(limit: limit_up_to_page(page, per_page)), page, per_page)
  when 'commits'
    paginated_commits(page, per_page)
  when 'users'
    users.page(page).per(per_page)
  else
    super(scope, page: page, per_page: per_page, without_count: true)
  end
end

#usersObject

rubocop:disable CodeReuse/ActiveRecord



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/project_search_results.rb', line 48

def users
  results = super

  if @project.is_a?(Array)
    team_members_for_projects = User.joins(:project_authorizations).where(project_authorizations: { project_id: @project })
      .allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/422045')
    results = results.where(id: team_members_for_projects)
  else
    results = results.where(id: @project.team.members)
  end

  results
end

#wiki_blobs_countObject

rubocop: enable CodeReuse/ActiveRecord



83
84
85
# File 'lib/gitlab/project_search_results.rb', line 83

def wiki_blobs_count
  @wiki_blobs_count ||= wiki_blobs(limit: count_limit).count
end