Customizing Rails Generators

With our template handler in hand and rendering multipart emails, the final step is to create a generator for our plug-in. Our generator will hook into Rails’s mailer generator and configure it to create merb instead of erb templates.

Rails generators provide hooks to allow other generators to extend and customize the generated code. A quick look at the mailer generator in the Rails source code reveals the hooks it provides:

rails/railties/lib/rails/generators/rails/mailer/mailer_generator.rb
 
module​ Rails
 
module​ Generators
 
class​ MailerGenerator < NamedBase
 
source_root File.expand_path(​"../templates"​, __FILE__)
 
argument :actions, type: :array,
 
default: [], banner: ​"method method"
 
check_class_collision ...

Get Crafting Rails 4 Applications, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.