Module: SearchHelper

Included in:
SearchController
Defined in:
app/helpers/search_helper.rb

Constant Summary collapse

SEARCH_GENERIC_PARAMS =

params which should persist when a new tab is selected

[
  :search,
  :scope,
  :project_id,
  :group_id,
  :repository_ref,
  :snippets,
  :sort,
  :force_search_results,
  :type
].freeze

Instance Method Summary collapse

Instance Method Details

#blob_data_oversize_messageObject



267
268
269
# File 'app/helpers/search_helper.rb', line 267

def blob_data_oversize_message
  _('The file could not be displayed because it is empty.')
end

#generic_results(term) ⇒ Object



64
65
66
67
68
69
# File 'app/helpers/search_helper.rb', line 64

def generic_results(term)
  search_pattern = Regexp.new(Regexp.escape(term), "i")

  generic_results = combined_generic_results
  generic_results.select { |result| result[:label] =~ search_pattern }
end

#header_search_contextObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/helpers/search_helper.rb', line 214

def header_search_context
  {}.tap do |hash|
    if search_has_group?
      hash[:group] = { id: search_group.id, name: search_group.name, full_name: search_group.full_name }
      hash[:group_metadata] = {
        issues_path: issues_group_path(search_group),
        mr_path: merge_requests_group_path(search_group, assignee_username: current_user&.username)
      }
    end

    if search_has_project?
      hash[:project] = { id: @project.id, name: @project.name }
      hash[:project_metadata] = {}

      if @project.feature_available?(:merge_requests, current_user)
        mr_path = project_merge_requests_path(@project, assignee_username: current_user&.username)
        hash[:project_metadata][:mr_path] = mr_path
      end

      if @project.feature_available?(:issues, current_user)
        hash[:project_metadata][:issues_path] =
          project_issues_path(@project)
      end

      hash[:code_search] = search_scope.nil?
      hash[:ref] = @ref if @ref && can?(current_user, :read_code, @project)
    end

    hash[:scope] = search_scope if search_has_project? || search_has_group?
    hash[:for_snippets] = @snippet.present? || @snippets&.any?
  end
end

#recent_items_autocomplete(term) ⇒ Object



71
72
73
# File 'app/helpers/search_helper.rb', line 71

def recent_items_autocomplete(term)
  recent_merge_requests_autocomplete(term) + recent_issues_autocomplete(term)
end

#repository_ref(project) ⇒ Object



156
157
158
159
160
161
# File 'app/helpers/search_helper.rb', line 156

def repository_ref(project)
  return project.default_branch unless params[:project_id]

  # Always #to_s the repository_ref param in case the value is also a number
  params[:repository_ref].to_s.presence || project.default_branch
end

#resource_results(term, scope: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/search_helper.rb', line 39

def resource_results(term, scope: nil)
  return [] if term.length < Gitlab::Search::Params::MIN_TERM_LENGTH
  return scope_specific_results(term, scope) if scope.present?

  [
    groups_autocomplete(term),
    projects_autocomplete(term),
    users_autocomplete(term),
    issue_autocomplete(term)
  ].flatten
end

#scope_specific_results(term, scope) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/search_helper.rb', line 51

def scope_specific_results(term, scope)
  case scope&.to_sym
  when :projects
    projects_autocomplete(term)
  when :users
    users_autocomplete(term)
  when :issues
    recent_issues_autocomplete(term)
  else
    []
  end
end

#search_autocomplete_opts(term, filter: nil, scope: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/search_helper.rb', line 17

def search_autocomplete_opts(term, filter: nil, scope: nil)
  return unless current_user

  results = case filter&.to_sym
            when :search
              resource_results(term, scope: scope)
            when :generic
              [
                recent_items_autocomplete(term),
                generic_results(term)
              ]
            else
              [
                recent_items_autocomplete(term),
                resource_results(term),
                generic_results(term)
              ]
            end

  results.flatten { |item| item[:label] }
end

#search_entries_empty_message(scope, term, group, project) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/search_helper.rb', line 127

def search_entries_empty_message(scope, term, group, project)
  options = {
    scope: search_entries_scope_label(scope, 0),
    term: "<code>#{h(term)}</code>".html_safe
  }

  # We check project first because we have 3 possible combinations here:
  # - group && project
  # - group
  # - group: nil, project: nil
  if project
    ERB::Util.html_escape(
      _("We couldn't find any %{scope} matching %{term} in project %{project}")) % options.merge(
        project: link_to(
          project.full_name,
          project_path(project),
          target: '_blank',
          rel: 'noopener noreferrer'
        ).html_safe
      )
  elsif group
    ERB::Util.html_escape(_("We couldn't find any %{scope} matching %{term} in group %{group}")) % options.merge(
      group: link_to(group.full_name, group_path(group), target: '_blank', rel: 'noopener noreferrer').html_safe
    )
  else
    ERB::Util.html_escape(_("We couldn't find any %{scope} matching %{term}")) % options
  end
