This project is archived and is in readonly mode.
Titleize doesn't take all-uppercase words into account
-
John Pignata
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
- Tag changed from 2.x, capitalize, inflections, string, titleize to 2.x, bugmash, capitalize, inflections, string, titleize
-
Matt Duncan
+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
Verified John's patch - sorry, should have included that in my previous update.
-
Josh Sharpe
verified and +1
This has always bugged me! -
Elad Meidar
Patch does not apply. i wasn't able to determine the reason so the patch was not fixed.
-
Elad Meidar
+1 verified Patch applies, and all tests run.
Previous error was due to my mistake, sorry. -
Steve St. Martin
-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
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 endThat's a bit hacky, I know....but I think it'd do the trick
-
Steve St. Martin
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' endproblem 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
Here's an addition to the previous patch that titleizes ALL CAPS strings.
-
Steve St. Martin
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
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
-1 I also vote to leave titleize helper alone. It works as expected.
-
Matt Duncan
@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
-1 leave #titlieze alone too, it is an edge cases that require this much attention anyways.
-
Greg Sterndale
- I think edge cases should be handled in the Inflector.
-
Milan Dobrota
- Works as expected.
-
Milan Dobrota
that was minus one :)
-
Dan Croak
-1 ditto eize.sus and gsterndale
-
chucknelson
-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
-1
-
Jeremy Kemper
- State changed from new to wontfix
-
Jeremy Kemper
- Tag changed from 2.x, bugmash, capitalize, inflections, string, titleize to 2.x, capitalize, inflections, string, titleize
