# Monkey-patching generators?

**URL:** <https://www.ruby-forum.com/t/monkey-patching-generators/107206>\
**Category:** Rails\
**Created:** [June 19, 2007, 6:01am UTC](https://www.ruby-forum.com/t/monkey-patching-generators/107206 "2007-06-19T06:01:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 19, 2007, 6:01am UTC](https://www.ruby-forum.com/t/monkey-patching-generators/107206/1 "2007-06-19T06:01:28Z")

</div>

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 G.  
grzm seespotcode net

[1](http://dev.rubyonrails.org/ticket/8656)

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 22, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/monkey-patching-generators/107206/2 "2007-06-22T18:37:02Z")

</div>

On Jun 18, 2007, at 23:00 , Michael G. 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 G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 27, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/monkey-patching-generators/107206/3 "2007-06-27T22:22:54Z")

</div>

On Jun 22, 2007, at 11:36 , Michael G. 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 G.  
grzm seespotcode net

[1](http://dev.rubyonrails.org/changeset/7108)
