Commit 46daf01a155b68fb1cda270881aa1f6c8307e830
1 parent
634cbd71
Exists in
master
and in
4 other branches
Search for blobs by default inside project. Added pagination for blobs search
Showing
6 changed files
with
31 additions
and
16 deletions
Show diff stats
app/contexts/search_context.rb
| ... | ... | @@ -14,10 +14,17 @@ class SearchContext |
| 14 | 14 | result[:projects] = projects.search(query).limit(10) |
| 15 | 15 | |
| 16 | 16 | # Search inside singe project |
| 17 | - result[:project] = project = projects.first if projects.length == 1 | |
| 17 | + project = projects.first if projects.length == 1 | |
| 18 | 18 | |
| 19 | 19 | if params[:search_code].present? |
| 20 | - result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo? | |
| 20 | + blobs = [] | |
| 21 | + | |
| 22 | + unless project.empty_repo? | |
| 23 | + blobs = project.repository.search_files(query, params[:repository_ref]) | |
| 24 | + blobs = Kaminari.paginate_array(blobs).page(params[:page]).per(20) | |
| 25 | + end | |
| 26 | + | |
| 27 | + result[:blobs] = blobs | |
| 21 | 28 | else |
| 22 | 29 | result[:merge_requests] = MergeRequest.where(project_id: project_ids).search(query).limit(10) |
| 23 | 30 | result[:issues] = Issue.where(project_id: project_ids).search(query).limit(10) | ... | ... |
app/controllers/search_controller.rb
| ... | ... | @@ -10,13 +10,13 @@ class SearchController < ApplicationController |
| 10 | 10 | group_project_ids = @group.projects.map(&:id) |
| 11 | 11 | project_ids.select! { |id| group_project_ids.include?(id)} |
| 12 | 12 | elsif project_id.present? |
| 13 | + @project = Project.find(params[:project_id]) | |
| 13 | 14 | project_ids.select! { |id| id == project_id.to_i} |
| 14 | 15 | end |
| 15 | 16 | |
| 16 | 17 | result = SearchContext.new(project_ids, params).execute |
| 17 | 18 | |
| 18 | 19 | @projects = result[:projects] |
| 19 | - @project = result[:project] | |
| 20 | 20 | @merge_requests = result[:merge_requests] |
| 21 | 21 | @issues = result[:issues] |
| 22 | 22 | @wiki_pages = result[:wiki_pages] | ... | ... |
app/views/layouts/_search.html.haml
| ... | ... | @@ -2,7 +2,9 @@ |
| 2 | 2 | = form_tag search_path, method: :get, class: 'navbar-form pull-left' do |f| |
| 3 | 3 | = text_field_tag "search", nil, placeholder: "Search", class: "search-input" |
| 4 | 4 | = hidden_field_tag :group_id, @group.try(:id) |
| 5 | - = hidden_field_tag :project_id, @project.try(:id) | |
| 5 | + - if @project && @project.persisted? | |
| 6 | + = hidden_field_tag :project_id, @project.id | |
| 7 | + = hidden_field_tag :search_code, true | |
| 6 | 8 | = hidden_field_tag :repository_ref, @ref |
| 7 | 9 | = submit_tag 'Go' if ENV['RAILS_ENV'] == 'test' |
| 8 | 10 | .search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source } | ... | ... |
| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | +%li | |
| 2 | + .file_holder | |
| 3 | + .file_title | |
| 4 | + = link_to project_blob_path(@project, tree_join(blob.ref, blob.filename), :anchor => "L" + blob.startline.to_s) do | |
| 5 | + %i.icon-file | |
| 6 | + %strong | |
| 7 | + = blob.filename | |
| 8 | + .file_content.code.term | |
| 9 | + %div{class: user_color_scheme_class} | |
| 10 | + = raw blob.colorize( formatter: :gitlab, options: { first_line_number: blob.startline } ) | ... | ... |
app/views/search/_result.html.haml
| 1 | 1 | %fieldset |
| 2 | 2 | %legend |
| 3 | 3 | Search results |
| 4 | - %span.cgray (#{@projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.count}) | |
| 4 | + %span.cgray (#{@projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.total_count}) | |
| 5 | 5 | |
| 6 | 6 | - if @project |
| 7 | 7 | %ul.nav.nav-pills |
| ... | ... | @@ -43,17 +43,11 @@ |
| 43 | 43 | %strong.term |
| 44 | 44 | = truncate wiki_page.title, length: 50 |
| 45 | 45 | %span.light (#{wiki_page.project.name_with_namespace}) |
| 46 | - - @blobs.each do |file| | |
| 47 | - %li | |
| 48 | - .file_holder | |
| 49 | - .file_title | |
| 50 | - = link_to project_blob_path(@project, tree_join(file.ref, file.filename), :anchor => "L" + file.startline.to_s) do | |
| 51 | - %i.icon-file | |
| 52 | - %strong | |
| 53 | - = file.filename | |
| 54 | - .file_content.code.term | |
| 55 | - %div{class: user_color_scheme_class} | |
| 56 | - = raw file.colorize( formatter: :gitlab, options: { first_line_number: file.startline } ) | |
| 46 | + | |
| 47 | + - @blobs.each do |blob| | |
| 48 | + = render 'blob', blob: blob | |
| 49 | + | |
| 50 | + = paginate @blobs, theme: 'gitlab' | |
| 57 | 51 | |
| 58 | 52 | :javascript |
| 59 | 53 | $(function() { | ... | ... |
app/views/search/show.html.haml
| ... | ... | @@ -4,6 +4,8 @@ |
| 4 | 4 | %span Looking for |
| 5 | 5 | .input |
| 6 | 6 | = search_field_tag :search, params[:search], placeholder: "issue 143", class: "input-xxlarge search-text-input", id: "dashboard_search" |
| 7 | + = hidden_field_tag :project_id, params[:project_id] | |
| 8 | + = hidden_field_tag :group_id, params[:group_id] | |
| 7 | 9 | = hidden_field_tag :search_code, params[:search_code] |
| 8 | 10 | = submit_tag 'Search', class: "btn btn-primary wide" |
| 9 | 11 | .prepend-top-10 | ... | ... |