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.

Autocorrect plural ModelName passed to script/generate scaffold

#333

During his RailsConf '08 keynote Jeremy Kemper accidentally illustrated a common problem users run into when using "script/generate scaffold" - passing in the pluralized ModelName causes issues. The user must destroy the scaffold and regenerate it using the singularized form.

This simple patch recognizes when a user has given a plural ModelName, warns the user, and then uses the singularized form of that ModelName to generate the scaffold.

It can be overridden using the switch "--force-plural".

Patch adds a new switch option and an if stmt to ScaffoldGenerator and tests.

Reported by Pat George · June 4th, 2008 @ 10:28 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Ryan McGeary
    Ryan McGeary

    +1 Tested and inspected. I like the auto-correcting nature as to not require remembering whether a singular or plural is required. I'd love to see this added to script/generate model and script/generate resource too.

    June 5th, 2008 @ 02:27 AM

  2. Coderifous
  3. Pratik
    Pratik
    • State changed from new to incomplete
    • Assigned user set to Pratik

    If someone tries to pass pluralized name to generator, they should be given three choices on command line to contrinue :

    • Continue generating with plural
    • Singularize it
    • Abort

    This will also get rid of --force-plural option.

    Thanks.

    June 5th, 2008 @ 10:25 PM

  4. Pat George
    Pat George

    Pratik,

    The only other place I found in which a prompt is used in generation - Rails::Generator::Commands::Create.file - can be overridden by the --force and --skip switches. Which means to stick with the precedent I'd still need to use a switch (or multiple switches) to override the prompt.

    So if you're sure you want a prompt how about the default would be "ask" with optional switches of "--use-plural" and "--use-singular". Although, IMO, the default should be "use-singular".

    Pat

    June 6th, 2008 @ 02:46 PM

  5. Ryan McGeary
    Ryan McGeary

    Pratik,

    I agree with Pat. Passing a plural to a scaffold generation is almost always a mistake, so why not just get out of the way and auto-correct it? After all, if you do decide to override the scaffold generation with a plural, the generated scaffolding will actually result in NameErrors while accessing the named resource routes. Let's continue to enforce good Rails conventions while also having niceties that auto-correct a common human error.

    I don't see a prompt asking whether you want to pluralize or abort as adding a lot of benefit, especially considering you can always script/destroy your way out of a mistake. Was the idea for a prompt just a way to avoid the --force-plural option or was there a different underlying benefit that I'm not thinking of? Thanks for your time on this.

    June 6th, 2008 @ 03:45 PM

  6. Pratik
    Pratik

    Hey,

    I feel if Rails auto corrects the mistake, people would possibly keep repeating the same mistake. If we add an extra step for people making mistakes, it's more likely they'll take a notice and not do it again.

    Having said that, if prompting user involves a lot of extra efforts on code front, we can just go with what you already have.

    Thanks.

    June 6th, 2008 @ 03:55 PM

  7. Ryan McGeary
    Ryan McGeary

    Cool. Pat's patch does display a warning message. I realize this isn't as painful as a halting prompt, but I'm one of those that can't stand seeing warnings, so it serves as enough of a wrist slap for me.

    $ script/generate scaffold books
         warning  Plural version of the model detected, using singularized version.  Override with --force-plural.
         ...
    

    June 6th, 2008 @ 04:17 PM

  8. Pat George
    Pat George

    I think prompting the user involves more effort than it's worth on the code front.

    I do see your point but I think Jeremy's accidental example made it clear that it's not really a teaching tool that's needed but more a nudge in the right direction.

    June 6th, 2008 @ 04:26 PM

  9. Jeremy Kemper
  10. Michael Trim
    Michael Trim

    This probably deserves a separate ticket, but what about 'uncountable' names e.g. "news" (where "news".singularize == "news".pluralize == "news")? This works fine except that the named routes clash so have to be overridden with e.g. :singular => 'news_item' in the routes and renaming all the references in the generated files. It would be nice if the generator detected this as well.

    June 6th, 2008 @ 11:24 PM

  11. Pat George
    Pat George

    Assuming we come to an agreed upon solution and I get the 3 +1s necessary, I'll be looking into Ryan's suggestion next (to put the auto correction code into other Generators).

    When that happens, I would be interested in looking into the handling of uncountables. But yes, it should be a separate ticket.

    June 7th, 2008 @ 01:02 AM

  12. Christopher Murphy
  13. Pratik
    Pratik
    • State changed from incomplete to open

    June 12th, 2008 @ 04:24 PM

  14. Repository
    Repository
    • State changed from open to resolved
    • Tag set to generators, patch, railties

    (from [4ddca325eeef46e640a225efb9b297260e1e8e7d]) Warn and uses singularized ModelName if a plural ModelName is given to script/generate. Override with --force-plural. [#333 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    June 27th, 2008 @ 04:42 PM