diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-01-16 19:03:42 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-01-16 19:03:42 +0200 |
commit | 1d2bdb4d5880bb1ad39b62c81ff2971aa0cb3798 (patch) | |
tree | 8ac3215b2c25cb57b5f2198fd4d85c7bad968c2b /app/services/search | |
parent | dba982403b7b894d2096ea61b89a247060eefe57 (diff) | |
download | gitlab-ce-1d2bdb4d5880bb1ad39b62c81ff2971aa0cb3798.tar.gz |
Move all Context classes into Services
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/services/search')
-rw-r--r-- | app/services/search/global_service.rb | 40 | ||||
-rw-r--r-- | app/services/search/project_service.rb | 37 |
2 files changed, 77 insertions, 0 deletions
diff --git a/app/services/search/global_service.rb b/app/services/search/global_service.rb new file mode 100644 index 00000000000..c5ca3322362 --- /dev/null +++ b/app/services/search/global_service.rb @@ -0,0 +1,40 @@ +module Search + class GlobalService + attr_accessor :current_user, :params + + def initialize(user, params) + @current_user, @params = user, params.dup + end + + def execute + query = params[:search] + query = Shellwords.shellescape(query) if query.present? + return result unless query.present? + + authorized_projects_ids = [] + authorized_projects_ids += current_user.authorized_projects.pluck(:id) if current_user + authorized_projects_ids += Project.public_or_internal_only(current_user).pluck(:id) + + group = Group.find_by_id(params[:group_id]) if params[:group_id].present? + projects = Project.where(id: authorized_projects_ids) + projects = projects.where(namespace_id: group.id) if group + projects = projects.search(query) + project_ids = projects.pluck(:id) + + result[:projects] = projects.limit(20) + result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20) + result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) + result[:total_results] = %w(projects issues merge_requests).sum { |items| result[items.to_sym].size } + result + end + + def result + @result ||= { + projects: [], + merge_requests: [], + issues: [], + total_results: 0, + } + end + end +end diff --git a/app/services/search/project_service.rb b/app/services/search/project_service.rb new file mode 100644 index 00000000000..3ebaafc752c --- /dev/null +++ b/app/services/search/project_service.rb @@ -0,0 +1,37 @@ +module Search + class ProjectService + attr_accessor :project, :current_user, :params + + def initialize(project, user, params) + @project, @current_user, @params = project, user, params.dup + end + + def execute + query = params[:search] + query = Shellwords.shellescape(query) if query.present? + return result unless query.present? + + if params[:search_code].present? + blobs = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo? + blobs = Kaminari.paginate_array(blobs).page(params[:page]).per(20) + result[:blobs] = blobs + result[:total_results] = blobs.total_count + else + result[:merge_requests] = project.merge_requests.search(query).order('updated_at DESC').limit(20) + result[:issues] = project.issues.search(query).order('updated_at DESC').limit(20) + result[:total_results] = %w(issues merge_requests).sum { |items| result[items.to_sym].size } + end + + result + end + + def result + @result ||= { + merge_requests: [], + issues: [], + blobs: [], + total_results: 0, + } + end + end +end |