Skip to content

Commit 14e66ca

Browse files
committed
FormHelper: convert params#to_unsafe_h only if Rails 5, add tests.
This adresses an issue correctly brought up by @ryanswood in PR #644 concerning Rails 4.2 where params#to_unsafe_h still needs to be converted from a Hash to a HWIA to respond to symbol hash keys. I prefer to trying adding a conditional on Active Record version rather than an extra HWIA conversion. The tests are run for Rails 4 and 5, since ActionController::Parameters was backported to 4.x. Rails 3 raises if ActionController::Parameters is invoked, so the tests are skipped in that case. This commit is necessary because when the following is run: ActionController::Parameters.new(q: 1).to_unsafe_h.fetch(:q) with Rails 5 it works, but not with Rails 4.2. The reason is that in Rails 5, #to_unsafe_h calls StrongParameters::convert_parameters_to_hashes which converts hashes to HWIA, whereas in Rails 4.2 it just calls #to_hash without HWIA.
1 parent 9072028 commit 14e66ca

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/ransack/helpers/form_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ def html_options(args)
121121
private
122122

123123
def parameters_hash(params)
124-
return params unless params.respond_to?(:to_unsafe_h)
125-
params.to_unsafe_h
124+
if ::ActiveRecord::VERSION::MAJOR == 5 && params.respond_to?(:to_unsafe_h)
125+
params.to_unsafe_h
126+
else
127+
params
128+
end
126129
end
127130

128131
def extract_sort_fields_and_mutate_args!(args)

spec/ransack/helpers/form_helper_spec.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,28 +329,39 @@ module Helpers
329329
it { should match /exist\=existing/ }
330330
end
331331

332-
context 'using real ActionController Parameter object',
333-
if: ::ActiveRecord::VERSION::MAJOR > 4 do
334-
335-
describe '#sort_link should include search params' do
332+
context 'using a real ActionController::Parameter object',
333+
if: ::ActiveRecord::VERSION::MAJOR > 3 do
336334

335+
describe '#sort_link should include search params with symbol q:' do
337336
subject { @controller.view_context.sort_link(Person.search, :name) }
337+
let(:params) { ActionController::Parameters.new(
338+
{ :q => { name_eq: 'TEST' }, controller: 'people' }
339+
) }
340+
before { @controller.instance_variable_set(:@params, params) }
338341

339-
let(:params) {
340-
ActionController::Parameters
341-
.new({ 'q' => { name_eq: 'TEST' }, controller: 'people' })
342+
it {
343+
should match(
344+
/people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])
345+
=name\+asc/x,
346+
)
342347
}
348+
end
343349

350+
describe "#sort_link should include search params with string 'q'" do
351+
subject { @controller.view_context.sort_link(Person.search, :name) }
352+
let(:params) {
353+
ActionController::Parameters.new(
354+
{ 'q' => { name_eq: 'Test2' }, controller: 'people' }
355+
) }
344356
before { @controller.instance_variable_set(:@params, params) }
345357

346358
it {
347359
should match(
348-
/people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])
360+
/people\?q(%5B|\[)name_eq(%5D|\])=Test2&q(%5B|\[)s(%5D|\])
349361
=name\+asc/x,
350362
)
351363
}
352364
end
353-
354365
end
355366
end
356367

0 commit comments

Comments
 (0)