1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00
This commit is contained in:
Yuki Nishijima 2014-03-26 21:17:53 -07:00
parent 0f9a4da26f
commit 544acb4deb
2 changed files with 21 additions and 6 deletions

View file

@ -27,13 +27,28 @@ module Kaminari
end
def page_url_for(page)
@template.url_for @params.deep_merge(page_param(page)).merge(:only_path => true)
@template.url_for params_for(page).merge(:only_path => true)
end
private
def page_param(page)
Rack::Utils.parse_nested_query("#{@param_name}=#{page <= 1 ? nil : page}").symbolize_keys
def params_for(page)
page_params = Rack::Utils.parse_nested_query("#{@param_name}=#{page}")
page_params = @params.with_indifferent_access.deep_merge(page_params)
if page <= 1
# This converts a hash:
# from: {other: "params", page: 1}
# to: {other: "params"}
# (when @param_name == "page")
#
# from: {other: "params", user: {name: "yuki", page: 1}}
# to: {other: "params", user: {name: "yuki"}}
# (when @param_name == "user[page]")
@param_name.to_s.scan(/\w+/)[0..-2].inject(page_params){|h, k| h[k] }.delete($&)
end
page_params
end
end

View file

@ -20,11 +20,11 @@ describe 'Kaminari::Helpers' do
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\]=1/ }
it { should_not match /user\[page\]/ }
it { should match /user\[scope\]=active/ }
else
it { should_not match /user%5Bpage%5D=1/ } # match user[page]=1
it { should match /user%5Bscope%5D=active/ } # match user[scope]=active
it { should_not match /user%5Bpage%5D/ } # not match user[page]
it { should match /user%5Bscope%5D=active/ } # match user[scope]=active
end
end