Guest User

Untitled

a guest
Feb 5th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. # NearlyHumanize
  2. # The default Rails Humanize methods always capitalizes strings before returning them,
  3. # even if you explicitly set an inflect.human with some unique capitalization in your initalizers.
  4. # Sometimes you want that, but for the times you don't, here's a new method - nearly_humanize
  5.  
  6. # You can use this in any place you'd normally use humanize, but if it finds a custom inflection
  7. # that you've provided in your initializers, it won't capitalize it. Otherwise, it works like the
  8. # original humanize.
  9. # To use, paste this into an initializer or some other file you have loading at boot
  10.  
  11. # In case you were wondering, I needed this because I got tired of humanize butchering 'iPhone'
  12. # It's a bit messy and can be cleaned up, but it works pretty well for up-all-night 5AM code
  13.  
  14. module NearlyHumanize
  15. def nearly_humanize(lower_case_and_underscored_word)
  16. result = lower_case_and_underscored_word.to_s.dup
  17. caps = true
  18. inflections.humans.each do |rule, replacement|
  19. if result.gsub!(rule, replacement)
  20. caps = false
  21. break
  22. end
  23. end
  24. result.gsub(/_id$/, "").gsub(/_/, " ")
  25. if caps == true
  26. result.capitalize!
  27. end
  28. return result
  29. end
  30. end
  31.  
  32. module ActiveSupport
  33. module Inflector
  34. extend NearlyHumanize
  35. end
  36. end
  37.  
  38. class ::String
  39. def nearly_humanize
  40. ActiveSupport::Inflector.nearly_humanize(self)
  41. end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment