From: Jean-Philippe Lang Date: Thu, 23 Dec 2010 13:33:01 +0000 (+0000) Subject: Makes API accept offset/limit or page/limit parameters for retrieving collections. X-Git-Tag: 1.1.0~39 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d076c1982227af8af1c7505db131367ee6e40b99;p=redmine.git Makes API accept offset/limit or page/limit parameters for retrieving collections. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4571 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 40bdda06b..7c84b99f6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -349,20 +349,27 @@ class ApplicationController < ActionController::Base per_page end - def api_offset_and_limit - offset = nil - if params[:offset].present? - offset = params[:offset].to_i + # Returns offset and limit used to retrieve objects + # for an API response based on offset, limit and page parameters + def api_offset_and_limit(options=params) + if options[:offset].present? + offset = options[:offset].to_i if offset < 0 offset = 0 end end - limit = params[:limit].to_i + limit = options[:limit].to_i if limit < 1 limit = 25 elsif limit > 100 limit = 100 end + if offset.nil? && options[:page].present? + offset = (options[:page].to_i - 1) * limit + offset = 0 if offset < 0 + end + offset ||= 0 + [offset, limit] end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index a37f7de23..57f3e091c 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -43,4 +43,61 @@ class ApplicationControllerTest < ActionController::TestCase def test_call_hook_mixed_in assert @controller.respond_to?(:call_hook) end + + context "test_api_offset_and_limit" do + context "without params" do + should "return 0, 25" do + assert_equal [0, 25], @controller.api_offset_and_limit({}) + end + end + + context "with limit" do + should "return 0, limit" do + assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30}) + end + + should "not exceed 100" do + assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120}) + end + + should "not be negative" do + assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10}) + end + end + + context "with offset" do + should "return offset, 25" do + assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10}) + end + + should "not be negative" do + assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10}) + end + + context "and limit" do + should "return offset, limit" do + assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50}) + end + end + end + + context "with page" do + should "return offset, 25" do + assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1}) + assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3}) + end + + should "not be negative" do + assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0}) + assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2}) + end + + context "and limit" do + should "return offset, limit" do + assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100}) + assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100}) + end + end + end + end end