简体   繁体   English

如何在Rails应用程序中覆盖gem generator模板

[英]how to override gem generator template in Rails app

I know how to override a Rails template in a gem, but how can I override gem generator template in a Rails application 我知道如何在gem中覆盖Rails模板,但是如何在Rails应用程序中覆盖gem generator模板

eg: https://github.com/elabs/pundit/blob/master/lib/generators/pundit/policy/templates/policy.rb 例如: 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 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 这样rails g decorator Foo会生成我的模板,而不是gem本机模板

thx 谢谢

From Rails guide on generators : 关于发电机的Rails指南

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. 在Rails 3.0及更高版本中,生成器不仅在源根目录中查找模板,而且还在其他路径中搜索模板。 And one of them is lib/templates. 其中之一是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 因此,如果您模仿要尝试覆盖的gem / tamplate的目录层次结构,rails将选择您的模板,而不是gem源中的模板

Update: 更新:

Now, the question is how to correctly mimic that hierarchy so rails pick your template up? 现在,问题是如何正确模拟该层次结构,以便Rails选择您的模板?

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 这样的模式,例如,如果您想在此路径中覆盖模板: lib/generators/pundit/policy/templates/policy.rb

You should place your template in lib/templates/pundit/policy/policy.rb 您应该将模板放在lib/templates/pundit/policy/policy.rb

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

You should place your template in lib/templates/rails/decorator/decorator.rb 您应该将模板放在lib/templates/rails/decorator/decorator.rb

Update 2 更新2

It seems that the pattern is flowing: lib/templates/gem_name/generator_name/template_file_name.rb 似乎该模式正在流动: 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 gem的情况下,gem会强制自己像本机的Rails生成器一样工作:

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

...so that's why we needed to use: ...所以这就是为什么我们需要使用:

lib/templates/rails/generator_name/template_file_name.rb . 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 覆盖Draper gem的RSpec模板生成器: lib/templates/rspec/generator_name/template_file_name.rb

...and so on ...等等

To customize templates for twitter-bootswatch-rails gem's views generator, copy the whole content of its template folder into 要为twitter-bootswatch-rails gem的视图生成器自定义模板, 请将 其模板文件夹的全部内容复制到

lib/templates/bootswatch/themed

And run rails g bootswatch:themed YourModels 并运行rails g bootswatch:themed YourModels

To copy the default Pundit generators to your Rails project, you can use this command: 要将默认的Pundit生成器复制到Rails项目中,可以使用以下命令:

mkdir -p lib/templates/pundit/policy && \
cp $(bundle show pundit)/lib/generators/pundit/policy/templates/* lib/templates/pundit/policy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM