From 21baf16c0401dea1b6477a457aca5936237f4a56 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 5 Jan 2017 15:43:50 +0900 Subject: [PATCH] Don't accept negative value for padding Because negative offset behavior is so unpredictable: https://github.com/amatsuda/kaminari/issues/839#issuecomment-268211318 closes #839 --- kaminari-core/lib/kaminari/models/page_scope_methods.rb | 4 +++- kaminari-core/test/models/active_record/scopes_test.rb | 4 ++++ kaminari-core/test/models/array_test.rb | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kaminari-core/lib/kaminari/models/page_scope_methods.rb b/kaminari-core/lib/kaminari/models/page_scope_methods.rb index cffb29d..89a71bc 100644 --- a/kaminari-core/lib/kaminari/models/page_scope_methods.rb +++ b/kaminari-core/lib/kaminari/models/page_scope_methods.rb @@ -25,7 +25,9 @@ module Kaminari end def padding(num) - @_padding = num.to_i + num = num.to_i + raise ArgumentError, "padding must not be negative" if num < 0 + @_padding = num offset(offset_value + @_padding) end diff --git a/kaminari-core/test/models/active_record/scopes_test.rb b/kaminari-core/test/models/active_record/scopes_test.rb index 2d53b77..1c1f3f9 100644 --- a/kaminari-core/test/models/active_record/scopes_test.rb +++ b/kaminari-core/test/models/active_record/scopes_test.rb @@ -203,6 +203,10 @@ if defined? ActiveRecord assert_equal 19, relation.current_page assert_equal 19, relation.total_pages end + + test 'Negative padding' do + assert_raise(ArgumentError) { model_class.page(1).per(5).padding(-1) } + end end sub_test_case '#total_pages' do diff --git a/kaminari-core/test/models/array_test.rb b/kaminari-core/test/models/array_test.rb index cc03f68..b363df4 100644 --- a/kaminari-core/test/models/array_test.rb +++ b/kaminari-core/test/models/array_test.rb @@ -88,6 +88,10 @@ class PaginatableArrayTest < ActiveSupport::TestCase test 'per 25, padding 25' do assert_equal 3, @array.page(1).padding(25).total_pages end + + test 'Negative padding' do + assert_raise(ArgumentError) { @array.page(1).per(5).padding(-1) } + end end sub_test_case '#total_pages' do