Unverified Commit 073a2698 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Refactor search autocomplete. Use ajax for source


Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent e187ef7e7afd
...@@ -47,5 +47,9 @@ class Dispatcher ...@@ -47,5 +47,9 @@ class Dispatcher
initSearch: -> initSearch: ->
autocomplete_json = $('.search-autocomplete-json').data('autocomplete-opts') opts = $('.search-autocomplete-opts')
new SearchAutocomplete(autocomplete_json) path = opts.data('autocomplete-path')
project_id = opts.data('autocomplete-project-id')
project_ref = opts.data('autocomplete-project-ref')
new SearchAutocomplete(path, project_id, project_ref)
class SearchAutocomplete class SearchAutocomplete
constructor: (json) -> constructor: (search_autocomplete_path, project_id, project_ref) ->
project_id = '' unless project_id
project_ref = '' unless project_ref
query = "?project_id=" + project_id + "&project_ref=" + project_ref
$("#search").autocomplete $("#search").autocomplete
source: json source: search_autocomplete_path + query
minLength: 1
select: (event, ui) -> select: (event, ui) ->
location.href = ui.item.url location.href = ui.item.url
......
class SearchController < ApplicationController class SearchController < ApplicationController
include SearchHelper
def show def show
@project = Project.find_by_id(params[:project_id]) if params[:project_id].present? @project = Project.find_by_id(params[:project_id]) if params[:project_id].present?
@group = Group.find_by_id(params[:group_id]) if params[:group_id].present? @group = Group.find_by_id(params[:group_id]) if params[:group_id].present?
...@@ -10,4 +12,12 @@ def show ...@@ -10,4 +12,12 @@ def show
@search_results = Search::GlobalService.new(current_user, params).execute @search_results = Search::GlobalService.new(current_user, params).execute
end end
end end
def autocomplete
term = params[:term]
@project = Project.find(params[:project_id]) if params[:project_id].present?
@ref = params[:project_ref] if params[:project_ref].present?
render json: search_autocomplete_opts(term).to_json
end
end end
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
...@@ -65,23 +71,36 @@ def project_autocomplete ...@@ -65,23 +71,36 @@ def project_autocomplete
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: #{simple_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: #{simple_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: #{simple_sanitize(p.name_with_namespace)}",
url: project_path(p)
}
end end
end end
def simple_sanitize(str)
Sanitize.clean(str)
end
end end
...@@ -138,6 +138,10 @@ def search query ...@@ -138,6 +138,10 @@ def search query
joins(:namespace).where("projects.archived = ?", false).where("projects.name LIKE :query OR projects.path LIKE :query OR namespaces.name LIKE :query OR projects.description LIKE :query", query: "%#{query}%") joins(:namespace).where("projects.archived = ?", false).where("projects.name LIKE :query OR projects.path LIKE :query OR namespaces.name LIKE :query OR projects.description LIKE :query", query: "%#{query}%")
end end
def search_by_title query
where("projects.archived = ?", false).where("projects.name LIKE :query", query: "%#{query}%")
end
def find_with_namespace(id) def find_with_namespace(id)
if id.include?("/") if id.include?("/")
id = id.split("/") id = id.split("/")
......
...@@ -7,4 +7,4 @@ ...@@ -7,4 +7,4 @@
= hidden_field_tag :search_code, true = hidden_field_tag :search_code, true
= hidden_field_tag :repository_ref, @ref = hidden_field_tag :repository_ref, @ref
= submit_tag 'Go' if ENV['RAILS_ENV'] == 'test' = submit_tag 'Go' if ENV['RAILS_ENV'] == 'test'
.search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source } .search-autocomplete-opts.hide{:'data-autocomplete-path' => search_autocomplete_path, :'data-autocomplete-project-id' => @project.try(:id), :'data-autocomplete-project-ref' => @ref }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
# Search # Search
# #
get 'search' => "search#show" get 'search' => "search#show"
get 'search/autocomplete' => "search#autocomplete", as: :search_autocomplete
# API # API
API::API.logger Rails.logger API::API.logger Rails.logger
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment