Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

humanize and human_name don't separate words

#2120

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.

Reported by Justin French · March 3rd, 2009 @ 07:33 PM

State: committed
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. jack dempsey (jackdempsey)
    jack dempsey (jackdempsey)

    +1 I don't feel strongly on how it should be fixed, but I do think it needs an update.

    March 3rd, 2009 @ 07:59 PM

  2. Justin French
    Justin French
    • 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!

    March 4th, 2009 @ 07:44 AM

  3. José Valim
    José Valim

    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.

    March 6th, 2009 @ 10:49 AM

  4. Justin French
    Justin French

    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!

    March 6th, 2009 @ 11:35 AM

  5. Niels Ganser
    Niels Ganser

    +1 for Justin's patch. Applies nicely and does exactly what I'd expect from human_name.

    March 6th, 2009 @ 02:09 PM

  6. Michael Koziarski
    Michael Koziarski

    This doesn't apply cleanly at present, can you rebase it and reupload?

    Also the comments don't match the implementation?

    March 9th, 2009 @ 07:53 AM

  7. Justin French
    Justin French

    Ok, have rebased and corrected the comments (thanks for catching that), new file attached.

    March 9th, 2009 @ 11:15 PM

  8. Francesc Esplugas
    Francesc Esplugas

    +1 for this patch as it's what I expect from this method.

    June 29th, 2009 @ 02:16 PM

  9. Francesc Esplugas
    Francesc Esplugas

    Justin French last patch didn't merged with Rails Edge, updated patch.

    (Feel free to change commiter name and email)

    June 29th, 2009 @ 02:55 PM

  10. Repository
    Repository
    • 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 humanize and human_name don't separate words state:committed] http://github.com/rails/rails/commit/7d548f795d226f57278f8bdd36298c...

    June 30th, 2009 @ 04:34 AM

  11. Justin French
    Justin French

    Thanks Francesc, thanks Koz!

    June 30th, 2009 @ 06:36 AM

  12. Francesc Esplugas
    Francesc Esplugas

    Can the latest patch be applied to 2.3.X?

    September 13th, 2009 @ 06:55 PM

  13. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:11 PM

  14. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:39 AM

  15. bingbing