This project is archived and is in readonly mode.
humanize and human_name don't separate words
Reported by Justin French | March 3rd, 2009 @ 07:33 PM | in 2.x
Post.human_name # => "Post" PostComment.human_name # => "Postcomment"
I'd expect:
PostComment.human_name # => "Post Comment"
String#humanize has the same "bug", but #titleize works fine:
"PostComment".humanize # => "Postcomment" # WTF? "PostComment".titleize # => "Post Comment"
String#human_name is using humanize, which seems like it's the right method to use, but it isn't. According to the docs, humanize is expecting a lowercased, underscored string (like a method name), which it does perfectly:
"post_comment".titleize # => "Post Comment"
Since humanize is built to deal with method names, not class names, I think human_name should use titleize instead of humanize, which I'm happy to make a patch for.
Alternatively, humanize could be improved to deal with both class names and method names, which I'm also happy to create a patch for.
Comments and changes to this ticket
-
jack dempsey (jackdempsey) March 3rd, 2009 @ 07:59 PM
+1 I don't feel strongly on how it should be fixed, but I do think it needs an update.
-
Justin French March 4th, 2009 @ 07:44 AM
- Assigned user set to Michael Koziarski
I've attached a patch which changes human_name to use titleize instead of humanize at Koz's request, and assigned it to him. Also added some test coverage, because there wasn't any!
-
José Valim March 6th, 2009 @ 10:49 AM
I don't know if this is appropriate because it would make human_name behave differently from human_attribute_name. And I also think that "Post comment" is a better result (more human readable) than "Post Comment".
If we cannot make humanize work properly in such cases ("PostComment" becomes "Post comment"), I would call underscore first and then humanize, which would output the right result.
-
Justin French March 6th, 2009 @ 11:35 AM
I'll gladly support any patch that is better than "Postcomment", which is utterly broken.
Post Comment Post comment post comment
Anything other than Postcomment!
So here's a patchfile that uses self.name.underscore.humanize, which as José pointed out, is aligned closer to what human_attribute_name does, and humanize expects underscores things anyway. Win win!
-
Niels Ganser March 6th, 2009 @ 02:09 PM
+1 for Justin's patch. Applies nicely and does exactly what I'd expect from human_name.
-
Michael Koziarski March 9th, 2009 @ 07:53 AM
This doesn't apply cleanly at present, can you rebase it and reupload?
Also the comments don't match the implementation?
-
Justin French March 9th, 2009 @ 11:15 PM
Ok, have rebased and corrected the comments (thanks for catching that), new file attached.
-
Francesc Esplugas June 29th, 2009 @ 02:16 PM
+1 for this patch as it's what I expect from this method.
-
Francesc Esplugas June 29th, 2009 @ 02:55 PM
Justin French last patch didn't merged with Rails Edge, updated patch.
(Feel free to change commiter name and email)
-
Repository June 30th, 2009 @ 04:34 AM
- State changed from new to committed
(from [7d548f795d226f57278f8bdd36298c9552421053]) Changed ActiveRecord::Base.human_name to underscore the class name before it humanizes it
This gives you 'Post comment' rather than 'Postcomment' by default.
Signed-off-by: Michael Koziarski michael@koziarski.com
[#2120 state:committed] http://github.com/rails/rails/commit/7d548f795d226f57278f8bdd36298c...
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>
People watching this ticket
Attachments
Referenced by
- 2120 humanize and human_name don't separate words Signed-off-by: Michael Koziarski michael@koziarski.com [#...