Skip to content
Browse files

let demodulize do less work, and add tests

This is also faster on 1.9.
  • Loading branch information...
1 parent 2e74334 commit 0fc531392d1f606054a6d6e394304c72a846d4c8 @fxn fxn committed Oct 29, 2011
Showing with 5 additions and 1 deletion.
  1. +3 −1 activesupport/lib/active_support/inflector/methods.rb
  2. +2 −0 activesupport/test/inflector_test.rb
View
4 activesupport/lib/active_support/inflector/methods.rb
@@ -166,7 +166,9 @@ def dasherize(underscored_word)
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
# "Inflections".demodulize # => "Inflections"
def demodulize(class_name_in_module)
- class_name_in_module.to_s.gsub(/^.*::/, '')
+ # If you remove the module part of an empty string, you get an empty string.
+ # That's why the regexp uses the * quantifier.
+ class_name_in_module.to_s[/[^:]*\z/]
end
# Creates a foreign key name from a class name.
View
2 activesupport/test/inflector_test.rb
@@ -194,6 +194,8 @@ def test_underscore_with_slashes
def test_demodulize
assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
+ assert_equal "Account", ActiveSupport::Inflector.demodulize("Account")
+ assert_equal "", ActiveSupport::Inflector.demodulize("")
end
def test_foreign_key

0 comments on commit 0fc5313

Please sign in to comment.
Something went wrong with that request. Please try again.