Skip to content

Commit

Permalink
Add url_to_next_page and url_to_prev_page helper methods
Browse files Browse the repository at this point in the history
closes #861
  • Loading branch information
yuki24 committed Mar 7, 2018
1 parent ff38bee commit 38e95a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
33 changes: 32 additions & 1 deletion kaminari-core/lib/kaminari/helpers/helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Helpers
#
# * <tt>url_for</tt>: A method that generates an actual path
# * <tt>params</tt>: A method that returns query string parameters
# * <tt>request</tt>: A method that returns a Rack::Request object
#
# A normal Rails controller implements both of the methods, which makes it
# A normal Rails controller implements all the methods, which makes it
# trivial to use this module:
#
# ==== Examples
Expand All @@ -28,6 +29,36 @@ module Helpers
#
module UrlHelper

# A helper that calculates the url to the next page.
#
# ==== Examples
# Basic usage:
#
# <%= next_page_url @items %>
# #-> http://www.example.org/items?page=2
#
# It will return `nil` if there is no next page.
def next_page_url(scope, options = {})
"#{request.base_url}#{next_page_path(scope, options)}" if scope.next_page
end
alias path_to_next_url next_page_url

# A helper that calculates the url to the previous page.
#
# ==== Examples
# Basic usage:
#
# <%= prev_page_url @items %>
# #-> http://www.example.org/items
#
# It will return `nil` if there is no previous page.
def prev_page_url(scope, options = {})
"#{request.base_url}#{prev_page_path(scope, options)}" if scope.prev_page
end
alias previous_page_url prev_page_url
alias url_to_prev_page prev_page_url
alias url_to_previous_page prev_page_url

# A helper that calculates the path to the next page.
#
# ==== Examples
Expand Down
37 changes: 37 additions & 0 deletions kaminari-core/test/helpers/action_view_extension_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,5 +536,42 @@ def to_s
assert_equal'/users?page=2', view.path_to_prev_page(users, params: {controller: 'users', action: 'index'})
end
end

sub_test_case '#next_page_url' do
setup do
2.times {|i| User.create! name: "user#{i}"}
end

test 'the first page' do
users = User.page(1).per(1)
assert_equal 'http://test.host/users?page=2', view.next_page_url(users, params: {controller: 'users', action: 'index'})
end

test 'the last page' do
users = User.page(2).per(1)
assert_nil view.next_page_url(users, params: {controller: 'users', action: 'index'})
end
end

sub_test_case '#prev_page_url' do
setup do
3.times {|i| User.create! name: "user#{i}"}
end

test 'the first page' do
users = User.page(1).per(1)
assert_nil view.prev_page_url(users, params: {controller: 'users', action: 'index'})
end

test 'the second page' do
users = User.page(2).per(1)
assert_equal 'http://test.host/users', view.prev_page_url(users, params: {controller: 'users', action: 'index'})
end

test 'the last page' do
users = User.page(3).per(1)
assert_equal'http://test.host/users?page=2', view.prev_page_url(users, params: {controller: 'users', action: 'index'})
end
end
end
end

0 comments on commit 38e95a2

Please sign in to comment.