end

#search_entries_info(collection, scope, term) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/search_helper.rb', line 75

def search_entries_info(collection, scope, term)
  return if collection.to_a.empty?

  from = collection.offset_value + 1
  to = collection.offset_value + collection.to_a.size
  count = collection.total_count
  term_element = "<span>&nbsp;<code>#{h(term)}</code>&nbsp;</span>".html_safe

  search_entries_info_template(collection) % {
    from: from,
    to: to,
    count: count,
    scope: search_entries_scope_label(scope, count),
    term_element: term_element
  }
end

#search_entries_info_template(collection) ⇒ Object



119
120
121
122
123
124
125
# File 'app/helpers/search_helper.rb', line 119

def search_entries_info_template(collection)
  if collection.total_pages > 1
    s_("SearchResults|Showing %{from} - %{to} of %{count} %{scope} for %{term_element}").html_safe
  else
    s_("SearchResults|Showing %{count} %{scope} for %{term_element}").html_safe
  end
end

#search_entries_scope_label(scope, count) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/search_helper.rb', line 92

def search_entries_scope_label(scope, count)
  case scope
  when 'blobs'
    ns_('SearchResults|code result', 'SearchResults|code results', count)
  when 'commits'
    ns_('SearchResults|commit', 'SearchResults|commits', count)
  when 'issues'
    ns_('SearchResults|issue', 'SearchResults|issues', count)
  when 'merge_requests'
    ns_('SearchResults|merge request', 'SearchResults|merge requests', count)
  when 'milestones'
    ns_('SearchResults|milestone', 'SearchResults|milestones', count)
  when 'notes'
    ns_('SearchResults|comment', 'SearchResults|comments', count)
  when 'projects'
    ns_('SearchResults|project', 'SearchResults|projects', count)
  when 'snippet_titles'
    ns_('SearchResults|snippet', 'SearchResults|snippets', count)
  when 'users'
    ns_('SearchResults|user', 'SearchResults|users', count)
  when 'wiki_blobs'
    ns_('SearchResults|wiki result', 'SearchResults|wiki results', count)
  else
    raise "Unrecognized search scope '#{scope}'"
  end
end

#search_groupObject



201
202
203
204
# File 'app/helpers/search_helper.rb', line 201

def search_group
  # group gets derived from the Project in the project's scope
  @group || @project&.group
end

#search_has_group?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'app/helpers/search_helper.rb', line 206

def search_has_group?
  search_group.present? && search_group&.persisted?
end

#search_has_project?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'app/helpers/search_helper.rb', line 210

def search_has_project?
  @project.present? && @project&.persisted?
end

#search_navigation_jsonObject



271
272
273
274
275
276
277
278
279
280
281
# File 'app/helpers/search_helper.rb', line 271

def search_navigation_json
  search_navigation = Search::Navigation.new(
    user: current_user,
    project: @project,
    group: @group,
    options: nav_options
  )

  sorted_navigation = search_navigation.tabs.sort_by { |_, h| h[:sort] }
  parse_navigation(sorted_navigation).to_json
end

#search_scopeObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/helpers/search_helper.rb', line 247

def search_scope
  return if ::Gitlab::CurrentSettings.custom_default_search_scope_set?

  if current_controller?(:issues)
    'issues'
  elsif current_controller?(:merge_requests)
    'merge_requests'
  elsif current_controller?(:wikis)
    'wiki_blobs'
  elsif current_controller?(:commits)
    'commits'
  elsif current_controller?(:groups)
    controller.action_name if %w[issues merge_requests].include?(controller.action_name)
  end
end

#search_serviceObject



163
164
165
# File 'app/helpers/search_helper.rb', line 163

def search_service
  @search_service ||= ::SearchService.new(current_user, params)
end

#search_sort_optionsObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/helpers/search_helper.rb', line 167

def search_sort_options
  options = [
    {
      title: _('Created date'),
      sortable: true,
      sortParam: {
        asc: 'created_asc',
        desc: 'created_desc'
      }
    },
    {
      title: _('Updated date'),
      sortable: true,
      sortParam: {
        asc: 'updated_asc',
        desc: 'updated_desc'
      }
    }
  ]

  if search_service.scope == 'issues'
    options << {
      title: _('Popularity'),
      sortable: true,
      sortParam: {
        asc: 'popularity_asc',
        desc: 'popularity_desc'
      }
    }
  end

  options
end

#should_show_zoekt_results?(_scope, _search_type) ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
# File 'app/helpers/search_helper.rb', line 263

def should_show_zoekt_results?(_scope, _search_type)
  false
end