From 544acb4deb8dd9b9f369553b4af4d0eee788e40f Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Wed, 26 Mar 2014 21:17:53 -0700 Subject: [PATCH] :disappointed_relieved: --- lib/kaminari/helpers/tags.rb | 21 ++++++++++++++++++--- spec/helpers/tags_spec.rb | 6 +++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/kaminari/helpers/tags.rb b/lib/kaminari/helpers/tags.rb index d802850..6fe8370 100644 --- a/lib/kaminari/helpers/tags.rb +++ b/lib/kaminari/helpers/tags.rb @@ -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 diff --git a/spec/helpers/tags_spec.rb b/spec/helpers/tags_spec.rb index 6daa83f..e3a0218 100644 --- a/spec/helpers/tags_spec.rb +++ b/spec/helpers/tags_spec.rb @@ -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