##// END OF EJS Templates
REST API for Search (#6277)....
Jean-Philippe Lang -
r14880:5533a2b5f2e4
parent child
Show More
@@ -0,0 +1,12
1 api.array :results do
2 @results.each do |result|
3 api.result do
4 api.id result.id
5 api.title result.event_title
6 api.type result.event_type
7 api.url url_for(result.event_url(:only_path => false))
8 api.description result.event_description
9 api.datetime result.event_datetime
10 end
11 end
12 end
@@ -0,0 +1,68
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../../test_helper', __FILE__)
19
20 class Redmine::ApiTest::SearchTest < Redmine::ApiTest::Base
21 fixtures :projects, :issues
22
23 test "GET /search.xml should return xml content" do
24 get '/search.xml'
25
26 assert_response :success
27 assert_equal 'application/xml', @response.content_type
28 end
29
30 test "GET /search.json should return json content" do
31 get '/search.json'
32
33 assert_response :success
34 assert_equal 'application/json', @response.content_type
35
36 json = ActiveSupport::JSON.decode(response.body)
37 assert_kind_of Hash, json
38 assert_kind_of Array, json['results']
39 end
40
41 test "GET /search.xml without query strings should return empty results" do
42 get '/search.xml', :q => '', :all_words => ''
43
44 assert_response :success
45 assert_equal 0, assigns(:results).size
46 end
47
48 test "GET /search.xml with query strings should return results" do
49 get '/search.xml', :q => 'recipe subproject commit', :all_words => ''
50
51 assert_response :success
52 assert_not_empty(assigns(:results))
53
54 assert_select 'results[type=array]' do
55 assert_select 'result', :count => assigns(:results).count
56 assigns(:results).size.times.each do |i|
57 assert_select 'result' do
58 assert_select 'id', :text => assigns(:results)[i].id.to_s
59 assert_select 'title', :text => assigns(:results)[i].event_title
60 assert_select 'type', :text => assigns(:results)[i].event_type
61 assert_select 'url', :text => url_for(assigns(:results)[i].event_url(:only_path => false))
62 assert_select 'description', :text => assigns(:results)[i].event_description
63 assert_select 'datetime'
64 end
65 end
66 end
67 end
68 end
@@ -1,87 +1,91
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class SearchController < ApplicationController
18 class SearchController < ApplicationController
19 before_filter :find_optional_project
19 before_filter :find_optional_project
20 accept_api_auth :index
20
21
21 def index
22 def index
22 @question = params[:q] || ""
23 @question = params[:q] || ""
23 @question.strip!
24 @question.strip!
24 @all_words = params[:all_words] ? params[:all_words].present? : true
25 @all_words = params[:all_words] ? params[:all_words].present? : true
25 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
26 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
26 @search_attachments = params[:attachments].presence || '0'
27 @search_attachments = params[:attachments].presence || '0'
27 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
28
29
29 # quick jump to an issue
30 # quick jump to an issue
30 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
31 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
31 redirect_to issue_path(issue)
32 redirect_to issue_path(issue)
32 return
33 return
33 end
34 end
34
35
35 projects_to_search =
36 projects_to_search =
36 case params[:scope]
37 case params[:scope]
37 when 'all'
38 when 'all'
38 nil
39 nil
39 when 'my_projects'
40 when 'my_projects'
40 User.current.projects
41 User.current.projects
41 when 'subprojects'
42 when 'subprojects'
42 @project ? (@project.self_and_descendants.active.to_a) : nil
43 @project ? (@project.self_and_descendants.active.to_a) : nil
43 else
44 else
44 @project
45 @project
45 end
46 end
46
47
47 @object_types = Redmine::Search.available_search_types.dup
48 @object_types = Redmine::Search.available_search_types.dup
48 if projects_to_search.is_a? Project
49 if projects_to_search.is_a? Project
49 # don't search projects
50 # don't search projects
50 @object_types.delete('projects')
51 @object_types.delete('projects')
51 # only show what the user is allowed to view
52 # only show what the user is allowed to view
52 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
53 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
53 end
54 end
54
55
55 @scope = @object_types.select {|t| params[t]}
56 @scope = @object_types.select {|t| params[t]}
56 @scope = @object_types if @scope.empty?
57 @scope = @object_types if @scope.empty?
57
58
58 fetcher = Redmine::Search::Fetcher.new(
59 fetcher = Redmine::Search::Fetcher.new(
59 @question, User.current, @scope, projects_to_search,
60 @question, User.current, @scope, projects_to_search,
60 :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
61 :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
61 :cache => params[:page].present?
62 :cache => params[:page].present?
62 )
63 )
63
64
64 if fetcher.tokens.present?
65 if fetcher.tokens.present?
65 @result_count = fetcher.result_count
66 @result_count = fetcher.result_count
66 @result_count_by_type = fetcher.result_count_by_type
67 @result_count_by_type = fetcher.result_count_by_type
67 @tokens = fetcher.tokens
68 @tokens = fetcher.tokens
68
69
69 per_page = Setting.search_results_per_page.to_i
70 per_page = Setting.search_results_per_page.to_i
70 per_page = 10 if per_page == 0
71 per_page = 10 if per_page == 0
71 @result_pages = Paginator.new @result_count, per_page, params['page']
72 @result_pages = Paginator.new @result_count, per_page, params['page']
72 @results = fetcher.results(@result_pages.offset, @result_pages.per_page)
73 @results = fetcher.results(@result_pages.offset, @result_pages.per_page)
73 else
74 else
74 @question = ""
75 @question = ""
75 end
76 end
76 render :layout => false if request.xhr?
77 respond_to do |format|
78 format.html { render :layout => false if request.xhr? }
79 format.api { @results ||= []; render :layout => false }
80 end
77 end
81 end
78
82
79 private
83 private
80 def find_optional_project
84 def find_optional_project
81 return true unless params[:id]
85 return true unless params[:id]
82 @project = Project.find(params[:id])
86 @project = Project.find(params[:id])
83 check_project_privacy
87 check_project_privacy
84 rescue ActiveRecord::RecordNotFound
88 rescue ActiveRecord::RecordNotFound
85 render_404
89 render_404
86 end
90 end
87 end
91 end
General Comments 0
You need to be logged in to leave comments. Login now