Skip to content

Support camelizing ActionController::Parameters #932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/react.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def self.camelize_props(props)
when Array
props.map { |item| camelize_props(item) }
else
props
if defined?(ActionController::Parameters) && props.is_a?(ActionController::Parameters)
camelize_props(props.to_h)
else
props
end
end
end
end
19 changes: 19 additions & 0 deletions test/react_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,23 @@ def test_it_camelizes_props

assert_equal expected_props, React.camelize_props(raw_props)
end

def test_it_camelizes_params
Copy link
Member

@BookOfGreg BookOfGreg Sep 7, 2018

Choose a reason for hiding this comment

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

You can actually wrap the test in a defined check like this:

if defined?(ActionController::Parameters)
  def test_method
  end
end

Which should make it work on Rails <4

Edit: Though technically speaking we don't support Rails3

raw_params = ActionController::Parameters.new({
foo_bar_baz: 'foo bar baz',
nested_keys: {
qux_etc: 'bish bash bosh'
}
})
permitted_params = raw_params.permit(:foo_bar_baz, nested_keys: :qux_etc)

expected_params = {
'fooBarBaz' => 'foo bar baz',
'nestedKeys' => {
'quxEtc' => 'bish bash bosh'
}
}

assert_equal expected_params, React.camelize_props(permitted_params)
end
end