2

I know how to override a Rails template in a gem, but how can I override gem generator template in a Rails application

e.g.: https://github.com/elabs/pundit/blob/master/lib/generators/pundit/policy/templates/policy.rb

or

https://github.com/drapergem/draper/blob/master/lib/generators/rails/templates/decorator.rb

so that rails g decorator Foo would generate my template, not the gem native one

thx

equivalent8
  • 13,754
  • 8
  • 81
  • 109

3 Answers3

6

From Rails guide on generators:

In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is lib/templates.

So, if you mimic the directory hierarchy of the gem/tamplate you are trying to override, rails will pick your template instead of the ones in the gem source

Update:

Now, the question is how to correctly mimic that hierarchy so rails pick your template up?

Well it turned out there is some kind of a rule | pattern for that, for example if you want to override a template in this path: lib/generators/pundit/policy/templates/policy.rb

You should place your template in lib/templates/pundit/policy/policy.rb

To override lib/generators/rails/templates/decorator.rb

You should place your template in lib/templates/rails/decorator/decorator.rb

Update 2

It seems that the pattern is flowing: lib/templates/gem_name/generator_name/template_file_name.rb

In case of Draper gem, the gem is enforcing itself to act like native Rails generator:

/draper/lib/generators/rails/templates/decorator.rb

...so that's why we needed to use:

lib/templates/rails/generator_name/template_file_name.rb.

To override RSpec template generator of a Draper gem: lib/templates/rspec/generator_name/template_file_name.rb

...and so on

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Nimir
  • 5,727
  • 1
  • 26
  • 34
  • hey dude thx for helping. The thing is I tried every possible combination `lib/generators/pundit/policy/templates/policy.rb `, `lib/templates/policy.rb`, `lib/templates/haml/pundit/policy.html.haml` ... but the thing is the way how the gem is specifying the template path is like this https://github.com/elabs/pundit/blob/master/lib/generators/pundit/policy/policy_generator.rb – equivalent8 Feb 12 '14 at 16:55
  • 2
    @equivalent8 It's a very wild guess but try `lib/templates/pundit/policy/policy.rb` – Nimir Feb 12 '14 at 19:48
  • 1
    and for overriding Draper gem generator template I had to create: `lib/templates/rails/decorator/decorator.rb` – equivalent8 Feb 13 '14 at 11:58
  • @equivalent8 glad it worked:) i've updated my answer to cover what we found out – Nimir Feb 13 '14 at 12:25
  • 1
    It seems to me the pattern is flowing `lib/templates/gem_name/generator_name/template_file_name.rb` In case of Draper gem, gem is enforcing itself to act like native Rails generator: `draper/lib/generators/rails/templates/decorator.rb` so that's why we needed to use `lib/templates/rails/generator_name/template_file_name.rb` – equivalent8 Feb 13 '14 at 12:51
  • makes very good sense to me and i think you should edit the answer and put your pattern for ppl who will stuck on this in the future – Nimir Feb 13 '14 at 12:56
  • A tip for discovering generator names: run the scaffold generator and note what other generators it invokes. i.e. for draper, the generator is actually named 'decorator'. `invoke decorator create app/decorators/foo_decorator.rb invoke rspec create spec/decorators/foo_decorator_spec.rb` – Pathogen Jun 04 '14 at 18:23
1

To customize templates for twitter-bootswatch-rails gem's views generator, copy the whole content of its template folder into

lib/templates/bootswatch/themed

And run rails g bootswatch:themed YourModels

0

To copy the default Pundit generators to your Rails project, you can use this command:

mkdir -p lib/templates/pundit/policy && \
cp $(bundle info pundit --path)/lib/generators/pundit/policy/templates/* lib/templates/pundit/policy
Tim Krins
  • 3,134
  • 1
  • 23
  • 25