haml by default - ruby-on-rails

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?

Related

How to install migration in Rails engine from other engine?

I have an engine, that has gem dependency. This gem has rake task to install migrations:
rake acts_as_taggable_on_engine:install:migrations
What is the proper way to install migration? When I run this command from host app or my engine I getting
Don't know how to build task
Add the gem dependency to you gemspec:
Gem::Specification.new do |s|
# ...
s.add_dependency 'acts-as-taggable-on', '~> 6.0'
# ...
end
Then require the gem in your engine:
# lib/my_engine/engine.rb
require 'acts-as-taggable-on'
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace Chatty
end
end
ActsAsTaggableOn should be loaded through the main file which requires the engine as well unlike some gems where you require gemname/engine - and the file naming is not snake_case like most gems.
Then run bundle install and rake acts_as_taggable_on_engine:install:migrations in the folder of the dummy application (or the host).
max#MaxBook ~/p/c/t/dummy> rake acts_as_taggable_on_engine:install:migrations
Copied migration 20181030123059_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123060_add_missing_unique_indices.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123061_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123062_add_missing_taggable_index.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123063_change_collation_for_tag_names.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123064_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
I don't know why but invoking the command through bundler (bundle exec ...) does not work. This may give problems with RVM if you are using shims.
You can also create a generator for your engine that invokes the task:
# lib/generators/my_engine/install/install_generator.rb
module MyEngine
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)
desc "Installs MyEngine"
def copy_initializer
# template 'my_engine.rb', 'config/initializers/my_engine.rb'
rake "acts_as_taggable_on_engine:install:migrations"
end
end
end
Which you can then run with rails g my_engine:install.

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.

How to avoid to generate usefulness stuff in 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.

Generate all RSpec spec files from existing controllers, models & views in a Rails app

Does anyone know of a rake task or RSpec call that will generate a bunch of empty files relative to the existing controllers, models, helper files and views that already exist within your application?
You can generate an empty scaffold set of rspec tests against an existing controller using something like this:
rails generate rspec:scaffold recipe
You can improve on this by passing the attributes of the model you want to generate against, like this:
rails generate rspec:scaffold recipe title: string slug: string description: text
You'll still need to do some manual editing, but this should get you most of the way there.
The best solution for this is to add hooks in place within environment.rb to create the spec.rb files within the rails application each time a model or controller is created.
Here's the code for that (using RSpec and FactoryGirl):
module RailsApp
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
This should work:
Install the rspec-rails gem by adding it to your development and test groups in your gemfile gem 'rspec-rails'
Run the rspec generator from inside your app rails generate rspec:install
Read over this doc quickly to see how it integrates with your rails app RSpec-rails doc

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