aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-01-16 15:32:49 -0500
committerGitHub <noreply@github.com>2017-01-16 15:32:49 -0500
commitdc01a40eacfea12140378854b662e79a63dd874a (patch)
tree6387c62a8c75a54398b35cc065496737b4e92c9a
parent050018d48e2c94a36cb0ec61288b1c5519059d36 (diff)
parent5da6a9d6845661c8689fe5d15b22c1172b617122 (diff)
downloadrails-dc01a40eacfea12140378854b662e79a63dd874a.tar.gz
rails-dc01a40eacfea12140378854b662e79a63dd874a.tar.bz2
rails-dc01a40eacfea12140378854b662e79a63dd874a.zip
Merge pull request #27704 from kmcphillips/transliterate-exception
Raise ArgumentError if attempting to transliterate nil
-rw-r--r--activesupport/CHANGELOG.md5
-rw-r--r--activesupport/lib/active_support/inflector/transliterate.rb2
-rw-r--r--activesupport/test/transliterate_test.rb18
3 files changed, 25 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 0bb6b2466c..5207194fba 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Changed `ActiveSupport::Inflector#transliterate` to raise `ArgumentError` when it receives
+ anything except a string.
+
+ *Kevin McPhillips*
+
* Fixed bugs that `StringInquirer#respond_to_missing?` and
`ArrayInquirer#respond_to_missing?` do not fallback to `super`.
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb
index 3e78986e8e..de6dd2720b 100644
--- a/activesupport/lib/active_support/inflector/transliterate.rb
+++ b/activesupport/lib/active_support/inflector/transliterate.rb
@@ -57,6 +57,8 @@ module ActiveSupport
# transliterate('Jürgen')
# # => "Juergen"
def transliterate(string, replacement = "?".freeze)
+ raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
+
I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
replacement: replacement)
diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb
index 040ddd25fc..466b69bcef 100644
--- a/activesupport/test/transliterate_test.rb
+++ b/activesupport/test/transliterate_test.rb
@@ -31,4 +31,22 @@ class TransliterateTest < ActiveSupport::TestCase
def test_transliterate_should_allow_a_custom_replacement_char
assert_equal "a*b", ActiveSupport::Inflector.transliterate("a索b", "*")
end
+
+ def test_transliterate_handles_empty_string
+ assert_equal "", ActiveSupport::Inflector.transliterate("")
+ end
+
+ def test_transliterate_handles_nil
+ exception = assert_raises ArgumentError do
+ ActiveSupport::Inflector.transliterate(nil)
+ end
+ assert_equal "Can only transliterate strings. Received NilClass", exception.message
+ end
+
+ def test_transliterate_handles_unknown_object
+ exception = assert_raises ArgumentError do
+ ActiveSupport::Inflector.transliterate(Object.new)
+ end
+ assert_equal "Can only transliterate strings. Received Object", exception.message
+ end
end