This project is archived and is in readonly mode.

#2944 ✓wontfix
Owain Hunt

Titleize doesn't take all-uppercase words into account

Reported by Owain Hunt | July 23rd, 2009 @ 05:46 PM | in 2.x

When using the "my string".titleize method, words which are already all-uppercase are processed like any other word. This leads to certain strings being displayed differently to how one might intend.

For example:

"BLT sandwich".titleize => "Blt Sandwich"
"USA today".titleize => "Usa Today"

Below is the (incredibly hacky) version I use, which first checks whether a string is all-uppercase. If it is, the string is not capitalized.

def titleize(word)
  words = word.split(' ')
  processed_words = []
  words.each do |aWord|
    aWord = aWord.capitalize unless /^[A-Z]*$/.match(aWord)
    processed_words << aWord
  end
  processed_words.join(" ")
end

Comments and changes to this ticket

  • John Pignata

    John Pignata July 28th, 2009 @ 06:18 AM

    I extended ActiveSupport to allow me to do this in a project where we had some uppercase acronyms that we wanted to maintain for various reasons. The approach is not very different from Owain's. If a string has two consecutive upcased letters, there's some funny business going on.

    -jp

  • Michael Koziarski

    Michael Koziarski August 3rd, 2009 @ 06:01 AM

    • Tag changed from 2.x, capitalize, inflections, string, titleize to 2.x, bugmash, capitalize, inflections, string, titleize
  • Matt Duncan

    Matt Duncan August 8th, 2009 @ 01:23 AM

    +1 to John's patch which checks for two consecutive uppercase letters.

    Helpful for the following:

    >> "OpenID".titleize
    => "OpenID"
    >> "ActiveRecord".titleize
    => "Active Record"
    
  • Matt Duncan

    Matt Duncan August 8th, 2009 @ 01:28 AM

    Verified John's patch - sorry, should have included that in my previous update.

  • Josh Sharpe

    Josh Sharpe August 8th, 2009 @ 02:41 AM

    verified and +1
    This has always bugged me!

  • Elad Meidar

    Elad Meidar August 8th, 2009 @ 04:01 AM

    Patch does not apply. i wasn't able to determine the reason so the patch was not fixed.

  • Elad Meidar

    Elad Meidar August 8th, 2009 @ 04:26 AM

    +1 verified Patch applies, and all tests run.
    Previous error was due to my mistake, sorry.

  • Steve St. Martin

    Steve St. Martin August 8th, 2009 @ 05:02 AM

    -1 patch applies and may be a great solution for edge cases however poses a major issue as "MY ALL CAPITALIZED STRING".titleize will output all uppercase when users input data with caps-lock which is likely to happen often.

    perhaps a different solution would be to allow a config option of words to ignore in specific inflector methods?

  • Josh Sharpe

    Josh Sharpe August 8th, 2009 @ 05:52 AM

    I agree with steve, but I don't like the config option idea... What if we changed the patch to detect a caps-locked entry:

    pseudo-code

    if str == str.upcase
    titleize_old_way else
    titleize_patch_way end

    That's a bit hacky, I know....but I think it'd do the trick

  • Steve St. Martin

    Steve St. Martin August 8th, 2009 @ 06:23 AM

    although the config idea sounds off, it is more likely to be backwords compatible and to cleanup all kinds of crazy things "Oops i ACCidentally CaPPitalized some stuff i wasn't supposed to", as things are less likely to be an acronym.

    The inflector already has the ability for exceptions built in:

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.human /OpenID/, 'OpenID'
    end
    

    problem lies in that humanize takes an underscored string, so at this point the acronym is already lost, so titelize can be modified to check inflections.humans itself, or possibly move underscore into humanize

  • Josh Sharpe

    Josh Sharpe August 8th, 2009 @ 06:24 AM

    Here's an addition to the previous patch that titleizes ALL CAPS strings.

  • Steve St. Martin

    Steve St. Martin August 8th, 2009 @ 07:11 AM

    turns out a custom inflection option will probably need to be made and patched in as the real problem is that .capitalize is always called last and will ultimately destroy any replacements we do, so those words would need to be replaced after capitalize gets to them, or skip capetilize if ex: inflections.acronyms.included?(word)

  • John Pignata

    John Pignata August 8th, 2009 @ 02:07 PM

    Great comments. My vote would be to close this ticket, leave titleize alone, and encourage developers who have edge needs such as technical or medical content to write their own titleize helper to take into account everything you gents have discussed above. titleize as it stands does as advertised.

    -jp

  • Rizwan Reza

    Rizwan Reza August 8th, 2009 @ 02:23 PM

    -1 I also vote to leave titleize helper alone. It works as expected.

  • Matt Duncan

    Matt Duncan August 8th, 2009 @ 02:27 PM

    @stevestmartin great comments. I'd vote to leave titleize alone for now also. This is something which would be a lot easier to solve on an individual application basis for anyone who needs it without causing unexpected results.

  • Elad Meidar

    Elad Meidar August 8th, 2009 @ 04:21 PM

    -1 leave #titlieze alone too, it is an edge cases that require this much attention anyways.

  • Greg Sterndale

    Greg Sterndale August 8th, 2009 @ 05:35 PM

    1. I think edge cases should be handled in the Inflector.
  • Greg Sterndale
  • Milan Dobrota
  • Milan Dobrota
  • Dan Croak

    Dan Croak August 8th, 2009 @ 07:40 PM

    -1 ditto eize.sus and gsterndale

  • chucknelson

    chucknelson August 8th, 2009 @ 07:45 PM

    -1 reading all the above comments, it definitely makes sense for edge cases to be handled on a case-by-case basis and not to disturb the core functionality of titleize.

  • Rizwan Reza
  • Jeremy Kemper

    Jeremy Kemper August 8th, 2009 @ 11:06 PM

    • State changed from “new” to “wontfix”
  • Jeremy Kemper

    Jeremy Kemper August 8th, 2009 @ 11:26 PM

    • Tag changed from 2.x, bugmash, capitalize, inflections, string, titleize to 2.x, capitalize, inflections, string, titleize

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages