module SearchHelper
module SearchHelper
def search_autocomplete_source
def search_autocomplete_opts ( term )
return unless current_user
return unless current_user
resources_results = [
groups_autocomplete ( term ),
projects_autocomplete ( term ),
public_projects_autocomplete ( term ),
]. flatten
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
generic_results . select! { | result | result [ :label ] =~ Regexp . new ( term , "i" ) }
[
[
groups_autocomplete ,
resources_results ,
projects_autocomplete ,
generic_results
public_projects_autocomplete ,
default_autocomplete ,
project_autocomplete ,
help_autocomplete
]. flatten . uniq do | item |
]. flatten . uniq do | item |
item [ :label ]
item [ :label ]
end . to_json
end
end
end
private
private
... @@ -43,7 +49,7 @@ module SearchHelper
... @@ -43,7 +49,7 @@ module SearchHelper
# Autocomplete results for the current project, if it's defined
# Autocomplete results for the current project, if it's defined
def project_autocomplete
def project_autocomplete
if @project && @project . repository . exists? && @project . repository . root_ref
if @project && @project . repository . exists? && @project . repository . root_ref
prefix = simple _sanitize ( @project . name_with_namespace )
prefix = search_result _sanitize ( @project . name_with_namespace )
ref = @ref || @project . repository . root_ref
ref = @ref || @project . repository . root_ref
[
[
... @@ -65,23 +71,36 @@ module SearchHelper
... @@ -65,23 +71,36 @@ module SearchHelper
end
end
# Autocomplete results for the current user's groups
# Autocomplete results for the current user's groups
def groups_autocomplete
def groups_autocomplete ( term , limit = 5 )
current_user . authorized_groups . map do | group |
current_user . authorized_groups . search ( term ). limit ( limit ). map do | group |
{ label: "group: #{ simple_sanitize ( group . name ) } " , url: group_path ( group ) }
{
label: "group: #{ search_result_sanitize ( group . name ) } " ,
url: group_path ( group )
}
end
end
end
end
# Autocomplete results for the current user's projects
# Autocomplete results for the current user's projects
def projects_autocomplete
def projects_autocomplete ( term , limit = 5 )
current_user . authorized_projects . non_archived . map do | p |
current_user . authorized_projects . search_by_title ( term ). non_archived . limit ( limit ). map do | p |
{ label: "project: #{ simple_sanitize ( p . name_with_namespace ) } " , url: project_path ( p ) }
{
label: "project: #{ search_result_sanitize ( p . name_with_namespace ) } " ,
url: project_path ( p )
}
end
end
end
end
# Autocomplete results for the current user's projects
# Autocomplete results for the current user's projects
def public_projects_autocomplete
def public_projects_autocomplete ( term , limit = 5 )
Project . public_or_internal_only ( current_user ). non_archived . map do | p |
Project . public_or_internal_only ( current_user ). search_by_title ( term ). non_archived . limit ( limit ). map do | p |
{ label: "project: #{ simple_sanitize ( p . name_with_namespace ) } " , url: project_path ( p ) }
{
label: "project: #{ search_result_sanitize ( p . name_with_namespace ) } " ,
url: project_path ( p )
}
end
end
end
def search_result_sanitize ( str )
Sanitize . clean ( str )
end
end
end
end