Skip to content

Instantly share code, notes, and snippets.

@leeadkins
Created September 22, 2010 10:48
Show Gist options
  • Save leeadkins/591490 to your computer and use it in GitHub Desktop.
Save leeadkins/591490 to your computer and use it in GitHub Desktop.
# NearlyHumanize
# The default Rails Humanize methods always capitalizes strings before returning them,
# even if you explicitly set an inflect.human with some unique capitalization in your initalizers.
# Sometimes you want that, but for the times you don't, here's a new method - nearly_humanize
# You can use this in any place you'd normally use humanize, but if it finds a custom inflection
# that you've provided in your initializers, it won't capitalize it. Otherwise, it works like the
# original humanize.
# To use, paste this into an initializer or some other file you have loading at boot
# In case you were wondering, I needed this because I got tired of humanize butchering 'iPhone'
# It's a bit messy and can be cleaned up, but it works pretty well for up-all-night 5AM code
module NearlyHumanize
def nearly_humanize(lower_case_and_underscored_word)
result = lower_case_and_underscored_word.to_s.dup
caps = true
inflections.humans.each do |rule, replacement|
if result.gsub!(rule, replacement)
caps = false
break
end
end
result.gsub(/_id$/, "").gsub(/_/, " ")
if caps == true
result.capitalize!
end
return result
end
end
module ActiveSupport
module Inflector
extend NearlyHumanize
end
end
class ::String
def nearly_humanize
ActiveSupport::Inflector.nearly_humanize(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment