##// END OF EJS Templates
Makes API accept offset/limit or page/limit parameters for retrieving collections....
Jean-Philippe Lang -
r4457:d076c1982227
parent child
Show More
@@ -349,20 +349,27 class ApplicationController < ActionController::Base
349 per_page
349 per_page
350 end
350 end
351
351
352 def api_offset_and_limit
352 # Returns offset and limit used to retrieve objects
353 offset = nil
353 # for an API response based on offset, limit and page parameters
354 if params[:offset].present?
354 def api_offset_and_limit(options=params)
355 offset = params[:offset].to_i
355 if options[:offset].present?
356 offset = options[:offset].to_i
356 if offset < 0
357 if offset < 0
357 offset = 0
358 offset = 0
358 end
359 end
359 end
360 end
360 limit = params[:limit].to_i
361 limit = options[:limit].to_i
361 if limit < 1
362 if limit < 1
362 limit = 25
363 limit = 25
363 elsif limit > 100
364 elsif limit > 100
364 limit = 100
365 limit = 100
365 end
366 end
367 if offset.nil? && options[:page].present?
368 offset = (options[:page].to_i - 1) * limit
369 offset = 0 if offset < 0
370 end
371 offset ||= 0
372
366 [offset, limit]
373 [offset, limit]
367 end
374 end
368
375
@@ -43,4 +43,61 class ApplicationControllerTest < ActionController::TestCase
43 def test_call_hook_mixed_in
43 def test_call_hook_mixed_in
44 assert @controller.respond_to?(:call_hook)
44 assert @controller.respond_to?(:call_hook)
45 end
45 end
46
47 context "test_api_offset_and_limit" do
48 context "without params" do
49 should "return 0, 25" do
50 assert_equal [0, 25], @controller.api_offset_and_limit({})
51 end
52 end
53
54 context "with limit" do
55 should "return 0, limit" do
56 assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30})
57 end
58
59 should "not exceed 100" do
60 assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120})
61 end
62
63 should "not be negative" do
64 assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10})
65 end
66 end
67
68 context "with offset" do
69 should "return offset, 25" do
70 assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10})
71 end
72
73 should "not be negative" do
74 assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10})
75 end
76
77 context "and limit" do
78 should "return offset, limit" do
79 assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50})
80 end
81 end
82 end
83
84 context "with page" do
85 should "return offset, 25" do
86 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1})
87 assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3})
88 end
89
90 should "not be negative" do
91 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0})
92 assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2})
93 end
94
95 context "and limit" do
96 should "return offset, limit" do
97 assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100})
98 assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100})
99 end
100 end
101 end
102 end
46 end
103 end
General Comments 0
You need to be logged in to leave comments. Login now