Commit 4791084d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Search::GlobalContext and Search::ProjectContext for separation between 2 types of search

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent cb589d23
module Search
class GlobalContext
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?
projects = current_user.authorized_projects
if params[:group_id].present?
group = Group.find_by_id(params[:group_id])
projects = projects.where(namespace_id: group.id) if group
end
project_ids = projects.pluck(:id)
visibility_levels = if current_user
[Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC]
else
[Gitlab::VisibilityLevel::PUBLIC]
end
result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).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
module Search
class ProjectContext
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
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