Use new context for search

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2014-01-09 12:37:12 +02:00
parent 4791084d7c
commit b49ebf5737
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
7 changed files with 19 additions and 84 deletions

View file

@ -7,12 +7,9 @@
background: $primary_color; background: $primary_color;
} }
> li > a {
@include border-radius(0);
}
&.nav-stacked { &.nav-stacked {
> li > a { > li > a {
@include border-radius(0);
border-left: 4px solid #EEE; border-left: 4px solid #EEE;
padding: 12px; padding: 12px;
color: #777; color: #777;

View file

@ -1,42 +0,0 @@
class SearchContext
attr_accessor :project_ids, :current_user, :params
def initialize(project_ids, user, params)
@project_ids, @current_user, @params = project_ids, user, params.dup
end
def execute
query = params[:search]
query = Shellwords.shellescape(query) if query.present?
return result unless query.present?
visibility_levels = @current_user ? [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] : [ Gitlab::VisibilityLevel::PUBLIC ]
result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20)
# Search inside single project
single_project_search(Project.where(id: project_ids), query)
result
end
def single_project_search(projects, query)
project = projects.first if projects.length == 1
if params[:search_code].present?
result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
else
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[:wiki_pages] = []
end
end
def result
@result ||= {
projects: [],
merge_requests: [],
issues: [],
wiki_pages: [],
blobs: []
}
end
end

View file

@ -1,33 +1,13 @@
class SearchController < ApplicationController class SearchController < ApplicationController
def show def show
project_id = params[:project_id] @project = Project.find_by_id(params[:project_id]) if params[:project_id].present?
group_id = params[:group_id] @group = Group.find_by_id(params[:group_id]) if params[:group_id].present?
project_ids = find_project_ids(group_id, project_id) if @project
return access_denied! unless can?(current_user, :download_code, @project)
result = SearchContext.new(project_ids, current_user, params).execute @search_results = Search::ProjectContext.new(@project, current_user, params).execute
else
@projects = result[:projects] @search_results = Search::GlobalContext.new(current_user, params).execute
@merge_requests = result[:merge_requests]
@issues = result[:issues]
@wiki_pages = result[:wiki_pages]
@blobs = Kaminari.paginate_array(result[:blobs]).page(params[:page]).per(20)
@total_results = @projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.total_count
end
private
def find_project_ids(group_id, project_id)
project_ids = current_user.authorized_projects.map(&:id)
if group_id.present?
@group = Group.find(group_id)
group_project_ids = @group.projects.map(&:id)
project_ids.select! { |id| group_project_ids.include?(id) }
elsif project_id.present?
@project = Project.find(project_id)
project_ids = @project.public? ? [@project.id] : project_ids.select { |id| id == project_id.to_i }
end end
project_ids
end end
end end

View file

@ -9,7 +9,7 @@
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to search_path(group_id: nil) do = link_to search_path(group_id: nil, search: params[:search]) do
Any Any
- current_user.authorized_groups.sort_by(&:name).each do |group| - current_user.authorized_groups.sort_by(&:name).each do |group|
%li %li
@ -27,7 +27,7 @@
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to search_path(project_id: nil) do = link_to search_path(project_id: nil, search: params[:search]) do
Any Any
- current_user.authorized_projects.sort_by(&:name_with_namespace).each do |project| - current_user.authorized_projects.sort_by(&:name_with_namespace).each do |project|
%li %li

View file

@ -1,5 +1,5 @@
.search_results .search_results
%ul.bordered-list %ul.bordered-list
= render partial: "search/results/project", collection: @projects = render partial: "search/results/project", collection: @search_results[:projects]
= render partial: "search/results/merge_request", collection: @merge_requests = render partial: "search/results/merge_request", collection: @search_results[:merge_requests]
= render partial: "search/results/issue", collection: @issues = render partial: "search/results/issue", collection: @search_results[:issues]

View file

@ -1,4 +1,4 @@
%ul.nav.nav-pills %ul.nav.nav-pills.append-bottom-20
%li{class: ("active" if params[:search_code].present?)} %li{class: ("active" if params[:search_code].present?)}
= link_to search_path(params.merge(search_code: true)) do = link_to search_path(params.merge(search_code: true)) do
Repository Code Repository Code
@ -9,9 +9,9 @@
.search_results .search_results
- if params[:search_code].present? - if params[:search_code].present?
.blob-results .blob-results
= render partial: "search/results/blob", collection: @blobs = render partial: "search/results/blob", collection: @search_results[:blobs]
= paginate @blobs, theme: 'gitlab' = paginate @search_results[:blobs], theme: 'gitlab'
- else - else
%ul.bordered-list %ul.bordered-list
= render partial: "search/results/merge_request", collection: @merge_requests = render partial: "search/results/merge_request", collection: @search_results[:merge_requests]
= render partial: "search/results/issue", collection: @issues = render partial: "search/results/issue", collection: @search_results[:issues]

View file

@ -1,7 +1,7 @@
%fieldset %fieldset
%legend %legend
Search results Search results
%span.cgray (#{@total_results}) %span.cgray (#{@search_results[:total_results]})
- if @project - if @project
= render "project_results" = render "project_results"