如何在Rails应用中覆盖宝石生成器模板 [英] how to override gem generator template in Rails app
问题描述
我知道如何覆盖gem中的Rails模板,但是我怎样才能在Rails应用程序中重写gem生成器模板? https://github.com/elabs/pundit/blob/master /lib/generators/pundit/policy/templates/policy.rb
或
< a href =https://github.com/drapergem/draper/blob/master/lib/generators/rails/templates/decorator.rb =nofollow> https://github.com/drapergem/draper/ blob / master / lib / generators / rails / templates / decorator.rb
使得 rails g decorator Foo
会生成我的模板,而不是gem原生的模板。
thx
在Rails 3.0及以上版本中,生成器不仅仅在源文件根目录
中查找模板,而且还在其他路径中搜索模板。其中
是lib / templates。
所以,如果你模仿gem / tamplate的目录层次结构,试图重写,rails会选择你的模板而不是gem source中的模板。
现在,问题是如何正确地模拟该层次结构,以便rails选择模板?
那么事实证明有某种类型的规则|模式,例如,如果你想覆盖这个路径中的模板: lib / generators / pundit / policy / templates / policy.rb
您应该将模板放置在 lib / template / pundit / policy / policy.rb中
重写 lib / generators / rails / templates / decorator.rb
您应该将模板放入 lib / templates / rails / decorator / decorator.rb
更新2
看起来模式正在流动: lib / templates / gem_name / generator_name / template_file_name.rb
在Draper gem的情况下,gem强制自己像本地Rails生成器那样工作:
/ draper / lib / generators / rails / templates / decorator.rb
...这就是为什么我们需要使用:
$ b
lib / template S /导轨/ generator_name / template_file_name.rb
。
覆盖Draper gem的RSpec模板生成器:
lib / templates / rspec / generator_name / template_file_name.rb
...等等
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
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/template/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
这篇关于如何在Rails应用中覆盖宝石生成器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!