Project

General

Profile

Patch #6277 » search_rest_api_trunk_r15148.patch

Takenori TAKAKI, 2016-02-25 02:05

View differences:

app/controllers/search_controller.rb
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] || ""
......
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
app/views/search/index.api.rsb
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
test/integration/api_test/search_test.rb
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
(2-2/2)