Prevent custom generator from generating tests - ruby-on-rails

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

Related

using ActiveRecord argument in custom generator

i wanna this custom generator named form , generate a model and create its migration file in db/migrate
this is output :
rails g form test name:string
create app/models/name:string.rb
create app/controllers/name:strings_controller.rb
create app/javascript/api/name:string.js
create app/javascript/component/name:strings.vue
create app/javascript/pages/name:string/index.vue
create app/javascript/pages/name:string/layout.vue
create app/javascript/store/actions/name:string.js
create app/javascript/store/getters/name:string.js
create app/javascript/store/modules/name:string.js
create app/javascript/store/mutations/name:string.js
create db/migrate/20200816162324_create_name:strings.rb
and this is my custom generator class :
require 'rails/generators/active_record'
class FormGenerator < ActiveRecord::Generators::Base
source_root File.expand_path('templates', __FILE__)
source_root File.expand_path('templates', __dir__)
argument :model, type: :string
def create_template
template "models/form.template", "app/models/#{model}.rb"
template "controllers/forms_controller.template", "app/controllers/#{model}s_controller.rb"
template "javascript/api/form.template", "app/javascript/api/#{model}.js"
template "javascript/pages/component/forms.template", "app/javascript/component/#{model}s.vue"
template "javascript/pages/form/index.template", "app/javascript/pages/#{model}/index.vue"
template "javascript/pages/form/layout.template", "app/javascript/pages/#{model}/layout.vue"
template "javascript/store/actions/form.template", "app/javascript/store/actions/#{model}.js"
template "javascript/store/getters/form.template", "app/javascript/store/getters/#{model}.js"
template "javascript/store/modules/form.template", "app/javascript/store/modules/#{model}.js"
template "javascript/store/mutations/form.template", "app/javascript/store/mutations/#{model}.js"
migration_template "create_forms.template", "db/migrate/create_#{model}s.rb"
end
as you can see generator uses the column that i gave it to model instead of model itself
template "models/form.template", "app/models/#{model}.rb" //code
rails g form test name:string
create app/models/name:string.rb //output
what should i do to fix that?
From Ruby on Rails Guide, if you want to add custom command line arguments to a custom generator you have to declare it as a class_option. In this case your custom arguments are an array of columns. For example:
class FormGenerator < ActiveRecord::Generators::Base
...
class_option :columns, type: :array, default: [] # Options :type and :default are not required
def create_template
#columns = options[:columns] # You can use this variable inside template
...
end
end
For more information about class_option method click here.

Using partials when building custom generators

I am building custom generators to generate rails models. The generator can use one of many templates to build one model. Some logic is the same in all templates.
Is it possible to extract some parts of the templates into partials?
Here's an example of what I want to do:
lib/generators/custom_model/custom_model_generator.rb
class CustomModelGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :model_name, type: :string
argument :model_type, type: :string
...
include GeneratorsHelper
def generate_model
template_path =
case model_type
when 'car' then 'car_model.rb.erb'
when 'plane' then 'plane_model.rb.erb'
...
end
template template_path, "app/models/#{model_name}.rb"
end
end
Here is one template:
lib/generators/custom_model/templates/car_model.rb.erb
class <%= model_name.camelcase %> < ApplicationRecord
def start
puts "Vroum!"
end
end
The #start method will also be used in the other model generators. I would like to extract it it something like a partial. Is that possible?

Command to create a rake task with some folder

rails g task folder1/namespace1 task1
Above command will create the task1.rake inside lib/tasks/task1.rake
But, I need to keep my task1.rake inside lib/tasks/folder1/task1.rake
The rails g task generator does not work this way.
Here are the code for the TaskGenerator class
module Rails
module Generators
class TaskGenerator < NamedBase # :nodoc:
argument :actions, type: :array, default: [], banner: "action action"
def create_task_files
template 'task.rb', File.join('lib/tasks', "#{file_name}.rake")
end
end
end
end
As you see the lib/tasks path is hardcoded and you can not pass in options to alter the path.
I think this might be a great addition to the TaskGenerator class.
The answer to your question is that you have to make the folders manually.

Add custom generator in addition to default rails generators

What I need is to add some translations to i18n on scaffold generating. I'd like to know: is there possibility to force rails g scaffold to invoke my own generator in addition to defaults?
If not, how can I invoke default Rails generators (e.g. active_record) in my generator?
Thank you!
According to the doc here, you can use generate from your generator to invoke another generator.
generate a_generator_name, args_as_string
class FooGenerator < Rails::Generators::Base
argument :attributes, :type => :array
def call
do_your_stuff
run_scaffold
end
private
def do_your_stuff
end
def run_scaffold
generate 'scaffold', attributes.join(' ')
end
end

Customizing Rspec generators in Rails 3

I'm writing a Rails 3.1 engine and testing with RSpec 2. When I use rails generate, I get spec files generated for me automatically, which is so convenient:
$ rails g model Foo
invoke active_record
create db/migrate/20111102042931_create_myengine_foos.rb
create app/models/myengine/foo.rb
invoke rspec
create spec/models/myengine/foo_spec.rb
However, to make the generated specs play nicely with my isolated namespace, I have to wrap the spec each time manually in a module:
require 'spec_helper'
module MyEngine
describe Foo do
it "should be round"
...
end
end
I would love to know if there's a clean and easy way to modify the automatically generated spec 'templates' so that I don't have to wrap the spec in Module MyEngine each time I generate a new model or controller.
You can copy RSpec's templates using a Rake task like:
namespace :spec do
namespace :templates do
# desc "Copy all the templates from rspec to the application directory for customization. Already existing local copies will be overwritten"
task :copy do
generators_lib = File.join(Gem.loaded_specs["rspec-rails"].full_gem_path, "lib/generators")
project_templates = "#{Rails.root}/lib/templates"
default_templates = { "rspec" => %w{controller helper integration mailer model observer scaffold view} }
default_templates.each do |type, names|
local_template_type_dir = File.join(project_templates, type)
FileUtils.mkdir_p local_template_type_dir
names.each do |name|
dst_name = File.join(local_template_type_dir, name)
src_name = File.join(generators_lib, type, name, "templates")
FileUtils.cp_r src_name, dst_name
end
end
end
end
end
Then you can modify the code in #{Rails.root}/lib/templates/rspec/model/model_spec.rb to include the module name.
Copy the '/lib/generators/rspec/scaffold/templates/controller_spec.rb' file from the rspec-rails gem to your 'app/lib/templates/rspec/scaffold' folder, then customize it. Obviously, if you move to a new version of rspec-rails you will want to make sure that your customized template hasn't gotten stale.

Resources