diff options
author | Dongqing Hu <sorra@outlook.com> | 2017-03-31 13:03:55 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-03-31 13:03:55 +0000 |
commit | b0ab0e4eff22ad35fcbad12c3feac6e8ac8b3822 (patch) | |
tree | 631e242fa529383e20921f8057f89be66f982f18 /app/services/search_service.rb | |
parent | 53b21c1e8e27a284a8691a73276204a85694eeb8 (diff) | |
download | gitlab-ce-b0ab0e4eff22ad35fcbad12c3feac6e8ac8b3822.tar.gz |
Refactor SearchController#show
Diffstat (limited to 'app/services/search_service.rb')
-rw-r--r-- | app/services/search_service.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/app/services/search_service.rb b/app/services/search_service.rb new file mode 100644 index 00000000000..8d46a8dab3e --- /dev/null +++ b/app/services/search_service.rb @@ -0,0 +1,63 @@ +class SearchService + include Gitlab::Allowable + + def initialize(current_user, params = {}) + @current_user = current_user + @params = params.dup + end + + def project + return @project if defined?(@project) + + @project = + if params[:project_id].present? + the_project = Project.find_by(id: params[:project_id]) + can?(current_user, :download_code, the_project) ? the_project : nil + else + nil + end + end + + def group + return @group if defined?(@group) + + @group = + if params[:group_id].present? + the_group = Group.find_by(id: params[:group_id]) + can?(current_user, :read_group, the_group) ? the_group : nil + else + nil + end + end + + def show_snippets? + return @show_snippets if defined?(@show_snippets) + + @show_snippets = params[:snippets] == 'true' + end + + delegate :scope, to: :search_service + + def search_results + @search_results ||= search_service.execute + end + + def search_objects + @search_objects ||= search_results.objects(scope, params[:page]) + end + + private + + def search_service + @search_service ||= + if project + Search::ProjectService.new(project, current_user, params) + elsif show_snippets? + Search::SnippetService.new(current_user, params) + else + Search::GlobalService.new(current_user, params) + end + end + + attr_reader :current_user, :params +end |