From 686e27fbaf266b1b41383a39554a066467d15ea3 Mon Sep 17 00:00:00 2001 From: Hirofumi Wakasugi Date: Thu, 9 Mar 2017 13:54:17 +0900 Subject: [PATCH 1/3] Fix URL generation when explicitly passing params to link helpers --- kaminari-core/lib/kaminari/helpers/tags.rb | 2 +- kaminari-core/test/helpers/tags_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/kaminari-core/lib/kaminari/helpers/tags.rb b/kaminari-core/lib/kaminari/helpers/tags.rb index 73b8ec99c..8a710050a 100644 --- a/kaminari-core/lib/kaminari/helpers/tags.rb +++ b/kaminari-core/lib/kaminari/helpers/tags.rb @@ -24,7 +24,7 @@ def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h) @params = @params.with_indifferent_access @params.except!(*PARAM_KEY_BLACKLIST) - @params.merge! params + @params.reverse_merge! params end def to_s(locals = {}) #:nodoc: diff --git a/kaminari-core/test/helpers/tags_test.rb b/kaminari-core/test/helpers/tags_test.rb index feb8bd961..c6cca130f 100644 --- a/kaminari-core/test/helpers/tags_test.rb +++ b/kaminari-core/test/helpers/tags_test.rb @@ -67,6 +67,20 @@ class TagTest < ActionView::TestCase end end + sub_test_case "when passing nested params" do + setup do + self.params[:filter] = ActionController::Parameters.new({status: 'active'}) + end + + test 'for first page' do + assert_match(/users\?filter%5Bstatus%5D=active/, Kaminari::Helpers::Tag.new(self, params: self.params).page_url_for(1)) + end + + test 'for other page' do + assert_match(/users\?filter%5Bstatus%5D=active&page=2/, Kaminari::Helpers::Tag.new(self, params: self.params).page_url_for(2)) + end + end + sub_test_case "with param_name = 'foo.page' option" do setup do self.params['foo.page'] = 2 From 34657444b6935c10e8db27e03143582bbf656718 Mon Sep 17 00:00:00 2001 From: Hirofumi Wakasugi Date: Mon, 21 Aug 2017 11:46:24 +0900 Subject: [PATCH 2/3] Convert passed params to a Hash unless it is --- kaminari-core/lib/kaminari/helpers/tags.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kaminari-core/lib/kaminari/helpers/tags.rb b/kaminari-core/lib/kaminari/helpers/tags.rb index 8a710050a..11342283c 100644 --- a/kaminari-core/lib/kaminari/helpers/tags.rb +++ b/kaminari-core/lib/kaminari/helpers/tags.rb @@ -24,7 +24,13 @@ def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h) @params = @params.with_indifferent_access @params.except!(*PARAM_KEY_BLACKLIST) - @params.reverse_merge! params + # params in Rails 5 may not be a Hash either, + # so it must be converted to a Hash to be merged into @params + unless params.empty? + params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) + params = params.with_indifferent_access + @params.merge! params + end end def to_s(locals = {}) #:nodoc: From 900f9671df07ac966ea388ba4371749cfb2e0126 Mon Sep 17 00:00:00 2001 From: Hirofumi Wakasugi Date: Mon, 21 Aug 2017 11:46:39 +0900 Subject: [PATCH 3/3] Warn when params are passed to helpers explicitly --- kaminari-core/lib/kaminari/helpers/tags.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/kaminari-core/lib/kaminari/helpers/tags.rb b/kaminari-core/lib/kaminari/helpers/tags.rb index 11342283c..3b5e22c8c 100644 --- a/kaminari-core/lib/kaminari/helpers/tags.rb +++ b/kaminari-core/lib/kaminari/helpers/tags.rb @@ -27,6 +27,7 @@ def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: # params in Rails 5 may not be a Hash either, # so it must be converted to a Hash to be merged into @params unless params.empty? + ActiveSupport::Deprecation.warn 'Explicitly passing params to helpers could be removed in the future.' params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) params = params.with_indifferent_access @params.merge! params