@@ -349,20 +349,27 class ApplicationController < ActionController::Base | |||
|
349 | 349 | per_page |
|
350 | 350 | end |
|
351 | 351 | |
|
352 | def api_offset_and_limit | |
|
353 | offset = nil | |
|
354 | if params[:offset].present? | |
|
355 | offset = params[:offset].to_i | |
|
352 | # Returns offset and limit used to retrieve objects | |
|
353 | # for an API response based on offset, limit and page parameters | |
|
354 | def api_offset_and_limit(options=params) | |
|
355 | if options[:offset].present? | |
|
356 | offset = options[:offset].to_i | |
|
356 | 357 | if offset < 0 |
|
357 | 358 | offset = 0 |
|
358 | 359 | end |
|
359 | 360 | end |
|
360 |
limit = |
|
|
361 | limit = options[:limit].to_i | |
|
361 | 362 | if limit < 1 |
|
362 | 363 | limit = 25 |
|
363 | 364 | elsif limit > 100 |
|
364 | 365 | limit = 100 |
|
365 | 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 | 373 | [offset, limit] |
|
367 | 374 | end |
|
368 | 375 |
@@ -43,4 +43,61 class ApplicationControllerTest < ActionController::TestCase | |||
|
43 | 43 | def test_call_hook_mixed_in |
|
44 | 44 | assert @controller.respond_to?(:call_hook) |
|
45 | 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 | 103 | end |
General Comments 0
You need to be logged in to leave comments.
Login now