When you want to override a generator template (without replacing the generator itself), in Rails 3 you can just drop files in appropriately named places in lib/templates and Rails will find them.
What if you want to do this with a gem? I’m trying to take my team’s standardized scaffold format and gemify it so we can share it in all projects and update it easily, rather than copy files into lib/ in every project. This works fine for the cases where I’ve created a new generator; I hook into it with config.generators in application.rb and Rails finds it. But when I drop template files into lib/templates in the gem, Rails finds its own default templates first, and renders them instead of mine. I think the search order is RAILS_ROOT/lib/templates, RAILS_GEMS/lib/templates, OTHER_GEMS/lib/templates.
What’s the solution? I’m not finding much docco on this, and code-diving through Rails hasn’t presented an obvious solution. Thanks!
We figured this out. the generators config has a ‘templates’ variable that lists search paths for templates. The problem is indeed that it searches this array in order until it finds a match, so templates in your app or in Rails will get found before templates in your gem.
The solution is to have your gem’s Railtie put the templates path onto the beginning of the array of template paths. It looks like this. This file is in [GEM]/lib/my_gem.rb. The templates are parallel to it in [GEM]/lib/templates/.
If the templates have a path inside [GEM]/lib/templates that matches the path of the default template you are overriding, this should work. For example, if you’ve done this and you create [GEM]/lib/templates/active_record/model/model.rb, it will override the default AR model template.
No monkeypatching of the generators required.
EDIT: Note that since this answer was originally posted, “config.generators” has been removed from Rails. Use
config.app_generatorsinstead as per pixelearth’s answer below.