I am learning to write generators. I used Rails' scaffold_controller generator as the starting point.
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
As you can see, I commented out the hook_for
, hoping this will not hook up the test framework generators. However when I run this generator, tests are still generated. Plus, I also want to skip jbuilder and helper generation. Here is a list of all things generated:
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
How can I skil generating test/helper/jbuilder by configuring my custom generator?
I realized that I was actually monkey patching the same generator class, so the original setting will still be intact.
I have to remove these hooks manually.
remove_hook_for :jbuilder, :test_framework