Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a rails generator template in a gem?

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!

like image 441
IdahoEv Avatar asked Apr 14 '11 16:04

IdahoEv


5 Answers

Update: IdahoEv has the right answer, but this code, as of 3.1 produces the following deprication warning:

DEPRECATION WARNING: config.generators in Rails::Railtie is deprecated. Please use config.app_generators instead.

So use this instead:

module MyGem
  class Railtie < Rails::Railtie
    config.app_generators do |g|
      g.templates.unshift File::expand_path('../templates', __FILE__)
    end 
  end
end 
like image 93
pixelearth Avatar answered Oct 16 '22 12:10

pixelearth


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

module MyGem
  class Railtie < Rails::Railtie
    config.generators do |g|
      g.templates.unshift File::expand_path('../templates', __FILE__)
    end 
  end
end 

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_generators instead as per pixelearth's answer below.

like image 39
IdahoEv Avatar answered Oct 16 '22 12:10

IdahoEv


I have the same problem using rails 4.1.5. And here is assembled puzzle solution.

First of all create Railtie in your gem like this. Keep in mind config.generators is deprecated and thalespf`s answer.

module SomeGem
  class Railtie < Rails::Railtie
    config.app_generators do |g|
      g.templates.unshift File::expand_path('../../templates', __FILE__)
    end
  end
end

Works like a charm!

UPD. I have tried to create a gem with templates only and use it within Rails::Engine. But it requires this:

# lib/your_engine/engine.rb
require 'your_gem_with_generator_templates' # Loads Railtie
like image 3
18augst Avatar answered Oct 16 '22 12:10

18augst


As for rails 4 and rails engine I'm able to do this as:

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine

    config.generators do |g|
      g.templates.unshift File::expand_path("../templates", File.dirname(__FILE__))
    end
  end
end
like image 1
Roman Trofimov Avatar answered Oct 16 '22 11:10

Roman Trofimov


Only the path '../../templates' in g.templates.unshift File::expand_path('../../templates', _ FILE _) worked for me. Im in rails 3.2

like image 1
thalespf Avatar answered Oct 16 '22 11:10

thalespf