Skip to content

Commit

Permalink
FormHelper: convert params#to_unsafe_h only if Rails 5, add tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonatack committed Jul 14, 2016
1 parent 9072028 commit 14e66ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ def html_options(args)
private

def parameters_hash(params)
return params unless params.respond_to?(:to_unsafe_h)
params.to_unsafe_h
if ::ActiveRecord::VERSION::MAJOR == 5 && params.respond_to?(:to_unsafe_h)
params.to_unsafe_h
else
params
end
end

def extract_sort_fields_and_mutate_args!(args)
Expand Down
29 changes: 20 additions & 9 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,28 +329,39 @@ module Helpers
it { should match /exist\=existing/ }
end

context 'using real ActionController Parameter object',
if: ::ActiveRecord::VERSION::MAJOR > 4 do

describe '#sort_link should include search params' do
context 'using a real ActionController::Parameter object',
if: ::ActiveRecord::VERSION::MAJOR > 3 do

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

let(:params) {
ActionController::Parameters
.new({ 'q' => { name_eq: 'TEST' }, controller: 'people' })
it {
should match(
/people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])
=name\+asc/x,
)
}
end

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

it {
should match(
/people\?q(%5B|\[)name_eq(%5D|\])=TEST&q(%5B|\[)s(%5D|\])
/people\?q(%5B|\[)name_eq(%5D|\])=Test2&q(%5B|\[)s(%5D|\])
=name\+asc/x,
)
}
end

end
end

Expand Down

1 comment on commit 14e66ca

@jonatack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction to the commit message: ActionController::Parameters has been a part of Rails since 4.0, which is why the tests in this commit are run for Rails 4 and 5, but not Rails 3.

Please sign in to comment.