Monkey-patching generators?

The new scaffold and resource generators are pretty nifty. One drawback they have is that they do not support the --skip-migration option that the model generator does. I've made a patch for the scaffold and resource generators[1], but while I wait for the patch to be applied (fingers-crossed), I want to monkey-patch the generators to make the --skip-migration option available in my local installation of vendor/rails.

I've made small changes before, monkey-patching    * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter by including code in config/environment.rb    * Fixtures by including code in test/test_helper.rb

However, I'm at a loss as to how to monkey-patch a generator without making a modification to code in vendor/rails. AFAICT, no code from the developer areas of the code is loaded after loading Rails::Generator::Base other than generators themselves in lib/generators/ and vendor/plugins/*/generators/ via the Rails::Generator::Lookup module. The require path is

script/generate -> rails_generator (in vendor/rails/railties/lib/) -> rails_generator/base, rails_generator/lookup, rails_generator/commands.

script/generate doesn't do anything but require code from inside of Rails where the actual generation occurs, so there's no opportunity to add the patch through script/generate.

I've tried creating a "scaffold" plugin with a generator (script/generate plugin scaffold --with-generator). This scaffold_generator.rb completely replaces the one in vendor/rails rather than monkey-patches it. I suppose this would work if I copy the existing scaffold_generator.rb (and its templates) and patch it directly, but there's not a lot of elegance in that. (And could this really be considered monkey-patching?)

Something else that's worked is adding the code to the bottom of vendor/rails/railties/lib/rails_generator.rb. This code gets called before the actual generator starts generating, so this works. However, this requires hacking the code in vendor/rails, which is what I was trying to avoid. However, it is cleaner than hacking up both scaffold_generator.rb and resource_generator.rb as it's localized and can be reduced to a single line (well, two, if you include the comment explaining what's going on) requiring another file in lib/ containing the guts of the patch.

Does anyone have alternatives to what I've described above? I'd love to hear critiques and opinions, as I'm benefitting from digging into how Rails does the magic it does.


Michael Glaesemann grzm seespotcode net


So, I take it I've pretty well covered the bases? I haven't come up with anything else in the meantime. Any suggestions or alternatives are most welcome and appreciated.


Michael Glaesemann grzm seespotcode net

Just a followup: Fixed by changeset #7108[1]. Problem solved!

Thanks, David!

Michael Glaesemann grzm seespotcode net