How to avoid to generate usefulness stuff in rails - ruby-on-rails

When i run the command
rails g controller admin/inbox
Its generate test_unit and helper/test_unit. But i dont want to generate it. how to avoid it during generate controller
create app/controllers/admin/inbox_controller.rb
invoke erb
create app/views/admin/inbox
invoke test_unit
create test/functional/admin/inbox_controller_test.rb
invoke helper
create app/helpers/admin/inbox_helper.rb
invoke test_unit
create test/unit/helpers/admin/inbox_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/admin/inbox.js.coffee
invoke scss
create app/assets/stylesheets/admin/inbox.css.scss

You can customize your workflow by configuring config/application.rb like this..
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => false
g.stylesheets false
end
for more details visit http://guides.rubyonrails.org/generators.html.

If, for some reason, you only want to skip the test generation just once, you can do this:
rails g controller admin/index --skip-test-framework
or
rails g controller admin/index --no-test-framework
This is covered in the documentation for Generators::Base.

Related

Can't seem to disable rails generator generating specs

In rails 4.2.0/ rspec 3.2.2/ rspec-rails 3.2.1. I'm trying to disable specs being generated when I generate new models. I'm using a spec folder structure that is different than the rails convention and would like to not have delete/move generated spec files for every new model. I tried to add generator configuration as mentioned in the rails guides and in What is the syntax to skip creating tests, assets & helpers when running `rails generate controller`?
My config/application.rb contains this:
config.generators do |g|
g.test_framework :rspec
g.model_specs false
g.view_specs false
g.helper_specs false
g.controller_specs false
g.model_spec false
g.helper_specs false
g.request_specs false
g.feature_specs false
end
and I'm still getting:
$rails g model category
invoke active_record
create db/migrate/20150416174523_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Even if I explicitly add tags:
$rails g model category --no-model-specs
invoke active_record
create db/migrate/20150416174908_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Anyone solved this before?
Simply set test_framework to something falsey if you want to disable all generators:
config.generators do |g|
g.test_framework nil
end
You sadly can't disable model specs from the generators alone. They're not optional.

Stop scaffold generates scafolds.css.scss

I'm using Rails 4
And refered these solutions by not works for me
ref
Run scaffold by
rails g scaffold Tool::Mvaas::P2pQuery --no-stylesheets user_id:integer interval:integer
But still generated scaffolds.css.scss for me
conflict app/assets/stylesheets/scaffolds.css.scss
Overwrite /Users/hsu-wei-cheng/workspace/Rails/dqa_dev_server/app/assets/stylesheets/scaffolds.css.scss?
Update
You can disable scaffold stylesheet placing this code on the file config/initializers/assets.rb:
Rails.application.config.generators do |g|
g.scaffold_stylesheet false
end
read this guides chapter "3.3 Configuring Generators".
Place into config/application.rb this code
config.generators do |g|
g.stylesheets false
end

Syntax to skip creating tests, assets & helpers for `rails generate controller`?

I read the help & tried the following command to skip generation of tests, assets & helper files
$ bin/rails generate controller home index --helper false --assets false --controller-specs false --view-specs false
create- app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke rspec
error false [not found]
error false [not found]
As you may notice by output above this works & only controller, routes & views are generated. But as last two lines are interesting:
error false [not found]
error false [not found]
Obviously rails doesn't seem to like --option-name false syntax. so this this error because I used the wrong syntax? If yes, then what is the correct way? Thanks
Try using --no- followed by optionname:
rails generate controller home index --no-helper --no-assets --no-controller-specs --no-view-specs
If you want to change the default behavior every time you run the generator command, you can configure the defaults you would like in the application.rb file - see How can I make sure Rails doesn't generate spec tests for views and helpers?.
To turn off without having to add options:
# application.rb
config.generators.assets = false
config.generators.helper = false
Applications which serve only API will not require javascript, stylesheet, views, helpers. To skip those files in generator/scaffold for Rails 3.x add the below code block in the application.rb
#to skip assets, scaffolds.css, test framework, helpers, view
config.generators do |g|
g.template_engine nil #to skip views
g.test_framework nil #to skip test framework
g.assets false
g.helper false
g.stylesheets false
end
check the link for more details about generators
More concisely:
rails g controller home index --no-assets --no-test-framework
Inside application.rb file write: This will stop generating everything apart from what is written in the command line
config.generators do |g|
g.test_framework nil
g.template_engine nil
g.assets false
g.helper false
g.stylesheets false
g.javascripts false
end
Example:
vidur#vidur-desktop:~/Downloads/tukaweb$ rails g controller uploader/three_d_models
Running via Spring preloader in process 3703
create app/controllers/uploader/three_d_models_controller.rb
invoke assets
invoke js
invoke scss
for one liner solution =>
rails g controller assets_garments --skip-test-framework --skip-assets --skip-helper
If you want to generate only controller, nothing else.
rails g controller [controller_name] [index] --no-helper --no-assets --no-template-engine --no-test-framework

