Module: SearchHelper

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

Constant Summary collapse

SEARCH_PERMITTED_PARAMS =
[:search, :scope, :project_id, :group_id, :repository_ref, :snippets, :state].freeze

Instance Method Summary collapse

Instance Method Details

#search_autocomplete_opts(term) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/search_helper.rb', line 6

def search_autocomplete_opts(term)
  return unless current_user

  resources_results = [
    recent_issues_autocomplete(term),
    groups_autocomplete(term),
    projects_autocomplete(term)
  ].flatten

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

  generic_results = project_autocomplete + default_autocomplete + help_autocomplete
  generic_results.concat(default_autocomplete_admin) if current_user.admin?
  generic_results.select! { |result| result[:label] =~ search_pattern }

  [
    resources_results,
    generic_results
  ].flatten.uniq do |item|
    item[:label]
  end
end

#search_blob_title(project, path) ⇒ Object

Overridden in EE


89
90
91
# File 'app/helpers/search_helper.rb', line 89

def search_blob_title(project, path)
  path
end

#search_entries_empty_message(scope, term) ⇒ Object


81
82
83
84
85
86
# File 'app/helpers/search_helper.rb', line 81

def search_entries_empty_message(scope, term)
  (s_("SearchResults|We couldn't find any %{scope} matching %{term}") % {
    scope: search_entries_scope_label(scope, 0),
    term: "<code>#{h(term)}</code>"
  }).html_safe
end

#search_entries_info(collection, scope, term) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/search_helper.rb', line 29

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


73
74
75
76
77
78
79
# File 'app/helpers/search_helper.rb', line 73

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


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/search_helper.rb', line 46

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_serviceObject


93
94
95
# File 'app/helpers/search_helper.rb', line 93

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