Stop Rails generating spec tests for views and helpers? - ruby-on-rails

Is there a way to do this from the application.rb?

edit: short answer on top
If you want to do this for every time you run the generators, you do indeed customize it within your application.rb file. Put this code in the file inside the Application class definition.
config.generators do |g|
g.view_specs false
g.helper_specs false
end
You can also accomplish this by passing some options into the generator command. This Railscast goes over the process in more detail, but the basic idea is pretty simple.
Rails generators can take several options. You can see the options for the controller generator by running rails g controller -h. Assuming you have Rspec set up already, if you look at the output, you notice a section that says "Rspec options". It looks like this:
Rspec options:
[--controller-specs] # Indicates when to generate controller specs
# Default: true
[--view-specs] # Indicates when to generate view specs
# Default: true
To negate these boolean values, just pass them in with a "no" in front of the name. So if you wanted a controller with no specs for your view, you would call it like this:
rails g controller Foobar index show new create --no-view-specs
And you would get a controller with the correct views and actions created for you, but no specs for your views.
The same thing applies if you are using the scaffold generator. There is a --helper-specs option, so if you wanted no view or helper specs you would run:
rails g scaffold Foobar name:string --no-helper-specs --no-view-specs

Related

Custom erb generator called from scaffold

I am looking to not only customise my erb scaffold templates but also to add new templates.
In ScaffoldGenerator < Erb::Generators::Base I can see there there is a way to provide additional templates in %w(index edit show new _form).
So I have created a custom erb generator and templates directory in my application in lib/generators/erb/scaffold/.
However when I run rails g scaffold Something, my custom generator is not picked up by via scaffold_controller. Does this mean I need to provide a custom scaffold generator to use a custom scaffold_controller generator just so it can then use my erb generator?
I can also see the scaffold argument:
ScaffoldController options:
-e, [--template-engine=NAME] # Template engine to be invoked
# Default: erb
Am I able to provide my erb generator as the template engine to be used?
(Using Rails 4)
lib/templates/erb/scaffold is the correct path for rails scaffold.
Just in case...
model goes in /lib/templates/active_record/model/model.rb
controller goes in /lib/templates/rails/scaffold_controller/controller.rb

Don't create view folder on rails generate controller

Is there a way with the usual generators config to turn OFF the creation of the view folders and action templates when you run a rails generate controller?
I can't find an option anywhere and the code here doesn't show me any pointers.
We are likely going to be building our own controller / resource generators at some point anyway, for our API, but I was curious if there was a way to turn off this annoyance in the meantime.
It's not a well documented feature, but try to add --skip-template-engine (alias --no-template-engine) option to the command.
rails generate controller foo bar --skip-template-engine
demo on a dummy app:
rails g controller my_controller index show --no-template-engine
create app/controllers/my_controller_controller.rb
route get "my_controller/show"
route get "my_controller/index"
invoke test_unit
create test/functional/my_controller_controller_test.rb
invoke helper
create app/helpers/my_controller_helper.rb
invoke test_unit
create test/unit/helpers/my_controller_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/my_controller.js.coffee
invoke scss
create app/assets/stylesheets/my_controller.css.scss
To skip views from being generated with your controller, disable the template engine.
Once:
rails g controller ControllerName action1 action2 --skip-template-engine
Note that every --skip option also has an aliased --no option.
Default:
# config/application.rb
config.generators do |g|
g.template_engine false
end
# OR
config.generators.template_engine = false
If you have an API-only application (no front end), you may also want to skip assets and helpers from being generated with your controllers.
Once:
rails g controller api/users --no-helper --no-assets --no-template-engine
Default:
# config/application.rb
config.generators do |g|
g.assets false
g.helper false
g.template_engine false
end
# OR
config.generators.assets = false
config.generators.helper = false
config.generators.template_engine = false
Disabling assets skips stylesheets and javascripts from being generated. If you only want to skip one, use --no-stylesheets or --no-javascripts, or in config/application.rb use:
config.generators.stylesheets = false
config.generators.javascripts = false
If your default configuration skips something from being generated (e.g. assets and helpers) but you need them in one case, you can generate them like so:
rails g controller foo --helper --assets --skip
where --skip skips generating files that already exist.
Just thought I'd try underscoring the --skip-template-engine flag to see if it worked in a generator and it worked a charm! No view templates generated from a bin/rails g controller command in a Rails 4.2 application.
Try:
config.generators do |g|
g.template_engine false
end
A little late I know but these things stick around in Google! ;)
If you're creating an API with no front end, you can go ahead and use rails new --api. However, I don't recommend this option if you do plan to create a front end (for example a single page app) because it turns a lot of things off, including the asset pipeline.

