Move the fix for Rails 5's AC::Parameters out of the top-level Tag class

This commit is contained in:
Yuki Nishijima 2017-09-25 17:48:05 +09:00
parent ef19c8f1ab
commit de92f065e3
3 changed files with 49 additions and 22 deletions

View File

@ -24,14 +24,7 @@ module Kaminari
@params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
@params = @params.with_indifferent_access
@params.except!(*PARAM_KEY_BLACKLIST)
# 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
end
@params.reverse_merge! params
end
def to_s(locals = {}) #:nodoc:
@ -124,6 +117,19 @@ module Kaminari
# The "previous" page of the current page
class PrevPage < Tag
include Link
# TODO: Remove this initializer before 1.3.0.
def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: nil, **options) #:nodoc:
# params in Rails 5 may not be a Hash either,
# so it must be converted to a Hash to be merged into @params
if params && params.respond_to?(:to_unsafe_h)
ActiveSupport::Deprecation.warn 'Explicitly passing params to helpers could be omitted.'
params = params.to_unsafe_h
end
super(template, params: params, param_name: param_name, theme: theme, views_prefix: views_prefix, **options)
end
def page #:nodoc:
@options[:current_page] - 1
end
@ -132,6 +138,19 @@ module Kaminari
# The "next" page of the current page
class NextPage < Tag
include Link
# TODO: Remove this initializer before 1.3.0.
def initialize(template, params: {}, param_name: nil, theme: nil, views_prefix: nil, **options) #:nodoc:
# params in Rails 5 may not be a Hash either,
# so it must be converted to a Hash to be merged into @params
if params && params.respond_to?(:to_unsafe_h)
ActiveSupport::Deprecation.warn 'Explicitly passing params to helpers could be omitted.'
params = params.to_unsafe_h
end
super(template, params: params, param_name: param_name, theme: theme, views_prefix: views_prefix, **options)
end
def page #:nodoc:
@options[:current_page] + 1
end

View File

@ -120,6 +120,17 @@ if defined?(::Rails::Railtie) && defined?(::ActionView)
assert_nil view.link_to_previous_page(users, 'Previous', params: {controller: 'users', action: 'index'})
assert_equal 'At the Beginning', (view.link_to_previous_page(users, 'Previous', params: {controller: 'users', action: 'index'}) do 'At the Beginning' end)
end
test '#link_to_previous_page accepts ActionController::Parameters' do
users = User.page(3)
params = ActionController::Parameters.new(controller: 'users', action: 'index', status: 'active')
html = view.link_to_previous_page users, 'Previous', params: params
assert_match(/page=2/, html)
assert_match(/rel="prev"/, html)
assert_match(/status=active/, html)
end
end
sub_test_case '#link_to_next_page' do
@ -161,6 +172,17 @@ if defined?(::Rails::Railtie) && defined?(::ActionView)
assert_nil view.link_to_next_page(users, 'More', params: {controller: 'users', action: 'index'})
end
test '#link_to_next_page accepts ActionController::Parameters' do
users = User.page(1)
params = ActionController::Parameters.new(controller: 'users', action: 'index', status: 'active')
html = view.link_to_next_page users, 'More', params: params
assert_match(/page=2/, html)
assert_match(/rel="next"/, html)
assert_match(/status=active/, html)
end
end
sub_test_case '#page_entries_info' do

View File

@ -67,20 +67,6 @@ if defined?(::Rails::Railtie) && defined?(ActionView)
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