##// 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 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class SearchController < ApplicationController
19 19 before_filter :find_optional_project
20 accept_api_auth :index
20 21
21 22 def index
22 23 @question = params[:q] || ""
23 24 @question.strip!
24 25 @all_words = params[:all_words] ? params[:all_words].present? : true
25 26 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
26 27 @search_attachments = params[:attachments].presence || '0'
27 28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
28 29
29 30 # quick jump to an issue
30 31 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
31 32 redirect_to issue_path(issue)
32 33 return
33 34 end
34 35
35 36 projects_to_search =
36 37 case params[:scope]
37 38 when 'all'
38 39 nil
39 40 when 'my_projects'
40 41 User.current.projects
41 42 when 'subprojects'
42 43 @project ? (@project.self_and_descendants.active.to_a) : nil
43 44 else
44 45 @project
45 46 end
46 47
47 48 @object_types = Redmine::Search.available_search_types.dup
48 49 if projects_to_search.is_a? Project
49 50 # don't search projects
50 51 @object_types.delete('projects')
51 52 # only show what the user is allowed to view
52 53 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
53 54 end
54 55
55 56 @scope = @object_types.select {|t| params[t]}
56 57 @scope = @object_types if @scope.empty?
57 58
58 59 fetcher = Redmine::Search::Fetcher.new(
59 60 @question, User.current, @scope, projects_to_search,
60 61 :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
61 62 :cache => params[:page].present?
62 63 )
63 64
64 65 if fetcher.tokens.present?
65 66 @result_count = fetcher.result_count
66 67 @result_count_by_type = fetcher.result_count_by_type
67 68 @tokens = fetcher.tokens
68 69
69 70 per_page = Setting.search_results_per_page.to_i
70 71 per_page = 10 if per_page == 0
71 72 @result_pages = Paginator.new @result_count, per_page, params['page']
72 73 @results = fetcher.results(@result_pages.offset, @result_pages.per_page)
73 74 else
74 75 @question = ""
75 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 81 end
78 82
79 83 private
80 84 def find_optional_project
81 85 return true unless params[:id]
82 86 @project = Project.find(params[:id])
83 87 check_project_privacy
84 88 rescue ActiveRecord::RecordNotFound
85 89 render_404
86 90 end
87 91 end
General Comments 0
You need to be logged in to leave comments. Login now