From 1d65185c90b0604d1b0a3ff3e9b53c3d6361dd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Di=CC=81az?= Date: Wed, 2 Aug 2017 00:10:36 -0500 Subject: Update String#camelize to provide feedback when wrong option is passed String#camelize was returning nil without any feedback when an invalid option was passed as parameter. This update makes the method to raises an ArgumentError when the option passed is invalid, similar to what Ruby does for String#downcase (and others) in 2.4.1. https://ruby-doc.org/core-2.4.1/String.html#method-i-downcase --- activesupport/CHANGELOG.md | 18 ++++++++++++++++++ .../lib/active_support/core_ext/string/inflections.rb | 2 ++ activesupport/test/core_ext/string_ext_test.rb | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 938d4f9d31..e3a39cb8c9 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,21 @@ +* Update String#camelize to provide feedback when wrong option is passed + + String#camelize was returning nil without any feedback when an + invalid option was passed as parameter. + + Previously: + + 'one_two'.camelize(true) + => nil + + Now: + + 'one_two'.camelize(true) + => ArgumentError: Invalid option, use either :upper or :lower. + + *Ricardo Díaz* + + * Fix modulo operations involving durations Rails 5.1 introduce an `ActiveSupport::Duration::Scalar` class as a wrapper diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 846600c623..b5bb385033 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -94,6 +94,8 @@ class String ActiveSupport::Inflector.camelize(self, true) when :lower ActiveSupport::Inflector.camelize(self, false) + else + raise ArgumentError, "Invalid option, use either :upper or :lower." end end alias_method :camelcase, :camelize diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 0afff5aa50..9fd6d8ac0f 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -108,6 +108,13 @@ class StringInflectionsTest < ActiveSupport::TestCase assert_equal("capital", "Capital".camelize(:lower)) end + def test_camelize_invalid_option + e = assert_raise ArgumentError do + "Capital".camelize(nil) + end + assert_equal("Invalid option, use either :upper or :lower.", e.message) + end + def test_dasherize UnderscoresToDashes.each do |underscored, dasherized| assert_equal(dasherized, underscored.dasherize) -- cgit v1.2.3