##// END OF EJS Templates
Handle search pagination with the REST API (#6277)....
Jean-Philippe Lang -
r14882:e1aa18b33388
parent child
Show More
@@ -27,6 +27,15 class SearchController < ApplicationController
27 @search_attachments = params[:attachments].presence || '0'
27 @search_attachments = params[:attachments].presence || '0'
28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
29
29
30 case params[:format]
31 when 'xml', 'json'
32 @offset, @limit = api_offset_and_limit
33 else
34 @offset = nil
35 @limit = Setting.search_results_per_page.to_i
36 @limit = 10 if @limit == 0
37 end
38
30 # quick jump to an issue
39 # quick jump to an issue
31 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
40 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
32 redirect_to issue_path(issue)
41 redirect_to issue_path(issue)
@@ -67,10 +76,9 class SearchController < ApplicationController
67 @result_count_by_type = fetcher.result_count_by_type
76 @result_count_by_type = fetcher.result_count_by_type
68 @tokens = fetcher.tokens
77 @tokens = fetcher.tokens
69
78
70 per_page = Setting.search_results_per_page.to_i
79 @result_pages = Paginator.new @result_count, @limit, params['page']
71 per_page = 10 if per_page == 0
80 @offset ||= @result_pages.offset
72 @result_pages = Paginator.new @result_count, per_page, params['page']
81 @results = fetcher.results(@offset, @result_pages.per_page)
73 @results = fetcher.results(@result_pages.offset, @result_pages.per_page)
74 else
82 else
75 @question = ""
83 @question = ""
76 end
84 end
@@ -1,4 +1,4
1 api.array :results do
1 api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do
2 @results.each do |result|
2 @results.each do |result|
3 api.result do
3 api.result do
4 api.id result.id
4 api.id result.id
@@ -71,4 +71,22 class Redmine::ApiTest::SearchTest < Redmine::ApiTest::Base
71 end
71 end
72 end
72 end
73 end
73 end
74
75 test "GET /search.xml should paginate" do
76 issue = (0..10).map {Issue.generate! :subject => 'search_with_limited_results'}.reverse.map(&:id)
77
78 get '/search.json', :q => 'search_with_limited_results', :limit => 4
79 json = ActiveSupport::JSON.decode(response.body)
80 assert_equal 11, json['total_count']
81 assert_equal 0, json['offset']
82 assert_equal 4, json['limit']
83 assert_equal issue[0..3], json['results'].map {|r| r['id']}
84
85 get '/search.json', :q => 'search_with_limited_results', :offset => 8, :limit => 4
86 json = ActiveSupport::JSON.decode(response.body)
87 assert_equal 11, json['total_count']
88 assert_equal 8, json['offset']
89 assert_equal 4, json['limit']
90 assert_equal issue[8..10], json['results'].map {|r| r['id']}
91 end
74 end
92 end
General Comments 0
You need to be logged in to leave comments. Login now