Rails 3 Generators: The Unusuals (part 2)
After the first two installments of the Rails 3 generator tour, you may be asking yourself, “Hey, what about
performance_test? I use that one all the time!” Well, wait no longer! Here are four more of the generators that have been held over from Rails 2.
[rails2] > ./script/generate session_migration exists db/migrate create db/migrate/20100221180822_add_session_table.rb [rails3] > rails generate session_migration invoke active_record create db/migrate/20100221180822_add_sessions_table.rb
If you’re a fan of storing sessions in the database, you’ll be interested to know that the only difference in Rails 3’s
session_migration generator is the name of the file - they pluralized it. Otherwise, everything’s identical.
[rails2] > ./script/generate integration_test login exists test/integration/ create test/integration/login_test.rb [rails3] > rails generate integration_test login invoke test_unit create test/integration/login_test.rb
The integration test generator hasn’t changed much – by default, it’s identical to Rails 2. There is a new option in Rails 3, however:
Options: [--integration-tool=NAME] # Integration tool to be invoked # Default: test_unit
With the --integration-tool option, you can specify a new set of generators to run – cucumber, for instance?
[rails2] > ./script/generate performance_test homepage exists test/performance/ create test/performance/homepage_test.rb [rails3] > rails generate performance_test homepage invoke test_unit create test/performance/homepage_test.rb
Like the integration test generator, the default case for performance tests is unchanged from Rails 2 to 3. Also like integration tests, though, there’s another new option:
Options: [--performance-tool=NAME] # Performance tool to be invoked # Default: test_unit
If you prefer to use a performance tool other than Test::Unit, you can now do so. I’m not aware of any specific performance testing libraries, though, so for the immediate future the alternatives here will be general-purpose testing frameworks like RSpec. (This points to a possible niche for a new open source project, though.)
[rails2] > ./script/generate plugin obsolete create vendor/plugins/obsolete/lib create vendor/plugins/obsolete/tasks create vendor/plugins/obsolete/test create vendor/plugins/obsolete/README create vendor/plugins/obsolete/MIT-LICENSE create vendor/plugins/obsolete/Rakefile create vendor/plugins/obsolete/init.rb create vendor/plugins/obsolete/install.rb create vendor/plugins/obsolete/uninstall.rb create vendor/plugins/obsolete/lib/obsolete.rb create vendor/plugins/obsolete/tasks/obsolete_tasks.rake create vendor/plugins/obsolete/test/obsolete_test.rb create vendor/plugins/obsolete/test/test_helper.rb [rails3] > rails generate plugin obsolete create vendor/plugins/obsolete create vendor/plugins/obsolete/init.rb create vendor/plugins/obsolete/install.rb create vendor/plugins/obsolete/MIT-LICENSE create vendor/plugins/obsolete/Rakefile create vendor/plugins/obsolete/README create vendor/plugins/obsolete/uninstall.rb create vendor/plugins/obsolete/lib create vendor/plugins/obsolete/lib/obsolete.rb invoke test_unit inside vendor/plugins/obsolete create test create test/obsolete_test.rb create test/test_helper.rb
There aren’t a lot of changes in the plugin generator in Rails 3, either. The biggest difference, in fact, is that the generated Rakefile has slimmed down a little bit (the documentation task has vanished), and the stub rake task file is gone.
There is one other new line in the output:
inside vendor/plugins/obsolete. This is just a flag that the following lines will take effect within a different subdirectory. Without it, you'd get your plugin tests in the top-level test directory, which wouldn't make a lot of sense.
The default case hides some deeper updates, however, as you can see from the options available to the Rails 3 generator:
Options: -t, [--test-framework=NAME] # Test framework to be invoked # Default: test_unit -g, [--generator] # Indicates when to generate generator -r, [--tasks=TASKS] # When supplied creates tasks base files.
We’ve seen the --test-framework option before, so we can ignore it for now. Starting from the bottom, the --tasks option brings back that missing stub tasks file (though the documentation task is gone from the Rakefile forever, I’m afraid). The --generator option is our first look at the functionality Rails 3 gives you to create your own generators – it adds template code for your plugin to define its own generator, which we’ll be exploring more in a later post.
That about does it for the generators that’ve been ported over from Rails 2*. Next time, we’ll start to look at the generators that are fresh and new in Rails 3: generator, model_subclass, and stylesheets. I can’t wait!
* OK, that’s kind of a lie. The scaffold generator has moved over, too, and it’s been joined by a new scaffold_controller generator, as well. These create a lot of files, though, so they’ll get a full post of their own later in the series.