Forcing override of file with template() in Rails generator?

I have myself a generator that, as part of a set of other operations, needs to set up a model class with a bunch of mixins, defaults, comments, etc.
I want to be using the same rails g model ... code (I'm calling invoke from my generator), but the problem is that there's a conflict because my template and the model generator's template are trying to splat each other:
$ be rails g entry_form karaoke events full_name:string group_name:string
create app/controllers/karaokes_controller.rb
create app/views/karaokes/show.html.erb
create app/views/karaokes/thanks.html.erb
route resource :karaoke
create app/models/karaoke_entry.rb
invoke active_record
create db/migrate/20111004004008_create_karaoke_entries.rb
conflict app/models/karaoke_entry.rb
Overwrite app/models/karaoke_entry.rb?
(enter "h" for help) [Ynaqdh]
Any recommendations how to get around this?
(The best I've come up with is to maybe move my model file creation to the bottom, and find some way to force template / copy_file to go ahead and overwrite the file without bothering the user, but I can't see any pre-existing way of doing this.)
Append force: true or skip: true to the template call:
template "model.rb", "app/models/#{model}.rb", force: true
Some group brainstorming turned up a way to work around this problem.
You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist. This works:
# Create the model definition from a template:
template "model.rb", "app/models/#{model}.rb"
# ... Later, get Rails to create everything else:
Rails::Generators.invoke("model", ["Example", "title:string", "--skip"])
(I would still welcome a way to allow template to override files.)

Rails scaffold without the css file?

Is there a way to generate a scaffold in rails 3.0 so that scaffold.css does NOT get created? Something at the command line I can enter to skip that step?
Thanks
There is a --no-stylesheets flag you can use:
rails g scaffold MyModel --no-stylesheets
You can also disable it by default -- in config/application.rb:
config.generators do |g|
g.stylesheets false
end
Rails itself only uses it for scaffold.css AFAIK, but unfortunately the same hook could be used by other generators, so you might need to remember to pass --stylesheets for a third-party gem that generates assets, for instance. It'd be really nice if Rails had an explicit option for scaffold.css :-/
You can find other generator options in the Rails Guides, by the way. Helpers are nice to turn off by default and generate them when you actually want them.
Since Rails 5.0, there is a configuration in config/application.rb which specifically disables generating the app/assets/stylesheets/scaffolds.css, but still generates the stylesheets for your new resource:
config.generators do |g|
g.scaffold_stylesheet false
end
You can also pass it in as the --no-scaffold-stylesheet command line option:
rails generate scaffold post title body:text --no-scaffold-stylesheet

Override default scaffold generator in rails 3

I've created a generator for a controller in rails 3.
Now I want to use this generator as the default generator when using the scaffolding generator.
Is that possible?
The correct position for your customized controller file is lib/templates/rails/scaffold_controller/controller.rb
If you simply want to use your own controller template, you can just put it in lib/templates/rails/scaffold_controller/controller.rb
If you want to replace the scaffold_controller_generator code itself, for example, so that the controller scaffold generates additional class files. you can create lib/generators/rails/my_scaffold_controller/my_scaffold_controller_generator.rb with templates under lib/generators/rails/my_scaffold_controller/templates.
Remember to point rails at your new scaffold_controller in config/application.rb:
config.generators do |g|
g.scaffold_controller = "my_scaffold_controller"
end
For my_scaffold_controller_generator.rb you could copy from the railties gem under railties-3.x.x/lib/rails/generators/rails/scaffold_controller if you want to modify default behaviour, or inherit from it if you just want to add functionality:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class MyScaffoldControllerGenerator < ScaffoldControllerGenerator
source_root File.expand_path("../templates", __FILE__)
def new_funtionality
end
end
end
end
You can override the templates that Rails uses for its generators. In this instance, just place the file at lib/templates/scaffold_controller/controller.rb and modify it how you wish. The next time you run rails g scaffold [modelName] it will pick up this new controller template and use it.
This is covered in Section 6 of the Creating and Customizing Rails Generators official guide.
This seems to have changed slightly with Rails 4. You can see which template the generator will look for in the invoke line when the scaffold is generated, and your template folder name should match this:
rails generate scaffold blub
...
invoke responders_controller
If you're using rails g scaffold_controller blubs the location of the template should be:
lib/templates/rails/scaffold_controller/controller.rb
If you're using rails g scaffold blub the location of the template should be:
lib/templates/rails/responders_controller/controller.rb
If anyone is wondering why this isn't working in a default Rails 4 install, it's because jbuilder is inserting itself into the template path before the override location. I don't need jbuilder so I removed it, but I also reported an issue in Github. Hopefully it'll be fixed soon.

Resources