Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to inspect current value of per_page #875

Merged
merged 1 commit into from May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion kaminari-core/lib/kaminari/models/page_scope_methods.rb
Expand Up @@ -19,7 +19,7 @@ def per(num, max_per_page: nil)

def max_paginates_per(new_max_per_page)
@_max_per_page = new_max_per_page
per ((defined?(@_per) && @_per) || default_per_page), max_per_page: new_max_per_page
per current_per_page, max_per_page: new_max_per_page
end

def padding(num)
Expand Down Expand Up @@ -52,6 +52,11 @@ def current_page
raise ZeroPerPageOperation, "Current page was incalculable. Perhaps you called .per(0)?"
end

# Current per-page number
def current_per_page
(defined?(@_per) && @_per) || default_per_page
end

# Next page number in the collection
def next_page
current_page + 1 unless last_page? || out_of_range?
Expand Down
14 changes: 14 additions & 0 deletions kaminari-core/test/models/active_record/scopes_test.rb
Expand Up @@ -293,6 +293,20 @@ def shutdown
end
end

sub_test_case '#current_per_page' do
test 'per 0' do
assert_equal 0, model_class.page.per(0).current_per_page
end

test 'no per specified' do
assert_equal model_class.default_per_page, model_class.page.current_per_page
end

test 'per specified as 42' do
assert_equal 42, model_class.page.per(42).current_per_page
end
end

sub_test_case '#next_page' do
test 'page 1' do
assert_equal 2, model_class.page.next_page
Expand Down