Ruby on Rails - Code Generator as a member of your Team
Submitted by Ignat on Tue, 2006-09-12 08:23.
One of the most interesting features of Ruby on Rails that increase its popularity is undoubtedly built-in source code generator. This only feature saves a lot of typing, allows developers to add features to the framework, encourages creating new generators for repetitive tasks. In the end it saves a lot of time and effort.
Most of Ruby on Rails projects start from code generation. And code generation is used throughout the project to add new controllers, views and models. For example:
script/generate controller Product list new edit
creates stubs for your product controller and three views to list, to create new, or to update products. You can remove controllers and views with 'script/destroy' command. You can also ask Ruby on Rails generator to automatically add newly created files to a subversion repository. Sweet! But keep reading...
But what happens when the file that the generator is going to create already exists? You only have two options, you can overwrite that file with the generator version or leave it as is. That's sad. Wouldn't it be nice if Ruby on Rails could offer a merge of its version and your version of the file? What happens when two developers edit the same file from a version control repository? Right, a merge operation is initiated. It can be automatic, when computer doesn't see any conflicts in modifications and can apply them together, and it can be developers decision, when they discuss conflicting modifications and accept the best one. So why cannot we consider modifications made by the generator as those made by any other developer? And to see a merge option instead of just overwrite yes/no. What do you think?