Topic: refactoring - how to rename a model?

Hello!

Because of the many conventions in rails, renaming a model seem hard. You have to edit the model, the controller, the migrations and the tests. What would be the smart way to do it?

Thanks

Re: refactoring - how to rename a model?

Edit the model, controller, migrations and the tests.  ;-)

Ruby Rockstars - Ruby Jobs for all!

Re: refactoring - how to rename a model?

Or write a script to do it for you wink
then share it with the community.  (There may already be one out there...)

Last edited by pjleonhardt (2006-11-18 01:23:20)

Re: refactoring - how to rename a model?

So there is no ready sollution in the framework? I'm surprised because Rails wants to be so agile.

Re: refactoring - how to rename a model?

There very well could be (though, I don't think so). There are many parts of rails that I've learned through reading blogs and forums because there is no official documentation for them.
This is definitely something the RoR team needs to work on.

Re: refactoring - how to rename a model?

I just realized that my text editor TextMate has a project wide find/replace tool. That makes renaming acceptable comfortable. The tool is activated by pressing shift+option+F

Re: refactoring - how to rename a model?

You can also use Aptana RadRails or probably plain old Eclipse.

For example, to change the 'tool' model to the 'machine' model:

  1. Make a "working set" that includes the files that actually change from app to app: /app/*,  /config/routes.rb, /test/fixtures/*, /test/functional/*, /test/unit/*. ("Project">>"Build Working Set">>"Select Working Set">>"New")

    This will restrict the search to those files, so you aren't in danger of changing something in a plugin or gem.

  2. Do a "File Search" in the working set for each term you're going to change: Start with the most specific, "Tools", then "Tool", then "tools", then "tool". After each "File Search," do a "Replace" (it seems you have to do the search first). So, you'll search for "Tools" and then replace "Tools" with "Machines". Next, you'll search for "Tool" and then replace it with "Machine", etc.

  3. Finally, you do have to rename all the files by hand, but it is only a handful, compared to the hundreds of references (in various Capitalizations and pluralizations) scattered throughout your controllers, views, models, helpers, routes, and tests.

It isn't perfect, but better is better.

Ron

Last edited by paron (2009-08-18 14:07:44)