I’ve decided that I don’t like that script/generate mailer puts the
mailer class in app/models. Call me picky, but I just think app/models
is for models. I’m not going to try to change the whole of rails just
for me, but I would like to be able to configure this.
First, I looked up the class that actually does the mailer generation.
This is rails_generator/components/mailer/mailer_generator.rb - it’s
either in your gem or in vendor/rails/railties/… if you’ve frozen
your rails version.
The code is straightforward, though it does have the downside of not
being configurable in any way. Ah, well, let’s just try redefining the
method within the file script/generate.
This doesn’t work. After some investigation, I find that it’s because
the includes in that file actually run the generation right after the
original MailerGenerator is defined, so my new definition doesn’t get
applied until after the code is run.
The next try was defining my class (after the necessary includes) at
the top of script/generate, then freezing the class so that the later
includes wouldn’t overwrite it. This, of course, results in a
TypeError. I could surround the later code in a begin…rescue
TypeError block, but I don’t like gobbling up errors.
What ended up working was copying in the contents of the included
script/generate, and inserting my class definition between its includes
and its code. This seemed awfully hackish.
Another solution would be to simply change the original code in
vendor/rails/railties/rails_generator/components/mailer/mailer_generator.rb.
I don’t like this because (a) you musts have rails frozen, (b)
updating to a new version overwrites my changes, and © my code
shouldn’t go in Rails’ directory; it should go in my app somewhere
(preferably lib/, but I’d accept script/).
Yet another solution would be to define my own generator that inherited
from MailerGenerator and call that, but I would really prefer not to.
Other developers might not know about the new one. Really, though, I
just want to be able to make Rails do what I want. I’m all for default
behavior, but I really should be able to change it.
Anyone have any thoughts?
-g c novus