aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/inflector.rb4
-rw-r--r--activesupport/test/inflector_test.rb11
3 files changed, 17 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index ecc9a75929..15c9b5b6ba 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,3 +1,5 @@
+* Added Inflector.humanize to turn attribute names like employee_salary into "Employee salary". Used by automated error reporting in AR.
+
* Added availability of class inheritable attributes to the masses #477 [bitsweat]
class Foo
diff --git a/activesupport/lib/inflector.rb b/activesupport/lib/inflector.rb
index f84a45ea7f..6c8a60e6c4 100644
--- a/activesupport/lib/inflector.rb
+++ b/activesupport/lib/inflector.rb
@@ -27,6 +27,10 @@ module Inflector
camel_cased_word.to_s.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
end
+ def humanize(lower_case_and_underscored_word)
+ lower_case_and_underscored_word.to_s.gsub(/_/, " ").capitalize
+ end
+
def demodulize(class_name_in_module)
class_name_in_module.to_s.gsub(/^.*::/, '')
end
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index 41b5f909bb..4523430ebe 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -65,6 +65,11 @@ class InflectorTest < Test::Unit::TestCase
"PrimarySpokesman" => "primary_spokesmen",
"NodeChild" => "node_children"
}
+
+ UnderscoreToHuman = {
+ "employee_salary" => "Employee salary",
+ "underground" => "Underground"
+ }
def test_pluralize
SingularToPlural.each do |singular, plural|
@@ -120,4 +125,10 @@ class InflectorTest < Test::Unit::TestCase
assert_equal(class_name, Inflector.classify(table_name))
end
end
+
+ def test_humanize
+ UnderscoreToHuman.each do |underscore, human|
+ assert_equal(human, Inflector.humanize(underscore))
+ end
+ end
end \ No newline at end of file