Forum: Ruby on Rails Monkey-patching generators?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael Glaesemann (Guest)
on 2007-06-19 06:01
(Received via mailing list)
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.

Cheers,

Michael Glaesemann
grzm seespotcode net

[1](http://dev.rubyonrails.org/ticket/8656)
Michael Glaesemann (Guest)
on 2007-06-22 18:37
(Received via mailing list)
On Jun 18, 2007, at 23:00 , Michael Glaesemann wrote:

> 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.


> 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.

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.

Thanks!

Michael Glaesemann
grzm seespotcode net
Michael Glaesemann (Guest)
on 2007-06-27 22:22
(Received via mailing list)
On Jun 22, 2007, at 11:36 , Michael Glaesemann wrote:

>
>
>> 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.
>
> 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.

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

Thanks, David!

Michael Glaesemann
grzm seespotcode net

[1](http://dev.rubyonrails.org/changeset/7108)
This topic is locked and can not be replied to.