我正在学习编写生成器。我用过Rails'以scaffold_controller生成器为出发点。
require 'rails/generators/resource_helpers'
module Rails
module Generators
class ScaffoldControllerGenerator < NamedBase # :nodoc:
include ResourceHelpers
check_class_collision suffix: "Controller"
class_option :helper, type: :boolean
class_option :orm, banner: "NAME", type: :string, required: true,
desc: "ORM to generate the controller for"
argument :attributes, type: :array, default: [], banner: "field:type field:type"
def create_controller_files
template "controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
end
#hook_for :template_engine, :test_framework, as: :scaffold
end
end
end
正如您所看到的,我注释掉了hook_for
,希望这不会与测试框架生成器挂钩。但是,当我运行此生成器时,仍会生成测试。另外,我还想跳过jbuilder和helper生成。以下是生成的所有内容的列表:
create app/controllers/books_controller.rb
invoke erb
create app/views/books
create app/views/books/index.html.erb
create app/views/books/edit.html.erb
create app/views/books/show.html.erb
create app/views/books/new.html.erb
create app/views/books/_form.html.erb
invoke test_unit
create test/controllers/books_controller_test.rb
invoke helper
create app/helpers/books_helper.rb
invoke test_unit
invoke jbuilder
create app/views/books/index.json.jbuilder
create app/views/books/show.json.jbuilder
如何通过配置自定义生成器来生成test / helper / jbuilder?
答案 0 :(得分:0)
我意识到我实际上是猴子修补相同的生成器类,所以原始设置仍然完好无损。
我必须手动删除这些钩子。
remove_hook_for :jbuilder, :test_framework