haml by default

Is there a way to configure rails to use haml by default, i.e. when a scaffold is generated the according scaffold_name/index.html.haml is generated instead of scaffold_name/index.html.erb.
Similar to how you are able to add config.sass.preferred_syntax = :sass to config/application.rb and have scaffold_name.sass generated by default.
Tried adding the following to config/application.rb
config.generators do |g|
g.template_engine :haml
end
but ened up with the following
$ rails generate scaffold foo name:string
invoke active_record
create db/migrate/20120208152550_create_foos.rb
create app/models/foo.rb
invoke test_unit
create test/unit/foo_test.rb
create test/fixtures/foos.yml
route resources :foos
invoke scaffold_controller
create app/controllers/foos_controller.rb
error haml [not found]
invoke test_unit
create test/functional/foos_controller_test.rb
invoke helper
create app/helpers/foos_helper.rb
invoke test_unit
create test/unit/helpers/foos_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/foos.js.coffee
invoke sass
create app/assets/stylesheets/foos.css.sass
invoke sass
identical app/assets/stylesheets/scaffolds.css.sass
$ rails destroy scaffold foo
invoke active_record
remove db/migrate/20120208152550_create_foos.rb
remove app/models/foo.rb
invoke test_unit
remove test/unit/foo_test.rb
remove test/fixtures/foos.yml
route resources :foos
invoke scaffold_controller
remove app/controllers/foos_controller.rb
error haml [not found]
invoke test_unit
remove test/functional/foos_controller_test.rb
invoke helper
remove app/helpers/foos_helper.rb
invoke test_unit
remove test/unit/helpers/foos_helper_test.rb
invoke assets
invoke coffee
remove app/assets/javascripts/foos.js.coffee
invoke sass
remove app/assets/stylesheets/foos.css.sass
invoke sass
I created a nice little bundle command to replace all erb with haml files following this screencast but I'm still interested in making it default when the scaffold is created! How do I make it so haml files (not erb!) are generated by default?
I use gem 'haml-rails', '= 0.3.4' in my gemfile. it automatically generates *.html.haml without any configuration.
In your application config, try setting the following:
config.generators do |g|
g.template_engine :haml
end
if you have gem 'haml-rails' in your Gemfile it should create haml files by default instead of erb.
This is pretty simple!
All you need to do is to add the following to your Gemfile:
gem 'haml'
gem 'haml-rails'
and then run bundle install
Found this to be the complete solution
Say if you have an Rails Engine project named rails_address
Add the haml config to lib/rails_address/engine.rb
module RailsAddress
class Engine < ::Rails::Engine
isolate_namespace RailsAddress
config.generators do |g|
g.template_engine :haml
end
end
end
Added haml deps to rails_address.gemspec
...
s.add_dependency "rails", "~> 4.1.10"
s.add_dependency 'haml', '~> 4.0.6'
s.add_dependency 'haml-rails', '~> 0.9.0'
...
Lastly require the haml gems in lib/rails_address.rb
require "rails_address/engine"
require "haml"
require "haml-rails"
module RailsAddress
end
Execute a bundle install just incase you have not installed the haml gems yet.
Now when you generate via scaffold or controller you will create haml views.
ex.
$ rails g scaffold Address street:string city:string state:string zip_code:string
...
invoke haml
exist app/views/rails_address/addresses
create app/views/rails_address/addresses/index.html.haml
create app/views/rails_address/addresses/edit.html.haml
create app/views/rails_address/addresses/show.html.haml
create app/views/rails_address/addresses/new.html.haml
create app/views/rails_address/addresses/_form.html.haml
...
The haml [not found] error is usually because the bundle is incomplete. Have you tried running bundle update and then rerunning the generator?

Rails 3.1: for some reason generators now creating .js and not .js.coffee files?

Any one know why my Rails (3.1rc5) generators aren't creating CoffeeScript "templates" anymore?
Instead of:
$ rails g controller sessions new
create app/controllers/sessions_controller.rb
route get "sessions/new"
invoke haml
create app/views/sessions
create app/views/sessions/new.html.haml
invoke helper
create app/helpers/sessions_helper.rb
invoke assets
create app/assets/javascripts/sessions.js.coffee ##### <- HERE
invoke scss
create app/assets/stylesheets/sessions.css.scss
I get:
$ rails g controller sessions new
...
invoke assets
invoke js
create app/assets/javascripts/sessions.js
...

Resources