]> source.dussan.org Git - redmine.git/commitdiff
Handle search pagination with the REST API (#6277).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 19 Mar 2016 10:27:55 +0000 (10:27 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 19 Mar 2016 10:27:55 +0000 (10:27 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@15264 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/search_controller.rb
app/views/search/index.api.rsb
test/integration/api_test/search_test.rb

index ee8c8387914bb45c97bea1c3974130727c3477f1..2888a888912123715c555d46359ee47676debf60 100644 (file)
@@ -27,6 +27,15 @@ class SearchController < ApplicationController
     @search_attachments = params[:attachments].presence || '0'
     @open_issues = params[:open_issues] ? params[:open_issues].present? : false
 
+    case params[:format]
+    when 'xml', 'json'
+      @offset, @limit = api_offset_and_limit
+    else
+      @offset = nil
+      @limit = Setting.search_results_per_page.to_i
+      @limit = 10 if @limit == 0
+    end
+
     # quick jump to an issue
     if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
       redirect_to issue_path(issue)
@@ -67,10 +76,9 @@ class SearchController < ApplicationController
       @result_count_by_type = fetcher.result_count_by_type
       @tokens = fetcher.tokens
 
-      per_page = Setting.search_results_per_page.to_i
-      per_page = 10 if per_page == 0
-      @result_pages = Paginator.new @result_count, per_page, params['page']
-      @results = fetcher.results(@result_pages.offset, @result_pages.per_page)
+      @result_pages = Paginator.new @result_count, @limit, params['page']
+      @offset ||= @result_pages.offset
+      @results = fetcher.results(@offset, @result_pages.per_page)
     else
       @question = ""
     end
index 831f04b5f66044123f673f67b48030abe934ee4a..6619c00e368dee9ff814416d6c22a9810453b851 100644 (file)
@@ -1,4 +1,4 @@
-api.array :results do
+api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do
   @results.each do |result|
     api.result do
       api.id          result.id
index 9ec115409c45934d541037cb4d256e2a08234258..21351ce832e17554e454e5b839c48b5f10f3f0aa 100644 (file)
@@ -71,4 +71,22 @@ class Redmine::ApiTest::SearchTest < Redmine::ApiTest::Base
       end
     end
   end
+
+  test "GET /search.xml should paginate" do
+    issue = (0..10).map {Issue.generate! :subject => 'search_with_limited_results'}.reverse.map(&:id)
+
+    get '/search.json', :q => 'search_with_limited_results', :limit => 4
+    json = ActiveSupport::JSON.decode(response.body)
+    assert_equal 11, json['total_count']
+    assert_equal 0, json['offset']
+    assert_equal 4, json['limit']
+    assert_equal issue[0..3], json['results'].map {|r| r['id']}
+
+    get '/search.json', :q => 'search_with_limited_results', :offset => 8, :limit => 4
+    json = ActiveSupport::JSON.decode(response.body)
+    assert_equal 11, json['total_count']
+    assert_equal 8, json['offset']
+    assert_equal 4, json['limit']
+    assert_equal issue[8..10], json['results'].map {|r| r['id']}
+  end
 end