有没有办法在脚手架发电机完成后触发我自己的发电机?
类似rails g scaffold HighScore game:string score:integer
的事物将触发rails generate generator my_generator
创建的my_generator
答案 0 :(得分:0)
我用另一种方式解决了这个问题。
最初我的想法是:
您可以自定义脚手架工作流程as the documentation says
所以我定义了这样的脚手架生成器
config/application.rb
# resource_controller defines which generator to use
# for generating a controller when using rails generate the resource.
# Defaults to :controller.
# scaffold_controller different from resource_controller, defines which generator
# to use for generating a scaffolded controller when using rails generate scaffold.
# Defaults to :scaffold_controller.
config.generators do |gen|
gen.orm :active_record
gen.template_engine :haml
gen.test_framework :rspec,
fixtures: true,
view_specs: true,
helper_specs: true,
routing_specs: true,
controller_specs: true,
request_specs: true
gen.helper :helper_and_policy
gen.scaffold_controller :controller_with_smart_listing
gen.fixture_replacement :factory_bot, dir: "spec/factories"
end
如您所见,我只更改了适合我问题的gen.scaffold_controller
。但我认为也可以更改脚手架模型生成器。
然后我为生成器lib/generators/rails/controller_with_smart_listing/
创建了一个文件夹
我不记得我是如何生成此结构的,但是我在repository
中共享文件