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 Ra
Only the path '../../templates' in g.templates.unshift File::expand_path('../../templates', _ FILE _) worked for me. Im in rails 3.2
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
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.
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
If anyone's struggling with this, I just got a very basic gem working using the other answers here which you can see for an example: templates
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