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

Custom routes option #938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions kaminari-core/lib/kaminari/helpers/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Tag
def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: nil, **options) #:nodoc:
@template, @theme, @views_prefix, @options = template, theme, views_prefix, options
@param_name = param_name || Kaminari.config.param_name
@route = @options.delete(:route)
@params = template.params
# @params in Rails 5 no longer inherits from Hash
@params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
Expand All @@ -34,8 +35,12 @@ def to_s(locals = {}) #:nodoc:

def page_url_for(page)
params = params_for(page)
params[:only_path] = true
@template.url_for params
if @route
@template.send(@route, params)
else
params[:only_path] = true
@template.url_for params
end
end

private
Expand Down
11 changes: 11 additions & 0 deletions kaminari-core/test/fake_app/rails_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ def index

# helpers
Object.const_set(:ApplicationHelper, Module.new)

module CustomRoutesHelper
def posts_path(params={})
page = params.delete(:page)
if page
"/posts/page/#{page}"
else
"/posts"
end
end
end
5 changes: 5 additions & 0 deletions kaminari-core/test/helpers/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ def template
paginator = Paginator.new(template, param_name: :pagina)
assert_equal :pagina, paginator.page_tag(template).instance_variable_get('@param_name')
end

test '#route' do
paginator = Paginator.new(template, route: :users_path)
assert_equal :users_path, paginator.page_tag(template).instance_variable_get('@route')
end
end
29 changes: 29 additions & 0 deletions kaminari-core/test/helpers/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ class TagTest < ActionView::TestCase
end
end

sub_test_case "with a custom route setting" do
include CustomRoutesHelper

setup do
self.params[:controller] = nil
self.params[:action] = nil
self.params[:page] = 3
end

sub_test_case 'for first page' do
test 'by default' do
assert_equal('/posts', Kaminari::Helpers::Tag.new(self, route: :posts_path).page_url_for(1))
end

test 'config.params_on_first_page == false' do
begin
Kaminari.config.params_on_first_page = true
assert_equal('/posts/page/1', Kaminari::Helpers::Tag.new(self, route: :posts_path).page_url_for(1))
ensure
Kaminari.config.params_on_first_page = false
end
end
end

test 'for other page' do
assert_equal('/posts/page/5', Kaminari::Helpers::Tag.new(self, route: :posts_path).page_url_for(5))
end
end

sub_test_case "with param_name = 'user[page]' option" do
setup do
self.params[:user] = {page: '3', scope: 'active'}
Expand Down