Fix a bug where the friendly url for the first page is broken

fixes #554
This commit is contained in:
Yuki Nishijima 2014-05-21 17:07:29 -07:00
parent 5f7c5a27c9
commit 157586b5b3
3 changed files with 26 additions and 15 deletions

View File

@ -40,13 +40,13 @@ module Kaminari
if page <= 1
# This converts a hash:
# from: {other: "params", page: 1}
# to: {other: "params"}
# to: {other: "params", page: nil}
# (when @param_name == "page")
#
# from: {other: "params", user: {name: "yuki", page: 1}}
# to: {other: "params", user: {name: "yuki"}}
# to: {other: "params", user: {name: "yuki", page: nil}}
# (when @param_name == "user[page]")
@param_name.to_s.scan(/\w+/)[0..-2].inject(page_params){|h, k| h[k] }.delete($&)
@param_name.to_s.scan(/\w+/)[0..-2].inject(page_params){|h, k| h[k] }[$&] = nil
end
page_params

View File

@ -20,6 +20,9 @@ app.initialize!
# routes
app.routes.draw do
resources :users
resources :addresses do
get 'page/:page', :action => :index, :on => :collection
end
end
#models

View File

@ -4,27 +4,35 @@ include Kaminari::Helpers
describe 'Kaminari::Helpers' do
describe 'Tag' do
describe '#page_url_for', :if => defined?(Rails) do
context "with a friendly route setting" do
before do
helper.request.assign_parameters(_routes, "addresses", "index", :page => 3)
end
context "for first page" do
subject { Tag.new(helper).page_url_for(1) }
it { should == "/addresses" }
end
context "for other page" do
subject { Tag.new(helper).page_url_for(5) }
it { should == "/addresses/page/5" }
end
end
context "with param_name = 'user[page]' option" do
before do
stub(helper).params do
{
:controller => 'users',
:action => 'index',
:user => {
:scope => "active",
:page => 3
}
}.with_indifferent_access
end
helper.request.assign_parameters(_routes, "users", "index")
helper.params.merge!(:user => {:page => "3", :scope => "active"})
end
context "for first page" do
subject { Tag.new(helper, :param_name => "user[page]").page_url_for(1) }
if ActiveSupport::VERSION::STRING < "3.1.0"
it { should_not match(/user\[page\]/) }
it { should_not match(/user\[page\]=\d+/) }
it { should match(/user\[scope\]=active/) }
else
it { should_not match(/user%5Bpage%5D/) } # not match user[page]
it { should_not match(/user%5Bpage%5D=\d+/) } # not match user[page]=\d+
it { should match(/user%5Bscope%5D=active/) } # match user[scope]=active
end
end